如果错误数组为空,则返回表单中输入的数据

时间:2017-10-18 19:55:21

标签: php

我有这个代码

第一个文件是一个获取数据并执行一些基本电子邮件验证的表单 第二个文件获取所有数据并执行php验证,如果用户输入错误,则返回存储在数组中的错误消息。

我的问题是,如果没有错误并且错误数组为空,我如何显示表单的内容。

<?php
$error = $_GET['message'];


?>

<html>

      <head>

      <title>Form Validation</title>

      <script type="text/javascript">

             function validateEmail()

      {

         var email = document.getElementById('email').value;
         var reEmail = document.getElementById('reEmail').value;

         atpos = email.indexOf("@");

         dotpos = email.lastIndexOf(".");



         if (atpos < 1 || ( dotpos - atpos < 2 )) 

         {

            alert("Please enter correct email")

            document.getElementById('email').focus() ;


            return false;

         }

         if (email === reEmail){
            return true;
         }
         alert("emails don't match!");
         return false;





      }
      </script>

    </head>

      <body>

     <div>
         <?php

         if ($error == ""){

         }
         else{
            foreach ($error as $key => $value) {
               echo "<h1>". $value . "</h1>";


            }

         } 

         ?>
      </div>


      <form action="registerExec.php" method="post" name="myForm" onsubmit="return(validateEmail());">

         <table cellspacing="2" cellpadding="2" border="1">

            <tr>

               <td align="right">Name</td>

               <td><input type="text" name="name" /></td>

            </tr>

            <tr>

               <td align="right">Email</td>

               <td><input type="text" name="email" id="email" /></td>

            </tr>

            <tr>

               <td align="right">Retype Email</td>

               <td><input type="text" name="reEmail" id="reEmail" /></td>

            </tr>

            <tr>

               <td align="right">Zip Code</td>

               <td><input type="text" name="zip" /></td>

            </tr>

             <tr>

               <td align="right">Country</td>

               <td>

                  <select name="country">

                     <option value="-1" selected>[choose yours]</option>

                     <option value="1">USA</option>

                     <option value="2">UK</option>

                     <option value="3">INDIA</option>

                  </select>

               </td>

            </tr>

             <tr>

               <td align="right"></td>

               <td><input type="submit" value="Submit" /></td>

            </tr>

            </table>

      </form>

    </body>

</html>

第二档:

<?php
$email = $_POST['email'];
$zip = $_POST['zip'];
$name = $_POST['name'];
$message = array(" ");
$goodjob = 'goodjob';

if ($email == "" || $zip == "" || $name ==""){

    array_push($message, "Email, Zip, Name should not be empty!");
    // chedk if any of these fields is empty

}

if ($name != ""){
        if (is_numeric($name)) {
            array_push($message, "don't include numberss in name");

        }
    }

if (is_numeric ($zip) ){


} else {
    array_push($message, "zip is not a number!");
}

if (strlen($zip) != 5){
    array_push($message, "Wrong Zip!");
}



$finalmessage =  http_build_query(array('message' => $message));
header("Location: http://localhost/register/classexercise.php?".$finalmessage);

?>

1 个答案:

答案 0 :(得分:1)

在这里,试试这个,让我知道你是否期待别的东西。

    <?php
$error = $_GET['message'];
?>
<html>
<head>
  <title>Form Validation</title>
  <script type="text/javascript">
   function validateEmail(){
     var email = document.getElementById('email').value;
     var reEmail = document.getElementById('reEmail').value;
     atpos = email.indexOf("@");
     dotpos = email.lastIndexOf(".");

     if (atpos < 1 || ( dotpos - atpos < 2 )) 
     {
        alert("Please enter correct email")
        document.getElementById('email').focus() ;
        return false;
     }
     if (email === reEmail){
        return true;
     }
     alert("emails don't match!");
     return false;
  }
  </script>

</head>
  <body>

  <div>
     <?php
     if ($error == ""){
     }
     else{
        foreach ($error as $key => $value) {
           echo "<h1>". $value . "</h1>";
        }
     } 

     ?>
     </div>

  <form action="registerExec.php" method="post" name="myForm" onsubmit="return(validateEmail());">

     <table cellspacing="2" cellpadding="2" border="1">

        <tr>
           <td align="right">Name</td>
           <td><input type="text" name="name" /></td>
        </tr>
        <tr>
           <td align="right">Email</td>
           <td><input type="text" name="email" id="email" /></td>
        </tr>
        <tr>
           <td align="right">Retype Email</td>
           <td><input type="text" name="reEmail" id="reEmail" /></td>
        </tr>

        <tr>
           <td align="right">Zip Code</td>
           <td><input type="text" name="zip" /></td>
        </tr>
         <tr>
           <td align="right">Country</td>
           <td>
              <select name="country">
                 <option value="-1" selected>[choose yours]</option>
                 <option value="1">USA</option>
                 <option value="2">UK</option>
                 <option value="3">INDIA</option>
              </select>
           </td>
        </tr>
         <tr>
           <td align="right"></td>
           <td><input type="submit" value="Submit" /></td>
        </tr>
        </table>
  </form>
</body>
</html>

您的PHP显示表单内容 -

<?php
$email = $_POST['email'];
$zip = $_POST['zip'];
$name = $_POST['name'];
$country = $_REQUEST['country'];
$message = array();
$goodjob = 'goodjob';

if ($email == "" || $zip == "" || $name ==""){

    array_push($message, "Email, Zip, Name should not be empty!");
    // chedk if any of these fields is empty
}

if ($name != ""){
        if (is_numeric($name)) {
            array_push($message, "don't include numberss in name");
        }
    }

if (!is_numeric ($zip)){
    array_push($message, "zip is not a number!");
}

if (strlen($zip) != 5){
    array_push($message, "Wrong Zip!");
}

if (empty($message)){
    array_push($message, $name);
    array_push($message, $email);
    array_push($message, $zip);
    $finalmessage =  http_build_query(array('message' => $message));
    header("Location: index.php?".$finalmessage);
} else {
    $finalmessage =  http_build_query(array('message' => $message));
    header("Location: index.php?".$finalmessage);
}
?>