Sunday, 18 August 2013

ajax request with php responses in sequence, not in array

ajax request with php responses in sequence, not in array

Hi guys here is what I'm trying to do, this is a registration form and
first I'm doing a check to see if the username is available, if it is it
them proceeds with the registration. In case the username is available i
wanted to give the user feedback, that the name is available and it's
proceeded with registration, the issue is, i do the ajax request but the
responses come back together not one and then the other, is there a way i
could do it for responses to come one and then the other, below is the
code: *Note: both php and js files are external
JS file
$.ajax({
url: "register.php",
type: "POST",
data: ({username: nameToCheck, password: userPass, email: userEmail}),
async: true,
beforeSend: ajaxStartName,
success: nameVerify,
error: ajaxErrorName,
complete: function() {
//do something
}
});
function nameVerify(data){
console.log('ajax data: '+data); // this gives both responses, not one
then the other
if(data == 'nameAvailable'){
//user feedback, "name is available, proceeding with registration"
}
else if(data == 'registrationComplete'){
//user feedback, "registration is complete thank you"
{
else if(data == 'nameInUse'){
//user feedback, "name is in use, please select another…"
}
}
php file
<?php
// the connection and db selection here
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$total = $row[0];
if($total == 1){
echo 'nameInUse';
disconnectDb();
die();
}
else{
echo 'nameAvailable';
register(); //proceed with registration here
}
function register(){
$password= $_POST['password'];//and all other details would be gathered
here and sent
//registration happens here, then if successful
//i placed a for loop here, to simulate the time it would take for reg
process, to no avail
//the echoes always go at same time, is there a way around this?
echo 'registrationComplete';
}
?>
I found a few questions that where similar to mine, but not exactly and
could not find a definite answer, so I'm posting my question, thanks

No comments:

Post a Comment