简体   繁体   中英

AJAX Success function never true

I don't know why my success function isn't working. I mean although it passes the JSON data to the PHP file and changes the password.

// this is the id of the form
$("#password_form").submit(function(e) {

    e.preventDefault();
    $(".verify-user-loader").addClass("force-display-block");
    password = $("input#password-reset").val();
    url = 'reset-pass.php';

    $.ajax({

        type : 'POST',
        url : url,
        dataType : 'json',
        data : {
                cardnumber: <?php echo '\''.$cardnumber.'\''; ?>,
                act_token: <?php echo '\''.$activationToken.'\''; ?>,
                password: password
        },
        success : function(success){
            alert("success");
        },
        error : function(request, error) {
            console.log(arguments);
            alert(" Can't do because: " + error);
        }

    });

});

This file works as expected, it changes the password using the POST data

reset-pass.php

include_once '/../login/user.class.php';


$activationToken = $_POST['act_token'];
$cardnumber = $_POST['cardnumber'];
$password = $_POST['password'];

$user = new User();

$verifyToken = $user->verifyToken( $activationToken, $cardnumber );

if ($verifyToken['status'] === true) {

    $tokenStatus = "inactive";
    $user->signUp( $cardnumber, $password );
    $user->changeTokenStatus( $cardnumber, $tokenStatus );

    $success = true;

    return $success;

}else{
    print_r($verifyToken);
}

1 ) you need to echo $success; instead of return $success;

Ajax response should be any browser out put (ie like html or echo or print_r these are browser output ) . instead of return .

2) simple add data like this

........

data : {
           cardnumber: '<?php echo $cardnumber; ?>',
           act_token: '<?php echo $activationToken; ?>',
           password: password
        },

........

You should use echo instead of return , because when you work without function you should need to use echo .

So the code will be

 echo true; // or you can write  echo $success = true;
 exit(); // exit is use to stop further processing of code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM