View Full Version : Need to return a Session Variable after Ajax Request

11-09-2010, 11:22 PM

I have multiple forms on a page that I submit using ajax, it send a product_id so it knows which one to update, with limitations out of my control it regenerates a new product_id for the product just updated.

The problem I have is if a user is updating a product in their basket, it updates each product, but when they make a second alteration to the product without a page refresh which I want to avoid it does not update the product because the product id has changed.

During the update of a product once the new product id is generated it places it into $_SESSION['new_products_id_in_cart'] = $products_id;

What I need to do is once my form is submitted with Ajax, to return that session variable and place the result of this into an input box, ready for the next time the for is updated.

At the moment I'm using Jquery Form to handle Ajax requests, but can change that if needed.

I really hope someone can help, if you don't quite understand please let me know.


11-10-2010, 01:13 PM
And what prevents you from getting that session variable received by the AJAX object in a response?

I presume you simply post data using AJAX. If you will use a GET, you may get also a response from that PHP application, while the submitted data is sent to the DataBase;

In a schematic sequence:

- use GET as method in AJAX wile sending data to a php external code
- the php code should open connection, post data to DB (MySQL code INSERT), get the new $product_id (MySQL code SELECT), close the connection and echoes back that id (to the AJAX object) as responseText

11-10-2010, 03:42 PM
Thanks Kor for the reply.

How would I place the result of the $_GET into a form input box from the response?

11-10-2010, 04:20 PM
I don't work with JQuery (nor with other libraries unless I am forced to - it is not the case with AJAX codes) thus I can not give you a JQuery solve.

But in native JavaScript, there must be something like:

if(AJAXobj.readyState == 4){
if(AJAXobj.status == 200){
var id=AJAX.responseText;
var textBox=document.getElementById('textboxid');

So, if you find how to get that responseText value in JQuery, the rest is simple.