Redirect with PHP after ajax call

后端 未结 5 467
庸人自扰
庸人自扰 2020-12-06 07:11

Im doing the following ajax call:

$(\'#save_sale\').click(function() {
    var save_sale = 1;
    $.ajax({
        type: \'GET\',
        url: \'summary.php\         


        
5条回答
  •  没有蜡笔的小新
    2020-12-06 08:15

    You can use JavaScript to redirect in the success handler:

    success: function(data) { 
        window.location = 'newpage.php';
    },
    

    It can't be done with a PHP redirect, because that will only redirect the ajax call, not the original browser window.

    If you want to use the sale ID in the URL then you will need to output it so it can be accessed:

    $saleId = $new_sale->id; // or however you get the sale ID
    echo json_encode(array('saleId' => $saleId)); // output JSON containing the sale ID
    

    Ajax:

    $.ajax({
        type: 'GET',
        url: 'summary.php',
        dataType : 'json', // tell jQuery to parse the response JSON
        data: {save_sale: save_sale},
        success: function(data) {
            window.location = 'addcust.php?new_sale=' + encodeURIComponent(data.saleId);
        },
        error: function(xhr, ajaxOptions, thrownerror) { }
    });
    

提交回复
热议问题