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

在表单的同一页上显示PHP错误?,php,html,forms,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"><

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

下面是表单
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>
<?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
或者我必须将这两个脚本结合起来

您可以使用javascript验证,也可以在同一页面中同时使用html和php来显示错误

只需将您的
r.php
代码放入
index.php
中,然后更改表单action,只需将其设置为
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">

Name:您需要将错误消息作为HTTP post或get变量传递到
index.php

无论您当前在哪里
echo
ing errors now,请设置
$error
变量, 然后您可以添加如下内容

if ($error) {
   header("index.php?error=" + $error); // passing error(s) as an HTTP get variable via the querystring
}
然后在
index.php
中,您可以处理
错误
查询变量。


<!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!";
        }
  }       
 ?> 
您可以使用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)

Make
index.php
include
r.php
并在那里进行身份验证。另一种方法是将错误消息作为参数重定向回
index.phph
。请提供更详细的示例@MichaelCoxonJavascript可能会被删除。JS验证应该只作为客户端的“良好性”使用是的,这是使用客户端脚本的否定点。那么对于我的
r.php
它看起来像这样吗?我的
index.php
看起来像这样吗?接近。在
index.php
中,条件应该是
if($\u GET[“name”])
。相反,我只需要一个
$error
变量,并将其与任何错误消息一起附加(因为您可能有多个错误),然后按照我的回答执行重定向。文件中没有php标记吗?效果很好。但这里有几个问题。我是否可以摆脱
操作
,只使用
方法
?当用户提交无效数据时,我如何让输入保持不变?这样他们就不必重新键入它了?你可以去掉它,但我会把它保留为
action=“”
因为它是有效的html。查看我的编辑,对最后两个做同样的事情。您可能想添加
?>
,但现在我在尝试时得到了这个

注意:当我提交无效的infoNevermind时,在第5行
的/Users/idrisk/colority/s/index.php中未定义变量:name in/Users/idrisk/colority/s/index.php。我只需要把HTML放在文件的底部。是的,我忘记了结束标记,我还编辑了我的答案。祝你好运
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();