dsgwdigu84950 2015-07-19 16:07
浏览 28
已采纳

注册验证,同一页面上显示错误消息

I am currently creating a registration page and checking for errors. I have the errors working correctly but when I report them, they end up on a new page. I was wondering how I get it to not redirect to a different page?

I echo out a div that I was thinking would appear next to the form but that is not occuring. Here is my form and error validation:

<form action="signup.php" method="post">
                <input type="text" name="First_name" placeholder="First Name" />
                <input type="text" name="Last_name" placeholder="Last Name" />
                <input type="text" name="Email" placeholder="Email Address" />
                <input type="text" name="Email2" placeholder="Comfirm Email Address" />
                <input type="password" name="Password" placeholder="Password" />
                <input type="submit" value="Submit" name="submit"/>
</form>

This is part of my php file.

if (isset($_POST['submit'])) {
$error = array();
$firstname=$_POST['First_name'];
$lastname=$_POST['Last_name'];
$email=$_POST['Email'];
$email2=$_POST['Email2'];
$password=$_POST['Password'];

if (strlen($password) <= 6 and strlen($password) >= 1) {
    if (strlen($password) >= 20) {
        if (strlen($email) >= 1 and strlen($email) <= 55) {
            if ($email == $email2) {
                if (ereg('^[a-zA-Z0-9\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$', $email)) {
                        $sql = "INSERT INTO Users (First_Name, Last_Name, Email, Password) VALUES ('$firstname','$lastname','$email','$password')";
                            echo "Regiration Complete. Check Email for Validation.";
                            exit;
                    } else {
                        $error[] = 'Email is wrong';
                    }
                } else {
                    $error[] = 'Email addresses are not the same';
                }
            } else {
                $error[] = 'Email Address is too long';
            }
        } else {
            $error[] = 'Password is too long';
        }
    } else {
        $error[] = 'Password must be 6 Characters';
    }
}
echo '<div class="errormsgbox"> <ol>';
    foreach ($error as $key => $values) {
        echo '  <li>'.$values.'</li>';
    }
echo '</ol></div>';
  • 写回答

3条回答 默认 最新

  • dongyi1441 2015-07-19 16:38
    关注

    You can use HTML5 to validate these Data without PHP - so the form doesn't even get sent if its violating your standards.

    You can read more about it here:

    http://www.the-art-of-web.com/html/html5-form-validation/

    To your question:

    You load a different page. This means, that you have to include the form there, too, if you want to get it shown there. Otherwise it won't display.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?