阻止加载页面

时间:2014-04-23 02:41:55

标签: php html forms

如果出现错误,如何阻止我的PHP表单进入下一步?所以基本上我有一个名为index.php的表单和一个名为register.php的PHP文件来处理表单。如果有错误,我怎样才能register.phpindex.php发送错误而不转到register.php?所以基本上我想将错误从register.php传递到index.php,如果有错误,用户就不会转到register.php。我希望这是有道理的:)

register.php

<?php
include '/Users/idrisk/Colourity/s/db.php';

$name = mysqli_real_escape_string($con, $_POST['name']);
$username = mysqli_real_escape_string($con, $_POST['username']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$pass1 = mysqli_real_escape_string($con, $_POST['pass1']);
$pass2 = mysqli_real_escape_string($con, $_POST['pass2']);

// Verification

if (empty($name) || empty($username) || empty($email) || empty($pass1) || empty($pass2))
    {
    echo "Complete all fields";

    // you can stop it here instead of putting the curly brace ALL the way at the bottom :)

    return;
    }

// Password match

if ($pass1 <> $pass2)
    {
    echo $passmatch = "Passwords don't match";
    }

// Email validation

if (!filter_var($email, FILTER_VALIDATE_EMAIL))
    {
    echo $emailvalid = "Enter a  valid email";
    }

// Password length

if (strlen($pass1) <= 6)
    {
    echo $passlength = "Password must be at least 6 characters long";
    }

// Password numbers

if (!preg_match("#[0-9]+#", $pass1))
    {
    echo $passnum = "Password must include at least one number!";
    }

// Password letters

if (!preg_match("#[a-zA-Z]+#", $pass1))
    {
    echo $passletter = "Password must include at least one letter!";
    }



?>

我的index.php

    <!DOCTYPE html>
<html>
<body>
<form method="post" action="register.php">
     Name: <input type="text" name="name" value="<?php if(!empty($_post['name'])){echo $name;}?>"><br>
     Username: <input type="text" name="username" value="<?php if(!empty($_post['username'])){echo $username;}?>"><br>
     Email: <input type="text" name="email" value="<?php if(!empty($_post['email'])){echo $email;}?>"><br>
     Password: <input type="text" name="pass1" value="<?php if(!empty($_post['pass1'])){echo $pass1;}?>"><br>
     Password, again: <input type="text" name="pass2" value="<?php if(!empty($_post['pass2'])){echo $pass2;}?>"><br>
    <input type="submit" name="submit" value="submit">
</form>
</body>
</html>

所以我希望register.php的错误转到index.php,是的,我知道我的代码不安全。它只是在我的电脑上用作演示。

5 个答案:

答案 0 :(得分:0)

您实际上并未发布到register.php,而是发布到同一页index.php

这似乎是正确的做法,因为现在你可以使用:

include 'register.php';

位于脚本的顶部,以便在同一页面中处理变量。然后 - 在成功提交后 - 您可以将访问者重定向到另一个显示成功消息的页面。

如果表单包含错误,您可以在表单本身/上方/下方显示它们。

答案 1 :(得分:0)

有两种方法可以做到这一点

1 - 当您可以在同一页面上处理时,无需将信息发送到另一个页面

<form method="POST" action="<?php echo $_SERVER["PHP_SELF"];>">

在这种情况下,您可以使用

if (condition here) { 
// if true //
header('Location: register.php');
}
else {
echo "Error";
}

2 - 您也可以在register.php上设置临时cookie,如果有错误, 所以在index.php上你可以有

if (error) {
setcookie("Error","Invalid Login",strtotime("+1 minute");
}

和index.php

if (isset($_COOKIE["Error"]) {
echo "Invalid Login";
}

答案 2 :(得分:0)

如果这只是一个演示,你可以做的是将所有内容都放在一个页面中。

您的index.php将包含一个action="index.php"

的表单
<?php
include '/Users/idrisk/Colourity/s/db.php';

if(isset($_POST["submit"]) && !empty($_POST["submit"])) {

    $name = mysqli_real_escape_string($con, $_POST['name']);
    $username = mysqli_real_escape_string($con, $_POST['username']);
    $email = mysqli_real_escape_string($con, $_POST['email']);
    $pass1 = mysqli_real_escape_string($con, $_POST['pass1']);
    $pass2 = mysqli_real_escape_string($con, $_POST['pass2']);



    // Verification

    if (empty($name) || empty($username) || empty($email) || empty($pass1) || empty($pass2))
    {
        echo "Complete all fields";

        // you can stop it here instead of putting the curly brace ALL the way at the bottom :)

        return;
    }

    // Password match

    if ($pass1 <> $pass2)
    {
        echo $passmatch = "Passwords don't match";
    }

    // Email validation

    if (!filter_var($email, FILTER_VALIDATE_EMAIL))
    {
        echo $emailvalid = "Enter a  valid email";
    }

    // Password length

    if (strlen($pass1) <= 6)
    {
        echo $passlength = "Password must be at least 6 characters long";
    }

    // Password numbers

    if (!preg_match("#[0-9]+#", $pass1))
    {
        echo $passnum = "Password must include at least one number!";
    }

    // Password letters

    if (!preg_match("#[a-zA-Z]+#", $pass1))
    {
        echo $passletter = "Password must include at least one letter!";
    }

// check to see if all of the error variable are empty
if(empty($passmatch) && empty($emailvalid) && empty($passlength) && empty($passnum) && empty($passletter)) {
    // everything was empty so we have a good form
    // redirect to another page
    header('Location: example.com/');
}
}

?>


<!DOCTYPE html>
<html>
<body>
<form method="post" action="index.php">
     Name: <input type="text" name="name" value="<?php if(!empty($_post['name'])){echo $name;}?>"><br>
     Username: <input type="text" name="username" value="<?php if(!empty($_post['username'])){echo $username;}?>"><br>
     Email: <input type="text" name="email" value="<?php if(!empty($_post['email'])){echo $email;}?>"><br>
     Password: <input type="text" name="pass1" value="<?php if(!empty($_post['pass1'])){echo $pass1;}?>"><br>
     Password, again: <input type="text" name="pass2" value="<?php if(!empty($_post['pass2'])){echo $pass2;}?>"><br>
    <input type="submit" name="submit" value="submit">
</form>
</body>
</html>

答案 3 :(得分:0)

在index.php中

将action属性值表单设置为register.php。

然后在register.php的最顶端。

检查是否为空字段,然后使用header()函数重定向到index.php并退出;


如果发现错误,则在register.php中显示示例代码。

header("Location: index.php");
exit;

答案 4 :(得分:0)

您好,请尝试使用会话或Cookie。

register.php

<?php
session_start();

include '/Users/idrisk/Colourity/s/db.php';

$name = mysqli_real_escape_string($con, $_POST['name']);
$username = mysqli_real_escape_string($con, $_POST['username']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$pass1 = mysqli_real_escape_string($con, $_POST['pass1']);
$pass2 = mysqli_real_escape_string($con, $_POST['pass2']);

$error = array();

// Verification

if (empty($name) || empty($username) || empty($email) || empty($pass1) || empty($pass2))
{
    $empty = "All Fields must be filled up!";
    array_push($error, $empty);
    $_SESSION['error'] = $error;
    header('Location: index.php');
}

// Password match

if ($pass1 <> $pass2)
{
    $passmatch = "Passwords don't match";
    array_push($error, $passmatch);
    $_SESSION['error'] = $error;
}

// Email validation

if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
    $emailvalid = "Enter a  valid email";
    array_push($error, $emailvalid);
    $_SESSION['error'] = $error;
}

// Password length

if (strlen($pass1) <= 6)
{
    $passlength = "Password must be at least 6 characters long";
    array_push($error, $passlength);
    $_SESSION['error'] = $error;
}

// Password numbers

if (!preg_match("#[0-9]+#", $pass1))
{
    $passnum = "Password must include at least one number!";
    array_push($error, $passnum);
    $_SESSION['error'] = $error;
}

// Password letters

if (!preg_match("#[a-zA-Z]+#", $pass1))
{
    $passletter = "Password must include at least one letter!";
    array_push($error, $passletter);
    $_SESSION['error'] = $error;
}

header('Location: index.php');

?>

的index.php

<!DOCTYPE html>
<html>
<body>
<?php
session_start();
if(!empty($_SESSION["error"]))
{
    foreach($_SESSION["error"] as $error)
    {
        echo "<div>" . $error . "</div>";
    }
}
unset($_SESSION["error"]);
?>
<form method="post" action="register.php">
Name: <input type="text" name="name" value="<?php if(!empty($_post['name'])){echo $name;}?>"><br>
Username: <input type="text" name="username" value="<?php if(!empty($_post['username'])){echo $username;}?>"><br>
Email: <input type="text" name="email" value="<?php if(!empty($_post['email'])){echo $email;}?>"><br>
Password: <input type="text" name="pass1" value="<?php if(!empty($_post['pass1'])){echo $pass1;}?>"><br>
Password, again: <input type="text" name="pass2" value="<?php if(!empty($_post['pass2'])){echo $pass2;}?>"><br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>