Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

jquery - Redirect with PHP after ajax call

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...