PHP,Ajax表单验证

时间:2014-03-25 23:45:01

标签: php ajax json

我有这个表单,我正在尝试使用PHP验证用户输入。如果输入框为空,则打印#debug div中的所有错误消息。这是我想在页面底部显示的JSON数组,用于调试目的。

<h2>Form Validation with AJAX,JQuery,JSON and PHP</h2>
          <div class="form-container">    
              <span id="ajax-message"></span>


              <form id="ajax-form" onsubmit="return false;">
                  <p class="legend">All fields marked with an asterisk are required.</p>

                  <fieldset>
                      <legend>User Details</legend>
                      <div>
                        <label for="uname">Username <em>*</em></label> 
                        <input id="uname" type="text" name="uname" value=""  />
                      </div>
                      <div>
                        <label for="email">Email Address <em>*</em></label> 
                        <input id="email" type="text" name="email" value="" />
                      </div>
                      <div>
                        <label for="fname" class="error">First Name <em>*</em></label> 
                        <input id="fname" type="text" name="fname" value="" size="50" class="error"/>
                        <p class='note'>This is where the error message should go </p> 
                      </div>
                      <div>
                        <label for="lname">Last Name <em>*</em></label> 
                        <input id="lname" type="text" name="lname" value="" size="50" />
                      </div>
                  </fieldset>

                  <div class="buttonrow">
                      <input type="submit" value="Submit This Form via AJAX" class="button" />  
                      <input type="button" value="Start Again" class="button" />
                      <a href="ajaxformval.html">Refresh this Page</a>
                  </div>
              </form>
          </div> 
      <h3>JSON Array</h3>
      <pre id='debug'></pre>

我使用ajax将表单信息发送到数组中,如下所示:

      $.post(
            'ajaxformval_post.php', 
            variableToSend,
            function(data){
                alert(data['post']['uname']);
                $("#debug").html(JSON.stringify(data, null, 4))
                },
            "json"
          );

并且在php部分中,我正在打印传递的表单值,但是如果一个数组中发生错误,我还想打印错误消息。例如:     { "post":{ "uname":"anyNameUserEnters", "email":"Any Email", "lname":"A last name", "fname":"first name"}, "postVarErrorMsgs":{ "uname":"Please enter a username", "email":"required", "lname":"reqiured", "fname":"required" } 谁能帮帮我吗?     

if(!isset($_POST['uname']) || strlen(trim($_POST['uname'])) == 0){
    $json['postVarErrorMsg']['uname']=='Please enter your name.';
}
if(!isset($_POST['email']) || strlen(trim($_POST['email'])) == 0){
    $json['postVarErrorMsg']['email']=='An email address is required.';
}
if(!isset($_POST['lname']) || strlen(trim($_POST['lname'])) == 0){
    $json['postVarErrorMsg']['lname']=='Your last name must be entered too.';
}
if(!isset($_POST['fname']) || strlen(trim($_POST['fname'])) == 0){
    $json['postVarErrorMsg']['fname']=='We very much require your first name.';
}



$json = array(
        'post' =>array(
            'uname' =>$_POST['uname'],
            'email' =>$_POST['email'],
            'lname' =>$_POST['lname'],
            'fname' =>$_POST['fname']
            ),

        //print existing error messages

);
echo json_encode($json);
?>

1 个答案:

答案 0 :(得分:2)

在这种情况下,执行此操作时将覆盖整个数组。因为您重新声明了$json数组。

$json = array(
    'post' =>array(
        'uname' =>$_POST['uname'],
        'email' =>$_POST['email'],
        'lname' =>$_POST['lname'],
        'fname' =>$_POST['fname']
        ),

基本上,您需要重新构建将数据插入阵列的方式。

$json['post'] = array(
        'uname' =>$_POST['uname'],
        'email' =>$_POST['email'],
        'lname' =>$_POST['lname'],
        'fname' =>$_POST['fname']
        );

这意味着,您要添加另一个以post作为其键名的顶级键。与postVarErrorMsg相同的级别。

不要做这样的事情:

$json = array(some data);

因为这意味着您要重新声明数组并删除存储在其中的所有先前数据。

更简单的实现是使用array_merge函数:

$post_data = array(some data);
$error_data = array(some error data);
$json = array_merge($post_data, $error_data);

希望这有帮助!