PHP学习(3)-表单使用的总结

一.简单的列子

提交的表单,提交到welcome.php

<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

“welcome.php” 文件代码如下

<html>
<body>

Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>

</body>
</html>

用$_ POST来取值,get提交类似
补充:get提交与post提交的区别
GET::通过 GET 方法从表单发送的变量名和值都显示在 URL 中,可见。GET 对所发送信息的数量也有限制。限制在大于 2000 个字符。不过,由于变量显示在 URL 中,把页面添加到书签中也更为方便。GET 可用于发送非敏感的数据。不能使用 GET 来发送密码或其他敏感信息!
POST:通过 POST 方法从表单发送的信息对其他人是不可见的(所有名称/值会被嵌入 HTTP 请求的主体header中),并且对所发送信息的数量也无限制。
此外 POST 支持高阶功能,比如在向服务器上传文件时进行 multi-part 二进制输入。
不过,由于变量未显示在 URL 中,也就无法将页面添加到书签。

二.表单的验证

一个例子:

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
   名字: <input type="text" name="name" value="<?php echo $name;?>">
   <span class="error">* <?php echo $nameErr;?></span>
   <br><br>
   E-mail: <input type="text" name="email" value="<?php echo $email;?>">
   <span class="error">* <?php echo $emailErr;?></span>
   <br><br>
   网址: <input type="text" name="website" value="<?php echo $website;?>">
   <span class="error"><?php echo $websiteErr;?></span>
   <br><br>
   备注: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
   <br><br>
   性别:
   <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?>  value="female"><input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?>  value="male"><span class="error">* <?php echo $genderErr;?></span>
   <br><br>
   <input type="submit" name="submit" value="Submit"> 
</form>

1. $_SERVER[“PHP_SELF”] 变量

超级全局变量,返回当前正在执行脚本的文件名,与 document root相关。
所以, $_SERVER[“PHP_SELF”] 会发送表单数据到当前页面,而不是跳转到不同的页面。

2. htmlspecialchars()方法

把一些预定义的字符转换为 HTML 实体。预定义的字符是:
这里写图片描述

通过使用 htmlspecialchars() 函数能够避免 $_SERVER[“PHP_SELF”] 被利用。
如果用户在地址栏中键入了如下 URL:
http://www.example.com/test_form.php/%22%3E%3Cscript%3Ealert(‘hacked’)%3C/script%3E
在这种情况下,上面的代码会转换为:

<form method="post" action="test_form.php"/><script>alert('hacked')</script>

使用htmlspecialchars,会被保存为转义代码:
<script>location.href(‘http://www.hacked.com‘)</script>

3.验证表单数据

<?php
// 定义变量并设置为空值
$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;
}
// 定义变量并设置为空值
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";//传到页面显示出提示信息

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $nameErr = "Name is required";
  } else {
    $name = test_input($_POST["name"]);
  }

  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
  } 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 = "Gender is required";
  } else {
    $gender = test_input($_POST["gender"]);
  }
}
?>

通过 PHP trim() 函数,去除用户输入数据中不必要的字符(多余的空格、制表符、换行)
通过 PHP stripslashes() 函数,删除用户输入数据中的反斜杠(\)
empty验证表单是否为空。
接下来我们创建一个检查函数test_input()(相比一遍遍地写代码,这样效率更好)。

三.验证 E-mail 和 URL

1. 验证名字

以下代码展示的简单方法检查 name 字段是否包含字母和空格。如果 name 字段无效,则存储一条错误消息,
preg_match() 函数检索字符串的模式,如果模式存在则返回 true,否则返回 false。

$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
  $nameErr = "只允许字母和空格!"; 
}

2.验证 E-mail

以下代码展示的简单方法检查 e-mail 地址语法是否有效。

$email = test_input($_POST["email"]);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
  $emailErr = "无效的 email 格式!"; 
}

3.验证 URL

以下代码展示的方法检查 URL 地址语法是否有效(这条正则表达式同时允许 URL 中的斜杠)。

$website = test_input($_POST["website"]);
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%
=~_|]/i",$website)) {
  $websiteErr = "无效的 URL"; 
}

四.保留输入的值

如需在用户点击提交按钮后在输入字段中显示值,我们在以下输入字段的 value 属性中增加了一小段 PHP 脚本:name、email 以及 website。在 comment 文本框字段中,我们把脚本放到了 与 之间。这些脚本输出 name email、 website comment 变量的值。
然后,我们还需要显示选中了哪个单选按钮。对此,我们必须操作 checked 属性(而非单选按钮的 value 属性):

Name: <input type="text" name="name" value="<?php echo $name;?>">

E-mail: <input type="text" name="email" value="<?php echo $email;?>">

Website: <input type="text" name="website" value="<?php echo $website;?>">

Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>

Gender:

<input type="radio" name="gender"
<?php if (isset($gender) && $gender=="female") echo "checked";?>
value="female">Female
<input type="radio" name="gender"
<?php if (isset($gender) && $gender=="male") echo "checked";?>
value="male">Male

参考w3school

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值