Reputation: 149
I need a way to discern returned values from a PHP processing script to an Ajax return response function so that I can do different things if the response is positive vs negative.
I'm aware of indexOf (which seems to be deprecated) as well as inArray. The problem is my returned data is not an array, they are simple 'echos'.
I also want to avoid encoding the echo statements into JSON. It seems extraneous to do so.
How can I discern the returned data?
if(!mysqli_num_rows($result)){
// Insert data into database...
if($result){
echo "Thanks for Registering!";
$_SESSION['username'] = $userName;
} else {
echo "Something went wrong with your registration. Please try back later.";
}
} else {
echo 'It seems that your email has already been registered.';
// return false;
}
I thought about adding a 'return false' to "mimic" a failed Ajax response, but that doesn't work.
$.ajax({
url:'includes/register.php',
type:'POST',
data: formData,
success: function(data){
$("#register-form #message p").html(data);
}
}); // $.ajax
// if registration was successful, show the message and redirect
$('#register-form img').addClass('hide');
$("#register-form div + div").addClass('hide');
$("#register-form #message").removeClass('hide');
setTimeout(function(){
$('#overlay, form#register-form').fadeOut('slow');
$(".register").off();
window.location.href = 'sandbox/workshop.php';
},2000);
// if registration was not successful, do something else...
Thanks in advance for your replies.
Upvotes: 0
Views: 384
Reputation: 2221
You can return a response array containing a $success
value of either true
or false
and just print the error messages via JS. I don't think there's any other way to do than by using JSON.
Upvotes: 1