宽容他人,放过自己。

php提交表单相关_判断名字_邮箱_性别等

Posted on By anchoriteFili

<!DOCTYPE HTML> 
<html>
<head>
<meta charset="utf-8">
<title>表单提交(runoob.com)</title>
<style>
.error {color: #FF0000;}
</style>
</head>
<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 (!preg_match("/^[a-zA-Z ]*$/", $name)) {
            $nameErr = "只允许字母和空格";
        }
    }
    
    if (empty($_POST['email'])) {
        $emailErr = '邮箱是必须的';
    } else {
        $email = test_input($_POST['email']);
        
        if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) {
            $emailErr = '非法邮箱格式';
        }
    }
    
    if (empty($_POST['website'])) {
        $websiteErr = "";
    } else {
        $website = test_input($_POST['website']);
        
        if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $website)) {
            $websiteErr = "非法的URL的地址";
        }
    }
    
    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); // 去除用户输入数据中不必要的字符(如:空格,tab,换行)
    $data = stripslashes($data); // 去除用户输入数据中的反斜杠
    $data = htmlspecialchars($data); // 把一些预定义的字符转换为html实体,防止被黑
    return $data;
}
?>

<h2>PHP 表单验证实例</h2>
<p><span class="error">* 必须字段</span></p>

<!-- 
$_SERVER["PHP_SELF"]为超级全局变量,该变量可能被黑客使用,当黑客使用跨网站脚本的HTTP链接
来攻击时,$_SERVER["PHP_SELF"]服务器变量也会被植入脚本。原因就是跨网页脚本是负载执行文件的
路径后面的,因此$_SERVER["PHP_SELF"]的字符串就会包含HTTP链接后面的JavaScript程序代码
 -->
<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 rows="5" cols="40" name="comment"><?php echo $comment;?></textarea>
<br><br>

性别:
<input type="radio" name="gender" <?php if (isset($gender) && $gender == "female") echo "checked";?> value = "女"><input type="radio" name="gender" <?php if (isset($gender) && $gender == "male") echo "checked";?> value="男"><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>
</html>