Project V1
Project V1

Reputation: 357

JSON ajax and jquery, cannot get to work?

I have the following script in my javascript...

$.ajax({
    type: 'POST',
    url: 'http://www.example.com/ajax',
    data: {email: val},
    success: function(response) {   
             alert(response);
    }
});

And my php file looks like this...

    if ($_REQUEST['email']) {

$q = $dbc -> prepare("SELECT email FROM accounts WHERE email = ?");
$q -> execute(array($_REQUEST['email']));
if (!$q -> rowCount()) {

    echo json_encode(error = false);
}
else {

    echo json_encode(error = true);
}

    }

I cannot get either the variable error of true or false out of the ajax call?

Does it matter how I put the data into the ajax call?

At the minute it is as above, where email is the name of the request, and val is a javascript variable of user input in a form.

Upvotes: -1

Views: 270

Answers (2)

Shamrocker
Shamrocker

Reputation: 121

In your code, the return parameter is json

$.ajax({
    type: 'POST',
    url: 'http://www.example.com/ajax',
    dataType: 'json',
    data: {email: val},
    success: function(response) {   
             alert(response);
    }
});

PHP FILES

if ($_REQUEST['email']) {

   $q = $dbc -> prepare("SELECT email FROM accounts WHERE email = ?");
   $q -> execute(array($_REQUEST['email']));
   if (!$q -> rowCount()) {
       echo json_encode(error = false);
       return json_encode(error = false);
   } else {

       echo json_encode(error = true);
       return json_encode(error = true);
    }
 }

Upvotes: 1

aziz punjani
aziz punjani

Reputation: 25776

Try this instead. Your current code should give you a syntax error.

if (!$q -> rowCount()) {

    echo json_encode(array('error' => false));
}
else {

    echo json_encode(array( 'error' => true ))
}

Upvotes: 2

Related Questions