user1053263
user1053263

Reputation: 742

JQuery to PHP with JSON

The JQuery gets the data and send it to the PHP like this:

$(document).ready(function(){

        var email = encodeURIComponent($('#email').val());   
        var act = encodeURIComponent($('#act').val());   
        $('#loadingB').fadeIn(); 
        $.ajax({ 
            type: 'POST', url: 'activate.php',  dataType: "json",  data: { email: email, act: act,  }, 
            success: function(result) {                 
                if (!result.success) { timeout = setTimeout(function(){ $('#loadingB').fadeOut(); }, 1500); $('#fail').fadeIn(); }  
                else { timeout = setTimeout(function(){ $('#loadingB').fadeOut(); }, 1500); $('#success').fadeIn(); } 
            }  
        }); 
        return false; 
    }); 

The PHP does this:

$email = htmlspecialchars(trim(urldecode($_POST['email']))); 
$act = htmlspecialchars(trim(urldecode($_POST['act'])));
$first = mysql_query("UPDATE members SET active = '1' WHERE active_code = '$act' AND email = '$email' ");
$second = mysql_query("UPDATE member_search SET active = '1' WHERE email = '$email' ");

if(is_bool($first) == true && is_bool($second) == true)
{
$response = array(success => true); 
echo json_encode($response); 
}
else
{
$response = array(success => false); 
echo json_encode($response); 
}

the "loadingB" div fades in but never fades out to return successful or failure. I believe this is a PHP error. I don't think I'm correctly obtaining if the mysql-query returned true or false.

I know the proper data is being collect by the JQuery because I even echoed it just to make sure, the PHP is just not doing anything with it.

Upvotes: 0

Views: 258

Answers (2)

Jaspreet Chahal
Jaspreet Chahal

Reputation: 2750

one thing you can try is to change this

 $response = array(success => true); 

to

$response = array("success" => true); 

I would also use this

$first = mysql_query("UPDATE members SET active = '1' WHERE active_code = '$act' AND email = '$email' ");
$first_affected_rows = mysql_affected_rows();
$second = mysql_query("UPDATE member_search SET active = '1' WHERE email = '$email' ");
$second_affected_rows = mysql_affected_rows();

if($first_affected_rows > 0 && $second_affected_rows > 0)
{
 $response = array("success" => true); 
 echo json_encode($response); 
}
else 
{
 $response = array("success" => false); 
 echo json_encode($response);  
}

Depending on what you want to achieve above can be changed to suit your logic

Upvotes: 2

John V.
John V.

Reputation: 4670

First of all:

if(is_bool($first) == true && is_bool($second) == true)

is equivlent but longer than:

if(is_bool($first) && is_bool($second))

Second:

$response = array(success => false);
//and
$response = array(success => false);

Should probably be:

$response = array("success" => false);
//and
$response = array("success" => false);

Thirdly (and finally) I think your logic may be off, are you sure you want to send success = true if both are booleans and not both being true?

Upvotes: 1

Related Questions