javascript - How to retrieve an error message in the php using json? -
javascript - How to retrieve an error message in the php using json? -
i have next php code:
if (($name == "") || ($email == "") || ($telephone == "") || ($username == "") || ($password == "")) { $ret = array(); $ret["msg"] = "some input missing"; echo json_encode($ret); } elseif (!preg_match($pattern_email, $email)) { $ret = array(); $ret["msg"] = "email format incorrect"; echo json_encode($ret); } elseif (!preg_match($pattern_phone, $telephone)){ $ret = array(); $ret["msg"] = "telephone should digits"; echo json_encode($ret); }
and next javascript:
$(document).ready(function(){ $("#save").click(function(){ var name = $('#name').val(); var email = $('#email').val(); var telephone = $('#telephone').val(); var username = $('#username').val(); var password = $('#password').val(); $.ajax({ type:'post', url: 'contactdata.php', datatype: "json", data:{"name":name,"telephone":telephone,"email":email, "username":username, "password":password}, success: function(data) { //alert(data); var result = json.parse(data); $("#validate").html(data.msg); } }); }); });
the aim of programme output error message
using json whenever 1 of conditions true. i'm not retrieving info , programme shows error @ statement var result = json.parse(data);
can help plz???
first in php instead of
$errormsg = '{"msg":"some input missing"}';
use
$ret = array(); $ret["msg"] = "some input missing"; echo json_encode($ret);
in javascript
add in ajax request
datatype: "json",
your parse work now
javascript php json
Comments
Post a Comment