Reputation: 65
done function not being called in jquery even after data requested
AJAX Post request: ({"name":"name","fname":"testing","ques":"question","sec":"answer"})
Response from php: ({"invalid":"error"})
,
ie the else part is being executed & not satisfying the if condition.
jQuery Code:
$("#askques").on("submit", function(event) {
event.preventDefault();
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
var formdata=JSON.stringify(($('#askques').serializeObject()));
$.ajax({
type: 'POST',
contentType: "Content-Type: application/json",
url: 'ajaxphp/askques.php',
data: formdata,
dataType: 'json',
cache: false
}).done(function(data) {
if ('url' in data) {
$("#asknext").load("quesans/form.htm", function () {
$("#askcon").hide();
});
}
else if('prob' in data)
{
$("#resposne").text("Something went wrong, try again.");
}
else if('invalid' in data)
{
$("#resposne").text("Enter just the First Name, a word with letters only !");
}
});
});
PHP Code:
$name = $_POST["name"];
$fname = $_POST["fname"];
$ques = $_POST["sec"];
$ans = $_POST["ans"];
$time=date("y-m-d H:i:s");
if (ctype_alpha($name) && ctype_alpha($fname) && (strlen($ques)<=512) && (strlen($ans<=1024)) && (strlen($ques)>3) && (strlen($ans)>3) )
{
// not executed
}
else
{
$data = array('invalid' => "error" );
echo json_encode($data);
}
done function not being called in jquery even after data requested & got response from php file, but the else part is being executed & not satisfying the if condition.
Upvotes: 0
Views: 100
Reputation: 1653
contentType: "Content-Type: application/json",
Remove above line of code, when using this you are not getting the $_POST variables in your php
$.ajax({
type: 'POST',
//contentType: "Content-Type: application/json",
url: 'ajaxphp/askques.php',
data:{"name":"name","fname":"testing","ques":"question","sec":"answer"},
dataType: 'json',
cache: false
}).done(function(data) {
console.log(data);
if ('url' in data) {
$("#asknext").load("quesans/form.htm", function () {
$("#askcon").hide();
});
}
else if('prob' in data)
{ alert("Entered Prob")
$("#resposne").text("Something went wrong, try again.");
}
else if('invalid' in data)
{
alert("Entered Invalid")
$("#resposne").text("Enter just the First Name, a word with letters only !");
}
});
In your php file put below
//print_r($_POST);
$name = $_POST["name"];
$fname = $_POST["fname"];
$ques = $_POST["sec"];
$ans = $_POST["ans"];
$time=date("y-m-d H:i:s");
if (ctype_alpha($name) && ctype_alpha($fname) && (strlen($ques)<=512) && (strlen($ans<=1024)) && (strlen($ques)>3) && (strlen($ans)>3) )
{
$data = array('prob' => "error" );
echo json_encode($data);
}
else
{
$data = array('invalid' => "error" );
echo json_encode($data);
}
After you put the above code try see whether you are getting any alerts.
If it doesnot alert then un comment the first line the php code I gave and check the console response, you will see an array of variables if its posted otherwise youw ill see empty array
Upvotes: 1
Reputation: 6608
Issue is in the way you're checking for properties in data
object. Change your done()
like this and it should work :)
done(function(data) {
if (data.url) {
$("#asknext").load("quesans/form.htm", function () {
$("#askcon").hide();
});
}
else if(data.prob){
$("#resposne").text("Something went wrong, try again.");
}
else if(data.invalid){
$("#resposne").text("Enter just the First Name, a word with letters only !");
}
});
Upvotes: 1