在表单的同一页面上显示PHP错误?

时间:2014-04-19 19:33:19

标签: php html forms

所以我有两个文件,第一个是表单本身

此处的格式为index.php

<!DOCTYPE html>
<html>
<body>
<form method="post" action="http://localhost/s/r.php" >
Name: <input type="text" name="name"><br>
Username: <input type="text" name="username"><br>
Email: <input type="text" name="email"><br>
Password: <input type="text" name="pass1"><br>
Password, again: <input type="text" name="pass2"><br>
<input type="submit">
</form>
</body>
</html>

然后我的r.php

<?php
include '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!";
    }

?>

我的db.php也与此问题无关。因此,我尝试使表单不会转到r.php并在出现错误时显示错误,而是在index.php中将其显示在表单旁边。有没有办法阻止它转到r.php或者我必须合并这两个脚本?

5 个答案:

答案 0 :(得分:2)

只需将您的r.php代码放入index.php,然后更改表单操作,只需将其设为action=""而不是action="http://localhost/s/r.php" 要防止自动执行php代码,可以使用isset

更改输入按钮,如下所示

Name: <input type="text" name="name" value="<?php if(!empty($name)){echo $name;}?>"><br>
Username: <input type="text" name="username" value="<?php if(!empty($username){echo $username;}"><br>
Email: <input type="text" name="email" value="<?php if(!empty($email)){echo $email;}?>"><br>
Password: <input type="text" name="pass1" value="<?php if(!empty($pass1)){echo $pass1;}?>"><br>
Password, again: <input type="text" name="pass2" value="<?php if(!empty($pass2)){echo $pass2;}?>"><br>
<input type="submit" name="submit" value="submit">

然后把你的PHP代码放在这里。

<?php
if(isset($_POST['submit'])){

//put the php code here.
}
?>

答案 1 :(得分:0)

你可以通过javascript验证或者同时制作html&amp; php在同一页面显示错误。

答案 2 :(得分:0)

您需要将错误消息作为HTTP帖子传递或将变量传递给index.php

您现在正处于echo错误,而不是设置$error变量, 然后你可以添加像

这样的东西
if ($error) {
   header("index.php?error=" + $error); // passing error(s) as an HTTP get variable via the querystring
}

然后在index.php中,您可以处理error查询变量。

答案 3 :(得分:0)

<!DOCTYPE html>
<html>
<body>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]?>" >
Name: <input type="text" name="name"><br>
Username: <input type="text" name="username"><br>
Email: <input type="text" name="email"><br>
Password: <input type="text" name="pass1"><br>
Password, again: <input type="text" name="pass2"><br>
<input type="submit">
</form>
</body>
</html>
<?php
if($_SERVER["REQUEST_METHOD"]=="POST")
  {
    $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!";
        }
  }       
 ?> 

答案 4 :(得分:0)

你可以使用XHR(ajax)使用javascript发送表格而不离开页面。 您将使用表单的onsubmit事件(返回false以防止默认行为)或按钮的onclick事件。您将在回调中更新您的表单:

var x=(window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');
x.onreadystatechange=function(){if(x.readyState==4&&x.status==200){
alert(x.responseText); //update your form
}}
x.open('GET','r.php',true);x.send();

你也可以从你的php发送一个javascript,将一个脚本元素附加到文档正文并设置其innerHTML(或innerText)