使用javascript警报框进行PHP表单验证

使用javascript警报框进行PHP表单验证,php,forms,Php,Forms,您好,我是PHP新手,我正在尝试提交一份注册表,它工作正常,但问题是,当它在警告框中给出一些错误,如用户名已存在或密码太短,然后它再次重新加载表单页面,用户必须再次填写整个表单时,我希望正确的字段保持不变 这是表单页面代码 <!DOCTYPE HTML> <html> <head> <title>Details</title> <link rel="stylesheet" type=

您好,我是PHP新手,我正在尝试提交一份注册表,它工作正常,但问题是,当它在警告框中给出一些错误,如用户名已存在或密码太短,然后它再次重新加载表单页面,用户必须再次填写整个表单时,我希望正确的字段保持不变

这是表单页面代码

<!DOCTYPE HTML> 
<html> 
    <head> 
        <title>Details</title> 
        <link rel="stylesheet" type="text/css" href="reg.css">
    </head> 
        <body id="body"> 
            <div id="mmw"> <span> MAP MY WAY </span></div>
            <form name="reg" id="reg" method="post" action="insert.php">
        <h2>Kindly fill up your Information</h2>
        <p>
    <input name="username" required class="name" placeholder="Type Your User name" />
        <input name="password" placeholder="Type Your Password" class="name" type="password" required />
            <input name="first_name" required class="name" placeholder="Type Your First name" />
            <input name="last_name" required class="name" placeholder="Type Your Last name" />
            <input name="email" required class="email" placeholder="Type a valid E-Mail address" />
            <input name="m_no" class="name" placeholder="Type Your Mobile #"/>
            <input name="v_name" required class="name" placeholder="Type Your Vahical model and name"/>
            <input name="capacity" required class="name" placeholder="Seating capacity"/>
            <input name="fuel_type" required class="name" placeholder="Runs on what fuel type"/>
    </p>



        <p>
            <input name="submit" class="btn" type="submit" value="Register" />
        </p>
        </form>

            </div> 
        </body> 
</html>
这是处理数据的页面

 <?php
  $con = mysqli_connect("localhost", "root", "", "map_my_way");
  // Check connection
  if (mysqli_connect_errno()) {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

  // escape variables for security
  $username = mysqli_real_escape_string($con, $_POST['username']);
  $password = mysqli_real_escape_string($con, $_POST['password']);
  $first_name = mysqli_real_escape_string($con, $_POST['first_name']);
  $last_name = mysqli_real_escape_string($con, $_POST['last_name']);
  $email = mysqli_real_escape_string($con, $_POST['email']);
  $m_no = mysqli_real_escape_string($con, $_POST['m_no']);
  $v_name = mysqli_real_escape_string($con, $_POST['v_name']);
  $fuel_type = mysqli_real_escape_string($con, $_POST['fuel_type']);
  $capacity = mysqli_real_escape_string($con, $_POST['capacity']); 


 $exists = mysqli_num_rows(mysqli_query($con,"SELECT * FROM members WHERE username='" . $username . "'"));
 if ($exists > 0) {


echo "<script language=\"JavaScript\">\n";
echo "alert('username already exists!');\n";
echo "window.location='reg.php'";
echo "</script>";

 } 

if (strlen ($password) < 6){

            echo "<script language=\"JavaScript\">\n";
            echo "alert('password must be 6 characters');\n";
            echo "window.location='reg.php'";
            echo "</script>";

 }
 else{

 // if ($password < 6) {
 // echo "<script language=\"JavaScript\">\n";
 // echo "alert('username already exists!');\n";
 // echo "window.location='reg.php'";
 // echo "</script>";

 // } else{

 //insert query
 $sql = "INSERT INTO members (username, password, first_name, last_name, email, m_no, v_name, fuel_type, capacity)
 VALUES ('$username', '$password', '$first_name', '$last_name', '$email', '$m_no', '$v_name', '$fuel_type', '$capacity')";  
 }
 //}    
 if (!mysqli_query($con, $sql)) {
    die('Error: ' . mysqli_error($con));

 }  
    else{
    header("location:pic.php");
    }



  // Register $username
   session_start();
   $_SESSION['login'] = true;
   $_SESSION['username'] = $username;

   mysqli_close($con);
   ?>
提前感谢

标题“位置:”;用javascript重新放置它

另外,尝试使转义字符串的函数类型更少:

function security($danger) {
mysqli_real_escape_string$con$danger}


只需使用用户名调用它,比如$username=security$\u POST['username']

您需要使用值=。。。当返回表单页面时,请给出一个示例,谢谢我尝试了您的代码,但提示:未定义变量:C:\xampp\htdocs\mapmyway1\reg.php中的first_name,在文本字段的第12行,我已更改了的代码,在reg.php中的第12行之前是否定义了$first_name?我没有任何javascript文件可以替换为所有我只在警报框中使用JS的文件,仅此而已。为什么不创建一个带有标题的时间函数,例如“$time=time$超时=$time+5//就像一个例子do{//do stuff}while$time<$timeout::把它放在一个if函数中,这样时间就从那里开始,然后做你的头函数,如果你想完美地超时^^^它就像我头上的10个故事一样,我对所有这些事情都很陌生。你能举例说明我的代码吗?