dongteng2534
2014-06-23 12:26
浏览 175
已采纳

如何从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);     
       }        
    });

图片转代码服务由CSDN问答提供 功能建议

我的问题如下,我必须在3个案例中显示错误消息 1-某些文本丢失\ n2-电话号码应该是所有数字 3-电子邮件格式无效 $ errmsg是一个JSON对象,应该在ajax中解析后显示该消息。但我不知道如何在ajax中解析它并返回 值。 我的代码:

  $ errmsg ='{“invalid”:“some some missing”,
“mailerr”:“电子邮件格式不正确”  ,
“telephoneerr”:“电话应该是所有数字”}'; 
如果(($ name ==“”)||($ email ==“”)||($ telephone ==“”)|  |($ username ==“”)||($ password ==“”))
 {
 echo $ errmsg; 
} elseif(!preg_match($ pattern_email,$ email)){
 echo $ errmsg  ; 
} 
elseif(!preg_match($ pattern_phone,$ telephone)){
 echo $ errmsg; 
} 
   
 
 

,ajax代码为 如下:

  $ .ajax({
 type:'POST',
 url:'contactData.php',
 // dataType:“JSON”,\  n data {“name”:name,“telepho  ne“:电话”,电子邮件“:电子邮件,”用户名“:用户名,”密码“:密码},
成功:函数(数据){
 $(”#validate“)。html(data);  
} 
}); 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • 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 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 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 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);
    
    
    
       }        
    });
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题