dongye9991 2014-05-22 12:53
浏览 112
已采纳

ajax调用后使用PHP重定向

Im doing the following ajax call:

$('#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:

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

How can I accomplish this?

  • 写回答

5条回答 默认 最新

  • duancongjue9202 2014-05-22 12:56
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dshqd84261 2014-05-22 12:56
    关注

    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) { }
    });
    
    评论
  • douren6874 2014-05-22 12:56
    关注

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

    评论
  • doufei9805 2014-05-22 12:58
    关注

    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.

    评论
  • dpp34603 2014-05-22 12:59
    关注

    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.

    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 如何通过求后验分布求得样本中属于两种物种其中一种的概率?
  • ¥15 q从常量变成sin函数,怎么改写python代码?
  • ¥15 图论编程问题,有可以指导的吗
  • ¥15 DEA的CCR模型画图
  • ¥15 请假各位一个关于安卓车机的问题
  • ¥15 光谱仪怎么看这样的透射谱
  • ¥15 pyqt5 如何实现输入框输入关键词,下拉框显示模糊查询返回的结果?
  • ¥20 fluent模拟,可以燃烧和相变同时模拟吗?
  • ¥50 海康摄像头,C#如何识别车牌号码和抓取JPG
  • ¥15 yolov5 pt转engine的问题