dongteng2534
2014-06-23 12:26 阅读 137

如何从ajax回显json语句

my issue is as following,i have to display an error msg in 3 cases 1- some text is missing 2- telephone number should be all digits 3- email format is invalid the $errmsg is a JSON object that should display that message after being parsed in the ajax.but i don't know how to parse it in the ajax and return the value. here is my code:

$errmsg='{"invalid":"some text missing",
      "mailerr":"email format is incorrect",
      "telephoneerr":"telephone should be all digits"}';
 if(($name=="")||($email=="")||($telephone=="")||($username=="")||($password==""))
{
  echo $errmsg;
}elseif(!preg_match($pattern_email,$email)){
 echo $errmsg;
}
elseif (!preg_match($pattern_phone,$telephone)){
  echo $errmsg;
 }

and the ajax code is as follows:

  $.ajax({
          type:'POST',
          url: 'contactData.php',
          //dataType: "JSON",     
 data{"name":name,"telephone":telephone,"email":email,"username":username,"password":password},
       success: function(data) {            
      $("#validate").html(data);     
       }        
    });
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    dtnbjjq51949 dtnbjjq51949 2014-06-23 12:31

    Try this:

    $errmsg='{"invalid":"some text missing",
             "mailerr":"email format is incorrect",
             "telephoneerr":"telephone should be all digits"}';
    
    if (($name == "") || ($email == "") || ($telephone == "") || ($username == "") || ($password == "") || !preg_match($pattern_email, $email) || !preg_match($pattern_phone, $telephone)){
        return $errmsg;
    }
    

    UPDATE

    If you want to parse the JSON data using jQuery, then can check the below code:

    <?php
    if (($name == "") || ($email == "") || ($telephone == "") || ($username == "") || ($password == "")) {
        $errorMsg = array("error" => "some text missing");
    } elseif (!preg_match($pattern_email, $email)) {
        $errorMsg = array("error" => "email format is incorrect");
    } elseif (!preg_match($pattern_phone, $telephone)){
        $errorMsg = array("error" => "telephone should be all digits");
    }
    
    return json_encode($errorMsg);
    ?>
    <script>
        $.ajax({
            type:'POST',
            url: 'contactData.php',
            dataType: "JSON",     
            data{"name":name, "telephone":telephone, "email":email, "username":username, "password":password},
            success: function(data) {
                data = jQuery.parseJSON(data);
                $("#validate").html(data.error);     
            }        
        });
    </script>
    
    点赞 评论 复制链接分享
  • douxi3977 douxi3977 2014-06-23 12:31

    You can use "dataType:JSON" in ajax(in jQuery) and instead of

    $errmsg='{"invalid":"some text missing",
      "mailerr":"email format is incorrect",
      "telephoneerr":"telephone should be all digits"}';
    

    return an array with json_encode like this in php

    echo json_encode(array("invalid" => "some text missing",
      "mailerr" => "email format is incorrect",
      "telephoneerr" => "telephone should be all digits"));
    

    Now you can access your array values as objects like

    data.invalid OR data.mailerr
    
    点赞 评论 复制链接分享
  • duanhao4156 duanhao4156 2014-06-23 12:33

    It looks like your JSON is badly formed. Try writing it without the outer pair of single quotes, as follows:

    $errmsg = {
        "invalid"      : "some text missing",
        "mailerr"      : "email format is incorrect",
        "telephoneerr" : "telephone should be all digits"
    };
    

    and then calling $errmsg['invalid'], etc to get the required error message for each case.

    点赞 评论 复制链接分享
  • dongtao9095 dongtao9095 2014-06-23 12:41

    I think your output is correct and u need to show that in jquery

      $.ajax({
               type:'POST',
             url: 'contactData.php',
    
             //dataType: "JSON",
        data{"name":name,"telephone":telephone,"email":email,"username":username,"password":password},
       success: function(data) {
    
      var obj = jQuery.parseJSON(data);
    
      $("#validate").html(obj.invalid);
    
    
    
       }        
    });
    
    点赞 评论 复制链接分享

相关推荐