I am trying to adjust the quantity of an item when a user enters the quantity and click on Update but I am having a problem getting it to work. When you end the quantity and
you do not have to loop through $_SESSION["cart_array"]
if you already know the id.
this will give you are rough idea of what to do. it is easy to understand and change to your needs!
<?php session_start();
$itemid=(int)$_POST['itemid'];
$quantity=(int)$_POST['quantity'];
$size=(int)$_POST['size'];
$i = md5("$itemid # $size"); // id with size for cart array id
if (isset($_SESSION["cart_array"][$i])) {
$_SESSION["cart_array"][$i]['quantity']+=$quantity;
}
else
{
$_SESSION["cart_array"][$i] = array('id'=>$itemid,'size'=>$size,'quantity'=>$quantity);
}
print_r($_SESSION["cart_array"]);
?>
<p>item 1 - s= <?php echo($_SESSION["cart_array"][md5("1 # 1")]['quantity']); // debug purposes ?>
<form action="" method="post">
<input name="itemid" type="hidden" value="1" />
<input name="quantity" type="text" value="" size="1" maxlength="2" />
<select name="size">
<option value="1">s</option>
<option value="2">m</option>
<option value="3">l</option>
</select>
<input name="adjustBtn" type="submit" value="Add" />
</form></p>
<p>item 2
<form action="" method="post">
<input name="itemid" type="hidden" value="2" />
<input name="quantity" type="text" value="" size="1" maxlength="2" />
<select name="size">
<option value="1">s</option>
<option value="2">m</option>
<option value="3">l</option>
</select>
<input name="adjustBtn" type="submit" value="Add" />
</form>
</p>
you can learn alot from here programmers bible
i don't see any
session_start();
In your PHP code, if you want to work with the session of the user, you must include session_start();
in the beginning of your php file.
That includes ajax files.
If you make an ajax call from a php page that has session_start, that DOES NOT MEAN that the ajax script will be "in session". You MUST include session_start();
in the ajax script.