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);
}
});
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;
}
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>
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
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.
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);
}
});