0
votes

I have an include in my index which is a pager and list products. In an upper div to this include is the shopping cart. When I send the request data ajax to add the product the pager return the number of page,number of products and SID,but do not know how to retrieve the session to display products in the shop cart. I started the session at the beginning of the page.

Ajax code:

function agrega(codi,nropagina,num){

 divContenido = document.getElementById('contenido');

 ajax=objetoAjax();

 ajax.open("GET", "agregacar.php?CODI="+codi+"&pag="+nropagina+"&numero="+num);
 divContenido.innerHTML= '<img src="loading.gif">';
 ajax.onreadystatechange=function() {
 if (ajax.readyState==4) {  
 divContenido.innerHTML = ajax.responseText
 }
}
 ajax.send(null)
 }

PHP response:

 header("Location:paginador.php?".SID."&pag=".$PagAct."&numero=".$RegistrosAMostrar);
2

2 Answers

0
votes

Session is on the server side. You can't access it through javascript. All you have access to on the client side is the session id (which is no use unless you want to switch from one session to another or something weird like that)

You may want to retrieve your data through another ajax call to a PHP script that'll publish the content of the session.

0
votes

You could always just start the session on the agregacar.php and be sure to do a <?php session_start(); ?> on the index or what ever page you are loading it on.

So just send agregacar.php all the info it needs with a POST. Create some script that will start the session. If everything goes right you would have to do is refresh the page on the cart page to get the session to kick in. You could do that with a simple success function $('#someDiv').load('yourSessionScript.php');

EDIT: Just a quick example of the ajax I would use

$.ajax({
        type: 'post',
        url: 'agregacar.php?CODI="+codi+"&pag="+nropagina+"&numero="+num',
        success: function () {
            $('#someDiv').load('agregacar.php?CODI="+codi+"&pag="+nropagina+"&numero="+num');
        }
      });

Then for the #someDiv

<div id="someDiv">
  <?php //your cart script here ?>
</div>