search
HomeBackend DevelopmentPHP TutorialPHP4实际应用经验篇(8)

作者:孙运动

你可能已经注意到,到目前为止,在我们给你的所有例子中,我们都是给你两个页面-一个单纯的具有表单的HTML页面,和另一个用来处理表单输入并产生相应输出的php脚本。然而,PHP提供了一种把那两个页面通过 $submit 变量结合在一起的文雅的方法

  你已经知道,一旦一个表单提交给PHP脚本,所有的表单变量就变成了的PHP变量。现在,除了用户定义的变量外,在你每次点击表单的“提交(SUBMIT )”按扭的同时,也就创建了一个名为 $submit的变量。因此,通过测试该变量是否存在,聪明的程序员可以只使用一个页面来既初始化表单又产生提交后的输出。 


  让我们给你示范一下-我们用一个页面来实现上面那个幸运甜饼例子,包括最初的选择日期页面和后面的幸运甜饼页面。我们假设新的PHP文件也叫"cookie.php"

--------------------------------------------------------------------------------

if (!$submit)
{
// 如果$submit不存在, 这暗示表单还没有提交
// 所以显示第一个页面

?>




td {font-family: Arial;}






The Amazing Fortune Cookie Generator







Pick a day



Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

















}
else
{

// 如果$submit 确实存在了,表单已经被提交了
// 所以使用switch()函数来处理

// 这里的决定变量是用户选择的日期
switch ($day)
 {

// 第一种情况
    case "Monday":
    $fortune = "当能找到使每件事做的复杂精彩的方法的时候,不要使它简单有效";
    break;

// 第一种情况
    case "Tuesday":
    $fortune = "生活是游戏的桥梁?-你一定施展某种巧计了。";
    break;

    case "Wednesday":
    $fortune = "什么能使头脑清醒的人生活在这个世界上永不发疯?";
    break;

    case "Thursday":
    $fortune = "不要疯狂,要有趣";
    break;

    case "Friday":
    $fortune = "仅仅跟着时代走,顺着风气行,当你得到提升时你会发现类型是个魔鬼。";
    break;


// 如果以上条件都不符合...
    default:
    $fortune = "很抱歉,周末关门";
    break;

    }

?>







Here is your fortune for :






}
?>
--------------------------------------------------------------------------------
  正如你所看到的,脚本首先测试 $submit变量是否存在,如果没找到,它将认为表单没有提交并且显示最初的日期选择列表。

   由于标签的 ACTION属性设为同一个 PHP脚本,一旦表单被提交,同一个脚本将被呼叫来处理表单的输入。然而,被呼叫后的这次$submit变量将已经存在了,所以将不再显示最初的页面,反而显示具有幸运甜饼的页面了。

   注意为了使这些能正常工作,你的


--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
  必须有一个赋予"submit"值的NAME属性。

--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain how load balancing affects session management and how to address it.Explain how load balancing affects session management and how to address it.Apr 29, 2025 am 12:42 AM

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Explain the concept of session locking.Explain the concept of session locking.Apr 29, 2025 am 12:39 AM

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Are there any alternatives to PHP sessions?Are there any alternatives to PHP sessions?Apr 29, 2025 am 12:36 AM

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Define the term 'session hijacking' in the context of PHP.Define the term 'session hijacking' in the context of PHP.Apr 29, 2025 am 12:33 AM

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

What is the full form of PHP?What is the full form of PHP?Apr 28, 2025 pm 04:58 PM

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

How does PHP handle form data?How does PHP handle form data?Apr 28, 2025 pm 04:57 PM

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

What is the difference between PHP and ASP.NET?What is the difference between PHP and ASP.NET?Apr 28, 2025 pm 04:56 PM

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

Is PHP a case-sensitive language?Is PHP a case-sensitive language?Apr 28, 2025 pm 04:55 PM

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.