Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 click on update, it does nothing. Below is my php code

PHP code

<?php start_session(); ?>


<?php 
if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") {
    $item_to_adjust = $_POST['item_to_adjust'];
    $quantity = $_POST['quantity'];
    $quantity = preg_replace('#[^0-9]#i', '', $quantity);
    if ($quantity >= 100) { $quantity = 99; }
    if ($quantity < 1) { $quantity = 1; }
    if ($quantity == "") { $quantity = 1; }
    $i = 0;
    foreach ($_SESSION["cart_array"] as $array_key=>$each_item) { 
              if ($each_item['item_id'] == $pid  && $each_item['item_to_adjust'] == $item_to_adjust && $each_item['length'] == $length && $each_item['Category'] == $Category) {
                      $_SESSION["cart_array"][$array_key]['quantity']=$quantity;
                      $wasFound = true;
}
}


}
?>

FORM

echo '<form action="cart.php" method="post">
<input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" />
<input name="adjustBtn' . $item_id . '" type="submit" value="Update" />
<input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
</form>';
?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
274 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...