Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php验证中,当发现错误数据时,页面将被刷新,数据';已输入的将变为空。如何防止丢失?_Php_Jquery_Html_Forms - Fatal编程技术网

在php验证中,当发现错误数据时,页面将被刷新,数据';已输入的将变为空。如何防止丢失?

在php验证中,当发现错误数据时,页面将被刷新,数据';已输入的将变为空。如何防止丢失?,php,jquery,html,forms,Php,Jquery,Html,Forms,我是php的初学者。我在验证表单时尝试了php表单验证。当发现错误数据时,浏览器将显示一个警报框,当我单击它时,浏览器将重新加载页面,表单中已输入的详细信息将被刷新,我需要从第一个开始输入。 如何在验证时避免页面刷新。 对不起,我的英语很差 先谢谢你 这是我的php表单编码 formvalidate.php <html> <head> <title>USER REGISTRATION</title> </head> <body&g

我是php的初学者。我在验证表单时尝试了php表单验证。当发现错误数据时,浏览器将显示一个警报框,当我单击它时,浏览器将重新加载页面,表单中已输入的详细信息将被刷新,我需要从第一个开始输入。 如何在验证时避免页面刷新。 对不起,我的英语很差 先谢谢你

这是我的php表单编码 formvalidate.php

<html>
<head>
<title>USER REGISTRATION</title>
</head>
<body>
<form  name="registrationform" id="formid"   method="get";>
name:<input type="text" id="n1" name="name">  <br><br>
username:<input type="text" id="u1" name="username"> <br><br>
password:<input type="password" id="p1" name="password"> <br><br>
confirm password:<input type="password" id="p2" name="password2"> <br><br>
Address   :<textarea name="address" id= "a1"rows="4" cols="40"> </textarea><br><br>
phone   :<input id="ph" type="numbers" name="phone" ><br><br>
<input  type="submit" name="register"  value="register" >
<a href="login.php"><input id="button"  type="button"  value="cancel"> </a>
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="userlogin";
$conn=mysqli_connect($servername,$username,$password,$dbname);
    if(!$conn)
    {
    die("connection failed".mysqli_connect_error());
    }
    ?>
<?php   


    if (isset($_GET['register'])) {
    if(empty($_GET['name']))
    {
        echo'name should not empty';
            }

    if ( empty($_GET['username'])) {
    echo 'user name should not be empty';

    } else {
        $name1=$_GET['name'];
        $uname=$_GET['username'];

    $sql = "select * from contact where username='$uname'";

    $result=mysqli_query($conn,$sql);
    if($result->num_rows==1)
    {
        echo"username already exists";
    }
    else
    {
        echo"username available";
    }
    return false;
    }

    if((empty($_GET['password']))||(empty($_GET['confirmpassword'])))
    {
    echo'password and confirm password should not be empty';
    return false;
    }
    else{
    $pwd=$_GET['password'];
    $cpwd=$_GET['confirmpassword'];
    if($pwd.length==6){
    echo'password should not lesser than 6';
    }
    else
    {
    if($pwd==$cpwd){
    echo'password accepted';
    }
    else
    {
    echo 'password does not matched';
    return false;
    }
    }
    }

    if(empty($_GET['address']))
    {
    echo'address should not be empty';
    return false;
    }

    if(empty($_GET['phone']))
    {
    echo'phone should not be empty';
    return false;
    }
    }
?>
</body>
</form>
</html>

用户注册
名称:

用户名:

密码:

确认密码:

地址:

电话:


您可以在页面的开头执行检查,例如

$sParam1 = "";

if (isset ($_GET['param1']) ){
 $sParam1 = (string)$_GET['param1'];
}
将表单保留在页面底部,并在
值中回显变量
参数

<form method="get">
   <input type="text" name="param1" value="<?php echo htmlspecialchars($sParam1); ?>" />
</form>


Yoy还可以设置如下内容

if(isset($_GET['name']){echo“value=$_GET[name]”;}

对于每个输入属性

虽然我相信你应该在表单中使用POST属性,而不是tham GET


另外,您还没有为表单设置“action”属性!

请看这里。您有几个选项可以执行此操作。无论如何,我建议您应该使用外部php文件来处理数据(通过direct post或AJAX)。如果数据包含密码或敏感数据,请不要使用GET方法。此外,您的代码似乎是SQL可注入的,您必须在将数据发送到数据库之前对其进行清理(或者更好:使用)。使用javascript验证。因此,我需要检查用户名的可用性,以便输入数据库,并检查用户名是否可用。这可以用javascript完成。您还可以创建一个可验证的保留错误,例如
$sError
。每次发现错误、缺少字段、类型不正确等时,都将其附加到字符串。假设param1是int并且应该是字符串,则执行。
$sError.=“[Param1Int]”;
如果您觉得我的问题值得一提,请更新我的帖子。没问题,很高兴听到它在工作!sError变量也非常有用,不仅仅是为了防止代码的某些部分运行。您可以使用它添加错误类。假设您将字符串划界;
[param1NoContent][param2NotInt]
,您可以检查/访问html/class属性;
class=”“
谢谢您的建议,但我正在同一页中执行验证。因此,只有我使用了GETYou,您可以通过设置在同一页中执行验证