dqssst0144 2014-12-29 00:38
浏览 56
已采纳

使用返回数据从Ajax + Redirect过滤返回的数据

I have a validation that, when successful creates a session variable:

if( password_verify($password, $artist_password) ) { // if this is true
            // create $_SESSION artist name
            $_SESSION['artist_name'] = $artist_name;
            return $artist_name;
        } else {
            echo "Incorrect Password";
        }

This validation is initiated by an Ajax request.

If there are errors with the validation, I echo a message. This echoed message is received by the Ajax success method via the data argument.

$.ajax({
    url: '../includes/login_validation.php',
    type: 'post',
    data: inputs,
    success: function(data){ 
        $("span#error_msg").html(data);
    }, 
    // returns 'Incorrect Password'

My problem is in defining the return response from the PHP file.

For example, when there's an error, the PHP script echoes a message and in turn this response is received and processed through the Ajax success method and placed into a html placeholder.

However, if the SESSION variable is assigned I want to return that data and include it in a redirect.

My question is, how can I discern between an echoed response as the return value and a returned value containing other data (e.g $_SESSION['artist_name']).

And if successful, how can I include it in my redirect so that the redirected page will have access to the session variable.

window.location.href="artistWorkshop.php"; // plus session variable
  • 写回答

1条回答 默认 最新

  • dsw1608 2014-12-29 02:15
    关注

    How about doing a little JSON formatting:

    if( password_verify($password, $artist_password) ) { // if this is true
        // create $_SESSION artist name
        $_SESSION['artist_name'] = $artist_name;
        echo '{"status":"success","result":"'.$artist_name.'"}';
    } else {
        echo '{"status":"error","result":"Incorrect Password"}';
    }
    

    Next, handle the output in JS:

    $.ajax({
        url: '../includes/login_validation.php',
        type: 'post',
        data: inputs,
        success: function(data){ 
            var obj = JSON.parse(data);
            if (obj.status == 'success') {
                // redirect
                window.location.href="artistWorkshop.php?artist="+obj.result;
            }
            if (obj.status == 'error') {
                // throe error
                $("span#error_msg").html(obj.result);
            }
        },
    }); 
    

    You could handle multiple formats this way. Problem was is the JS was interpreting any output as a success, because the AJAX call itself was successful. Now, you have a message to determine the actual result of the request. In your code, to determine an actual failure of the ajax request, your would need to include a error node:

    $.ajax({
        url: '../includes/login_validation.php',
        type: 'post',
        data: inputs,
        success: function(data){ 
            $("span#error_msg").html(data);
        },
        error: function(data){
            // handle the error
        }, 
    

    If you go that route, check this out:

    http://api.jquery.com/jquery.ajax/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程