php表单操作

来源:互联网 发布:gfx在mac上用不起 编辑:程序博客网 时间:2024/05/21 10:30
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>

<?php
$name = $email = $sex = $comment = $website = "";
$nameErr = $emailErr = $genderErr = $websiteErr = "";
//$_SERVER["REQUEST_METHOD"]表示访问页面的请求方式
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if(empty($_POST["name"])){
        $nameErr="Name is required";
    }else{
        $name = test_input($_POST["name"]);
    }
    if(empty($_POST["email"])){
        $emailErr="E-mail is required";
    }else{
        $email = test_input($_POST["email"]);
    }
    if(empty($_POST["website"])){
        $websiteErr="";
    }else{
        $website = test_input($_POST["website"]);
    }
    if(empty($_POST["comment"])){
        $comment="";
    }else{
        $comment = test_input($_POST["comment"]);
    }
    if(empty($_POST["sex"])){
        $genderErr="Gander is required";
    }else{
        $sex = test_input($_POST["sex"]);
    }
}

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);  //stripslashes() 函数删除由 addslashes() 函数添加的反斜杠。
   $data = htmlspecialchars($data);
   return $data;
}
?>

<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">
姓 &nbsp名: <input type="text" name="name" value="<?php echo $_POST["name"];?>">  * <?php echo $nameErr;?><br>  
E-mail: <input type="text" name="email" value="<?php echo $_POST["email"];?>">  * <?php echo $emailErr;?><br>  
网 &nbsp址: <input type="text" name="website"><br>
评 &nbsp论:<textarea name="comment" rows="5" cols="35"></textarea><br>
性 &nbsp别: <input type="radio" name="sex" value="male" <?php echo $_POST['sex']=="male" ? "checked" : "" ?> >男
            <input type="radio" name="sex" value="female" <?php echo $_POST['sex']=="female" ? "checked" : "" ?> >女 * <?php echo $genderErr;?><br>
<input type="submit" value="提交111">
</form>

<?php
    echo "<h2>您的输入是</h2>";
    echo $name."<br>";
    echo $email;
    echo "<br>";
    echo $website;
    echo "<br>";
    echo $comment;
    echo "<br>";
    echo $sex;
?>

</body>
</html>

0 0
原创粉丝点击