Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

php - Quantity Not adjusting

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...