Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ajax调用后使用PHP重定向_Php_Jquery_Ajax_Redirect - Fatal编程技术网

ajax调用后使用PHP重定向

ajax调用后使用PHP重定向,php,jquery,ajax,redirect,Php,Jquery,Ajax,Redirect,我正在执行以下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:

我正在执行以下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) { }
    });
});
以下是我的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;
    }
}
创建销售后,我想重定向到addcust.php?new_sale=$sale\u id


如何实现这一点?

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

success: function(data) { 
    window.location = 'newpage.php';
},
这不能用PHP重定向来完成,因为这只会重定向ajax调用,而不会重定向原始的浏览器窗口

如果要在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
阿贾克斯:


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

在成功中重定向:

$('#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) { }
    });
});
从PHP脚本返回的任何内容都将在
数据中。所以echo
$sale\u id
你就可以在你的js页面上找到你的URL了。

 $.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)
    });
在php脚本上,回显id

$data['id'] = <sale_id>;
echo json_encode($data);exit
$data['id']=;
echo json_编码($data);出口

希望它能起作用。

如果销售成功,请通过
echo
(假设$new\u sale->id将返回一个id),从您的PHP文件中返回
$sale\u id

然后在响应数据中检索并将其添加到重定向:

success: function (data) {
    window.open("addcust.php?new_sale="+data, "_top");
},
这是一个使用变量的示例,我不确定是否存在,因为我不知道您的类是如何工作的。然而,逻辑是一样的


哦,作为一个瑞典人,还有拳头碰撞。

替换
/*在这里做点什么*/
with
window.location.href=“NEW\u URL\u HERE.PHP”
变量
data
@Styphon的PHP端如何从类中返回具有正确ID的变量数据?为什么要使用两个成功回调函数?当ajax调用成功编辑以使我的浏览器选项卡闪烁时,它将执行。尽管如此,还是有适当的回应。
if($new_sale->checkService())
{
    $new_sale->createSale();    //Skapar försäljningen
    echo $new_sale->id();
}
success: function (data) {
    window.open("addcust.php?new_sale="+data, "_top");
},