php之form表单

Posted 宝清老窖

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php之form表单相关的知识,希望对你有一定的参考价值。

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta charset="UTF-8"/>
        <style type="text/css">
            .error{
                color: #FF0000;
            }
        </style>
    </head>
    <body>
        <?php
            $name = $email = $gender = $comment = $website = $nameErr = $emailErr = $websiteErr = $textarea = $gender = "";
            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"]);
                }
                $gender = test_input($_POST["gender"]);
                $comment = test_input($_POST["commet"]);
                $website = test_input($_POST["website"]);
            }
            
            function test_input($data){
                $data = trim($data);
                $data = stripslashes($data);
                $data = htmlspecialchars($data);
                return $data;
            }
        ?>
        <h2>验证实例</h2>
        <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"])?>">
            姓名:<input type="text" name="name"/><span class="error"><?php echo $nameErr; ?></span><br />
            邮件:<input type="email" name="email"/><span class="error"><?php echo $emailErr; ?></span><br />
            网址:<input type="text" name="website" /><span class="error"><?php echo $websiteErr; ?></span><br />
            评论:<textarea name="commet" cols="30" rows="10"></textarea><?php echo $textarea; ?><br />
            性别:
                <input type="radio" name="gender" value="female"/>女性
                <input type="radio" name="gender" value="male" checked="checked"/>男性<br /><br />
                <span class="error"><?php echo $gender; ?></span>
            <input type="submit" value=" 提交" />
        </form>
        <?php
            echo "<h2>您的输入:</h2>";
            echo $name;
            echo "<br />";
            echo $email;
            echo "<br />";
            echo $gender;
            echo "<br />";
            echo $comment;
            echo "<br />";
            echo $website;
            echo "<br />";
            
        ?>
    </body>
</html>

 

以上是关于php之form表单的主要内容,如果未能解决你的问题,请参考以下文章

php获取form表单数据

SpringBoot中表单提交报错“Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported“(代码片段

form表单提交方式之1

php表单得不到post

php form表单怎么把数据提交到本页而不跳转?

php表单传值问题