打破PHP脚本

时间:2014-01-09 00:09:30

标签: php

我在下面有一个简单的表单,我在提交的表单中检查零字段值。如果我找到一个零字段,我会相应地设置$ register_failed_message,识别该字段。此时我只想跳过表单处理的其余部分并显示表单,因为表单包含

<?=$register_failed_message?>

告诉用户,在表单上,​​问题是什么。

但目前尚不清楚如何突破处理循环并跳转到表单显示HTML。我现在已退出,但这不会起作用,因为它们会停止整个脚本。我需要转到?&gt;。

有没有一种标准的方法来编程这种东西?

由于

<?php

    if(!empty($_POST)){
        // Form was submitted.

         if(empty($_POST['firstName'])) {
            $register_failed_message = "Please enter a firstName.";
            exit;
        }


    if(empty($_POST['lastName'])) {
            $register_failed = "Please enter a lastName.";
             exit;
        }



        if(empty($_POST['email'])) {
            $register_failed = "Please enter an email.";
            exit;
        }

        [Process form]
        header("Location: login.php");
        exit;
}
?>

<!doctype html> 
<!-- HTML5  -->
<html>
<head>
<body>


    <div id="content">
         <h2 id="heading">Open a Free Account . . .</h2>
          <form id='register' action=<?= $_SERVER["PHP_SELF"] ?> autocomplete="off" method="post">
              <div id='fname_label' class='label'>First Name:</div> 
              <input id='fname' type="text" name="firstName" />

             <div id='lname_label' class='label'>Last Name:</div> 
             <input type="text" name="lastName" />

             <div id='email_label' class='label'>Email:</div> 
                <input type="text" name="email"  autocomplete="off" />



               <input id='register_button' type="submit" value="Open your Free Account" />
            </form> 
            <div id='register_failed'><?=$register_failed_message?></div>
    </div>
 </div>

</body>
</head>
</html>

4 个答案:

答案 0 :(得分:2)

您的代码存在一些问题。首先,您有两个消息变量$ register_failed_message和$ register_failed,但您稍后只引用一个。其次,您的消息将相互覆盖。如果将它们放在一个数组中,您可以显示所有消息,而不仅仅是一个消息。

以下是您尝试做的事情,以及修复过的两个错误。

<?php
$register_failed = array();
if(!empty($_POST)){
    // Form was submitted.

    if(empty($_POST['firstName'])) {
        $register_failed[] = "Please enter a firstName.";
    }

    if(empty($_POST['lastName'])) {
        $register_failed[] = "Please enter a lastName.";
    }

    if(empty($_POST['email'])) {
        $register_failed[] = "Please enter an email.";
    }

    //If there are no error messages, we can process the form
    if(sizeof($register_failed) == 0) { //This means there are 0 messages collected
        [Process form]
        header("Location: login.php");
        exit;   //we exit here to get to the other page
    }
}
?>

随后:

        <div id='register_failed'>
           <?php echo implode('<br>',$register_failed); ?>
        </div>

答案 1 :(得分:1)

<?php

$register_failed_message = '';

if(isset($_POST)) 
{
    // Form was submitted.
    $error = array();
    if(empty($_POST['firstName'])) 
    {
        $error[] = "Please enter a firstName.";
    }

    if(empty($_POST['lastName'])) 
    {
        $error[] = "Please enter a lastName.";
    }

    if(!preg_match('#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s]+\.+[a-z]{2,6}))$#si', $_POST['email'])) 
    {
        $error[] = "Please enter an email.";
    }

    if(count($register_failed_message) == 0)
    {
        header("Location: login.php");
        die();
    }

    $register_failed_message = implode('<br />',$error);
}

?>

答案 2 :(得分:0)

您可以使用“return”中断脚本,但它也会停止页面内容。

您可以将脚本放入另一个文件中,使用“return”停止脚本,然后将其包含在您提供的页面中。

答案 3 :(得分:0)

我打算像评论中那样建议break(),但这不是你最好的解决方案。你真的不想抛出一条消息“请输入一个名字”然后让他们输入一个名字然后扔另一条消息“请输入一个姓氏”。所以不要破坏,只收集所有错误消息并显示它们:

(您是否也注意到您对该邮件的命名不一致?)

<?php

    if(!empty($_POST)){
        // Form was submitted.
         $register_failed_message = "";
         if(empty($_POST['firstName'])) {
            $register_failed_message .= "Please enter a firstName.<br />";

        }


    if(empty($_POST['lastName'])) {
            $register_failed_message .= "Please enter a lastName.<br />";

        }



        if(empty($_POST['email'])) {
            $register_failed_message .= "Please enter an email.<br />";

        }

        header("Location: login.php");

}
?>

注意我如何使用.=。这是一个方便的快捷方式,意思是“附加到”而不是“设置为”。