2

I don't think I am passing the variable the right way between my separate PHP and AJAX files.

I am debugging this by triggering the second condition $status = 'info'; in my PHP file.

Currently, status is coming up as "undefined" for alert(data.status);

signup_process.php

if (condition){

   $status = 'success';

else {

    $status = 'info';

    }

AJAX

function send() {
var data = $('#signup_form').serialize();
    $.ajax({
        type: "POST",
        url: "signup_process.php",
        data: data,
        success: function (data) {
        alert(data.status);
            if (data.status == 'success') {
                // everything went alright, submit
                $('#signup_form').submit();
            } else if (data.status == 'info')
            {
                console.log(data.status);
                $("label#email_error").show(); 
                return false; 
            }
        }
    });
    return false;
};

I know that the 2nd condition is being triggered because I put a header redirect there just for testing and it worked fine.

frankie
  • 661
  • 2
  • 10
  • 25

2 Answers2

7

Good to use json while return back data from php to ajax.

$return_data = array();
if (condition){
   $return_data['status'] = 'success';
} else {
    $return_data['status'] = 'info';
}

echo json_encode($return_data);
exit();

Now, if you are return back json data to ajax, then you need to specify return data type into ajax call as below

function send() {
var data = $('#signup_form').serialize();
    $.ajax({
        type: "POST",
        url: "signup_process.php",
        data: data,
        dataType: 'json', 
        success: function (data) {
        alert(data.status);
            if (data.status == 'success') {
                // everything went alright, submit
                $('#signup_form').submit();
            } else if (data.status == 'info')
            {
                console.log(data.status);
                $("label#email_error").show(); 
                return false; 
            }
        }
    });
    return false;
};
GBD
  • 15,847
  • 2
  • 46
  • 50
  • I added the datatype and the variable is still coming up as "undefined". I just tested to make sure the set "info" conditional was triggering and it is. In this case, shouldn't `(data.status)` be `(return_data.status)`? Thanks. – frankie Oct 06 '12 at 18:59
  • no. it should be data.status only. return_data.status this is wrong – GBD Oct 06 '12 at 19:02
  • 1
    `datatype: 'json'` should be `dataType: 'json'`as it's case sensitive – Bogdan Oct 06 '12 at 19:08
  • @spider, yes you are right. i missed it.. thanks.. edite my answer – GBD Oct 06 '12 at 19:10
3

You should send a JSON object back from php:

$data = array();
if (condition){
   $data['status'] = 'success';
else {
   $data['status'] = 'info';
}

header('Content-type: application/json');
echo json_encode($data);

The json_encode() method converts the array to a JSON object so you can access each array key by name on the js side.

Bogdan
  • 43,166
  • 12
  • 128
  • 129
  • Okay, but I'm unsure why I need an array if it's only one value at a time per the conditional. Or is it sending as a key/value pair? I just tried this with all the code on the 2nd conditional's block `$data = array(); //send variables back to AJAX $data['status'] = 'info'; echo json_encode($data); }` Should the AJAX portion work as I have written it? – frankie Oct 06 '12 at 18:45
  • you need to change datatype:'json' into ajax call to get json data in return – GBD Oct 06 '12 at 18:54
  • I've modified the answer to include the correct JSON `Content-Type `header, because jQuery Ajax guesses the response data type based on that, if you don't specify the `dataType` parameter in your ajax parameters (which you haven't specified). – Bogdan Oct 06 '12 at 18:58
  • @user1390395 As an alternative you can specify the `dataType: 'json'` parameter like @GBD pointed out but it must be camel case with a capital `T`. All lowercase won't work as the parameter name is case-sensitive – Bogdan Oct 06 '12 at 19:05
  • @spider, yes you are right. i missed camel case.. thanks.. edited my answer – GBD Oct 06 '12 at 19:09
  • 2
    @user1390395 - yes, JSON is a key-value pair system, which is why an array is used.... e.g. array('hello'=>'mum') is encoded to the JSON string {"hello":"mum"}. jQuery would then decode this automatically for you in to a javascript object that has the property '.hello' with a value of 'mum' – Matt Oct 06 '12 at 19:35