user500468
user500468

Reputation: 1221

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?

Upvotes: 9

Views: 50867

Answers (5)

Sougata Bose
Sougata Bose

Reputation: 31739

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.

Upvotes: 2

jme11
jme11

Reputation: 17374

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

Upvotes: -3

Styphon
Styphon

Reputation: 10447

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.

Upvotes: 16

Fredrik
Fredrik

Reputation: 764

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.

Upvotes: 0

MrCode
MrCode

Reputation: 64526

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

Upvotes: 10

Related Questions