Redirect with PHP after ajax call

后端 未结 5 454
庸人自扰
庸人自扰 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 07:51

    You redirect in success:

    $('#save_sale').click(function() {
        var save_sale = 1;
        $.ajax({
            type: 'GET',
            url: 'summary.php',
            data: {save_sale: save_sale},
            success: function(data) { 
                    window.location.href = 'addcust.php?new_sale=' + data
                },
            error: function(xhr, ajaxOptions, thrownerror) { }
        });
    });
    

    Whatever you echo back from the PHP script will be in data. So echo $sale_id and you'll have your URL.

    0 讨论(0)
  • 2020-12-06 08:00

    on your js page

     $.ajax({
            type: 'GET',
            url: 'summary.php',
            data: {save_sale: save_sale},
            //success: function(data) { /* Do something here?? */ },
            error: function(xhr, ajaxOptions, thrownerror) { }
        }).success(function(data) {
           window.location('addcust.php?new_sale='+data.id)
        });
    

    on your php script echo the id

    $data['id'] = <sale_id>;
    echo json_encode($data);exit
    

    hope it will work.

    0 讨论(0)
  • 2020-12-06 08:01

    Return the $sale_id from your PHP file if the sale is a success, via echo (assuming $new_sale->id would return an id):

    if($new_sale->checkService())
    {
        $new_sale->createSale();    //Skapar försäljningen
        echo $new_sale->id();
    }
    

    Then retrieve it in your response data and add it to your redirect:

    success: function (data) {
        window.open("addcust.php?new_sale="+data, "_top");
    },
    

    This is an example using variables I'm not sure exists, as I don't know how your class works. The logic stays the same, however.

    Oh, and fist bump for being swedish.

    0 讨论(0)
  • If you want to do a full redirect, you can use window.location = 'addcust.php?new_sale='+youridvariable In the success callback.

    0 讨论(0)
  • 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) { }
    });
    
    0 讨论(0)
提交回复
热议问题