Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers

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 {
    ...
}
share|improve this answer
 
thanks for your reply, so i guess it's not possible due to the (socket closing). Couse even with the way u suggested i could not pass the 'name_available' response first then the 'registration_complete' after completion? –  Bob Machine Aug 18 '13 at 21:25
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.