>PHPHTML与表单
>>表单的实现
<form action = "welcom.php" method = "post">
name:<input type = "text" name = "name"><br>
E-mail:<input type = "text" name "email"><br>
</form>
>>分析action指明了表单数据会发送到welcome.php进行处理。
method指明了发送方式为post
>>.php对表单的处理
<body>
welcome
<?php
echo $_POST["name"];?>
<br>
Your email address is:
<?php
echo $_POST["email"];
?>
</body>
>>get和post的理解
二者都是创建了数组,包含键/值对,键是表单控件的名称,值来源于用户输入的数据
创建的数组被赋值给超全局变量$_GET和$_POST
$_GET通过URL参数传递到当前脚本的变量数组;对任何人可见,不超过2000个字符
$_POST通过http post传递到当脚本的变量数组;对其他人不可见,传输量没有要求
>表单验证
<body>
<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$website = test_input($_POST["website"]);
$comment = test_input($_POST["comment"]);
$gender = test_input($_POST["gender"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>PHP 验证实例</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
姓名:<input type="text" name="name">
<br><br>
电邮:<input type="text" name="email">
<br><br>
网址:<input type="text" name="website">
<br><br>
评论:<textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
性别:
<input type="radio" name="gender" value="female">女性
<input type="radio" name="gender" value="male">男性
<br><br>
<input type="submit" name="submit" value="提交">
</form>
<?php
echo "<h2>您的输入:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>
</body>
>>总结:
有value属性的,返回name和value属性,没有的返回name和用户输入的字段
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"];?>)>"
$_SERVER["PHP_SELF"]返回当前执行脚本的文件名(即本章php文件)
htmlspecialchars()这个函数将返回的文件名转换为html实体(也就是只能在html作为文本出现,将<作为转义字符传给html)
>>trim()函数,去除用户中输入数据中不必要的字符(空格、制表符、换行符)
stripslashes()函数,删除用户输入的反斜杠
>> $_SERVER["REQUEST_METHOD"]变量可以检测表单是否提交,若提交,进行处理;若没有提交,则返回空表单。
>表单必填
<body>
<?php
// 定义变量并设置为空值
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "姓名是必填的";
} else {
$name = test_input($_POST["name"]);
}
if (empty($_POST["email"])) {
$emailErr = "电邮是必填的";
} else {
$email = test_input($_POST["email"]);
}
if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
}
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["gender"])) {
$genderErr = "性别是必选的";
} else {
$gender = test_input($_POST["gender"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>PHP 验证实例</h2>
<p><span class="error">* 必需的字段</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
姓名:<input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
电邮:<input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
网址:<input type="text" name="website">
<span class="error"><?php echo $websiteErr;?></span>
<br><br>
评论:<textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
性别:
<input type="radio" name="gender" value="female">女性
<input type="radio" name="gender" value="male">男性
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="提交">
</form>
<?php
echo "<h2>您的输入:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>
</body>
>>解释:
定义了新的变量储存错误信息$nameErr = $emailErr = $genderErr = $websiteErr = ""
当必选框输入的信息为空时,错误信息变量会被赋值为错误信息,否则为空
在表单中,每个数据项后都添加了一个php脚本,用于执行错误信息的输出,从一开始就被执行了,只是变量值为空。
最后,在表单最后输出用户已经输入的表单项