duandeng2265 2013-12-10 08:15
浏览 35
已采纳

php联系表单验证,在没有错误和成功的情况下停止显示表单

Here's my code:

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    if (empty($_POST["yourname"])) {
        $yournameErr = "Name is required";
    } else {
        $yourname = test_input($_POST["yourname"]);
    }

    if (empty($_POST["email"])) {
        $emailErr = "Email is required";
    } else {
        $email = test_input($_POST["email"]);
        // check if e-mail address syntax is valid
        if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) {
            $emailErr = "Invalid email format";
        }
    }

    if (empty($_POST["message"])) {
        $messageErr = "Message is required";
    } else {
        $message = test_input($_POST["message"]);
    }
}

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

I have got to the point where it doesn't show errors but I probably didn't explain myself too clearly. After the point at which it doesn't show error messages anymore, I would like the form to no longer appear and then I can put something down like "Successful." However I can't seem to achieve this.

my form is :

    <form action="contact.php" name="Form1" id="Form1" method="post">
<div>
<label>Your Name:</label>
<br />
<input type="text" name="yourname" id="yourname" placeholder="Full Name" 
    style="border:1;  border-color:#000000; " />
<span class="error">* <?php echo $yournameErr;?></span>
</div>
    <br />
<br />
<div>
<label> Email :</label> <br />
<input name="email" type="text" id="email" size="20" placeholder="Email" 
    style="border:1;  border-color:#000000; " />
<span class="error">* <?php echo $emailErr;?></span>
</div>
<br />
<br />
<div>
<label> Subject : </label><br />
<input name="subject" type="text" id="subject" size="20" placeholder="Subject" 
    style="border:1;  border-color:#000000; "  />
</div>
<br />
<br />
<div>
<label> Message :<br /> </label>
<textarea rows="5" cols="40" name="message" type="text" id="message" 
    placeholder="The message you want to send to us." style="border:1;  border-  
    color:#000000 " >
</textarea>
<span class="error">* <?php echo $messageErr;?></span>
</div> 
<br />
<br />
<div>
<input type="submit" name="button" id="button" style="border:1; border-
    color:#999999; " value="SEND"/>
</div>
</form>
  • 写回答

2条回答 默认 最新

  • douxiexie3574 2013-12-10 08:22
    关注

    What if you put your errors in an array and put a condition that check that array size and if it is 0 (no errors) echo the success message and don't show the form else join the errors and print it out.

    Maybe like this:

    contact.php

    <?php
    $error = array();
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["yourname"])) {
            $error['name'] = "Name is required";
        } else {
            $yourname = test_input($_POST["yourname"]);
        }
    
        if (empty($_POST["email"])) {
            $error['email'] = "Email is required";
        } else {
            $email = test_input($_POST["email"]);
            // check if e-mail address syntax is valid
            if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) {
                $error['email'] = "Invalid email format";
            }
        }
    
        if (empty($_POST["message"])) {
            $error['message'] = "Message is required";
        } else {
            $message = test_input($_POST["message"]);
        }
    
        if (!count($error)) {
             $noError = true;
        }
    }
    
    $successMessage = isset($noError) ? 'Successful.' : '';
    
    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }
    
    function getErrorMessage($type, $error)
    {
        return isset($error[$type]) ? $error[$type] : '';
    }
    
    if ($successMessage) {
        echo $successMessage;
    } else {
    ?>
    <form action="contact.php" name="Form1" id="Form1" method="post">
        <div>
            <label>Your Name:</label>
            <br/>
            <input type="text" name="yourname" id="yourname" placeholder="Full Name"
                   style="border:1px;  border-color:#000000; "/>
            <span class="error">* <?php echo getErrorMessage('name', $error); ?></span>
        </div>
        <br/>
        <br/>
    
        <div>
            <label> Email :</label> <br/>
            <input name="email" type="text" id="email" size="20" placeholder="Email"
                   style="border:1px;  border-color:#000000; "/>
            <span class="error">* <?php echo getErrorMessage('email', $error); ?></span>
        </div>
        <br/>
        <br/>
    
        <div>
            <label> Subject : </label><br/>
            <input name="subject" type="text" id="subject" size="20" placeholder="Subject"
                   style="border:1px;  border-color:#000000; "/>
        </div>
        <br/>
        <br/>
    
        <div>
            <label> Message :<br/> </label>
            <textarea rows="5" cols="40" name="message" type="text" id="message"
                      placeholder="The message you want to send to us." style="border:1px;  border-  
        color:#000000 "></textarea>
            <span class="error">* <?php echo getErrorMessage('message', $error); ?></span>
        </div>
        <br/>
        <br/>
    
        <div>
            <input type="submit" name="button" id="button" style="border:1px; border-
        color:#999999; " value="SEND"/>
        </div>
    </form>
    <?php } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?