php - How to update session array via ajax? -


i'm passing user input, quantity , product code in case php script store session array via ajax. expect, each time user clicks submit , ajax call made, quantity , product code added session array. happen is, each time updates existing data in session. 1 pair of data exist.

this ajax script:

<script>       <!--display form submission result-->       var param;       var param2;       var i;       function sendtocart(param, param2){     $("document").ready(function(){           $("#popup-order").submit(function(){                         var data = {               "action": "test"             };             data = $(this).serialize() + "&" + $.param(data);             $.ajax({               type: "post",               datatype: "json",               url: "addtocart.php?id="+param, //relative or absolute path response.php file               data: data,               success: function(data) {                   console.log(data);               }             });             return false;           });         });        }     </script> 

my php sendtocart.php

   <?php     session_start();      $return = $_post;       $return['json']= json_encode($return);      $data = json_decode($return['json'], true);       $_session['cart']=array();     array_push($_session['cart'], array("quantity"=>$data['qty'],"id"=>$data['id']));     echo json_encode($cart);       ?> 

you're overwriting session variable every time ajax call made ...

$_session['cart']=array(); 

you need read existing session data , add data.


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -