Reputation: 141
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
Upvotes: 1
Views: 172
Reputation: 141
The only way i was able to get this to work was to send a second request
code sample
//first request
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"
*** here after getting the first response, i send a 2nd request,and place the handlers only 'sharing' the error msgs
confirmation(); //second request
}
else if(data == 'registrationComplete'){
//user feedback, "registration is complete thank you"
{
else if(data == 'nameInUse'){
//user feedback, "name is in use, please select another…"
}
I hope it helps someone
Upvotes: 0
Reputation: 1372
You cannot process data in steps (not in this way at least) as JQuery will fire you function only upon your php script finishing working (socket closing).
Use json
$res - array('name_available' => false, 'registration_complete' => false);
... some code ...
if (...) {
...
} else {
$res['name_available'] = true;
...
if (...) {
$res['registration_complete'] = true;
}
}
...
echo json_encode($res);
Then in nameVerify
data = $.parseJSON(data);
if (data['name_available']) {
...
if (data['registration_complete']) {
...
}
} else {
...
}
Upvotes: 1