How can I get a PHP variable to AJAX?

后端 未结 2 492
轮回少年
轮回少年 2021-01-15 09:17

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 =

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-15 10:16

    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;
    };
    

提交回复
热议问题