php ajax调用后用PHP重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23807411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:58:06  来源:igfitidea点击:

Redirect with PHP after ajax call

phpjqueryajaxredirect

提问by user500468

Im doing the following ajax call:

我正在执行以下 ajax 调用:

$('#save_sale').click(function() {
    var save_sale = 1;
    $.ajax({
        type: 'GET',
        url: 'summary.php',
        data: {save_sale: save_sale},
        success: function(data) { /* Do something here?? */ },
        error: function(xhr, ajaxOptions, thrownerror) { }
    });
});

Here is my PHP:

这是我的PHP:

function createSale()
    {

        if($sale_id = $this->link->inQuery("INSERT INTO nya_forsaljningar(personnr, status, datum) VALUES('".$this->personnr."','".$this->status."','".$this->sale_date."')"))
        {
            $this->link->inQuery("UPDATE services_temp SET active=1 WHERE temppdtls='".$this->personnr."'");
            $this->link->inQuery("UPDATE summary_temp SET active=1 WHERE personnr='".$this->personnr."'");

            header("Location: addcust.php?new_sale=$sale_id");
            exit;
        }
        else
        {
            return false;   //Kunde inte skapa f?rs?ljningen
        }
    }

if(isset($_GET['save_sale']))
{
    $sale_date = date('Y-m-d');         //Datumet d? man skapar f?rs?ljning
    $personnr = $_SESSION['fil'][3];    //Personnummer p? personen, anv?nder detta f?r att ta fram de olika delarna fr?n tabellerna
    $save_true = $_GET['save_sale'];    //F?rs?krar oss av att vi ska hantera en uppl?ggning av en nyf?rs?ljning

    $new_sale = new newSale($personnr, $sale_date, $save_true, $link, $status='Obehandlad');    //Skapar ett objekt av f?rs?ljningen som vi anv?nder f?r att hantera den nya f?rs?ljning, kolla om den ?r ok, skapar kundbilden, nekar osv.
    if($new_sale->checkService())
    {
        $new_sale->createSale();    //Skapar f?rs?ljningen
    }
    else 
    {
        echo "Kunde inte skapa f?rs?ljningen";
        exit;
    }
}

After the sale is created, I want to redirect to addcust.php?new_sale=$sale_id

销售创建后,我想重定向到 addcust.php?new_sale=$sale_id

How can I accomplish this?

我怎样才能做到这一点?

回答by Styphon

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_idand you'll have your URL.

无论您从 PHP 脚本回显什么,都将在data. 所以回声$sale_id,你就会有你的网址。

回答by MrCode

You can use JavaScript to redirect in the success handler:

您可以使用 JavaScript 在成功处理程序中重定向:

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.

它不能通过 PHP 重定向来完成,因为这只会重定向 ajax 调用,而不是原始浏览器窗口。

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

如果您想在 URL 中使用销售 ID,则需要输出它以便可以访问:

$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) { }
});

回答by Sougata Bose

on your js page

在你的 js 页面上

 $.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

在您的 php 脚本上回显 id

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

hope it will work.

希望它会起作用。

回答by Fredrik

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

$sale_id如果销售成功,则从您的 PHP 文件中返回,通过echo(假设 $new_sale->id 将返回一个 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.

哦,还有拳头是瑞典人。

回答by jme11

If you want to do a full redirect, you can use window.location = 'addcust.php?new_sale='+youridvariableIn the success callback.

如果想做全重定向,可以在成功回调中使用 window.location = 'addcust.php?new_sale='+ youridvariable