Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 824
user500468 Avatar asked May 22 '14 12:05

user500468


People also ask

How to redirect to another page after ajax success in php?

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

When to use ajax?

Where Should Ajax be Used? Ajax should be used anywhere in a web application where small amounts of information could be saved or retrieved from the server without posting back the entire pages. A good example of this is data validation on save actions.

How ajax works?

AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page. Classic web pages, (which do not use AJAX) must reload the entire page if the content should change.


3 Answers

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.

like image 155
Styphon Avatar answered Oct 06 '22 23:10

Styphon


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) { }
});
like image 37
MrCode Avatar answered Oct 06 '22 23:10

MrCode


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.

like image 26
Sougata Bose Avatar answered Oct 06 '22 22:10

Sougata Bose