I have this input field.
<input type="email" name="email" required/>
When submit button is clicked, it goes into :
$(".store_email").click(function()
{
var ajax_url_store_email = "store_email.php";
var value= document.getElementsByName('email')[0].value;
$.post(ajax_url_store_email, {act:"Subscribe", value:value}, function(e){
console.log(e);
});
});
And the call is sent to:
$email=$_POST["value"];
$verify= $link->query(" INSERT INTO `Subscribers_email`(`email`) VALUES ('$email') ");
if($verify==true)
{
return json_encode( "Success!");
}
else
{
return json_encode("Something went wrong!");
}
Everything is working fine, But this call is not returning anything. I mean the console.log(e);
prints nothing, what could be the reason?
plus other answers you must set json dataType in $.post function if you want to return values as json :
$.post(ajax_url_store_email, {act:"Subscribe", value:value}, function(e){
console.log(e);
},'json');
To return a response to the ajax request, you must echo
not return
:
if($verify==true){
echo json_encode( "Success!");
}
else{
echo json_encode("Something went wrong!");
}
try echo and exit instead of return
$verify= $link->query(" INSERT INTO `Subscribers_email`(`email`) VALUES ('$email') ");
if($verify==true)
{
echo json_encode( "Success!");
}
else
{
echo json_encode("Something went wrong!");
}
exit;