php - Variable empty without reason -
i developing shopping cart system , working perfectly, except 1 thing.
well, use php session store data each product, including id, name, price , quantity. variables correctly completed, unless price variable. not know why! use jquery fetch values , send them page process them in session.
i put relevant parts of code facilitate. content of products.php:
<?php session_start(); ?> <html> <body> <?php $connection = new mysqli(db_host, db_user, db_pass, db_name); $sql = "select id, name, price products order id"; $result = $connection->query($sql); if ($result->num_rows > 0) { echo "<table>"; echo " <tr>"; echo " <th>id</th>"; echo " <th>name</th>"; echo " <th>price</th>"; echo " <th>quantity</th>"; echo " </tr>"; while($row = $result->fetch_assoc()) { echo " <tr>"; echo " <td><div class='id-product'>". $row["id"]."</div></td>"; echo " <td><div class='name-product'>". $row["name"]."</div></td>"; echo " <td>$<div class='price-product'>". $row["price"]."</div></td>"; echo " <form class='add'>"; echo " <td><input type='number' name='quantity' value='1' min='1'/></td>"; echo " <td><button type='submit'>add</button></td>"; echo " </form>"; echo " </tr>"; } echo "</table>"; } $connection->close(); ?> <script> $(document).ready(function() { $('.add').on('submit', function() { var id = $(this).closest('tr').find('.id-product').text(); var name = $(this).closest('tr').find('.name-product').text(); var price = $(this).closest('tr').find('.price-product').val(); var quantity = $(this).closest('tr').find('input').val(); window.location.href = "add.php?id=" + id + "&name=" + name + "&price=" + price + "&quantity=" + quantity; return false; }); }); </script> <body> </html>
now in add.php have following:
<?php session_start(); $id = isset($_get['id']) ? $_get['id'] : ""; $name = isset($_get['name']) ? $_get['name'] : ""; $price = isset($_get['price']) ? $_get['price'] : ""; $quantity = isset($_get['quantity']) ? $_get['quantity'] : ""; $cart_item = array ( 'id' => $id, 'name' => $name, 'price' => $price, 'quantity' => $quantity ); if(!isset($_session['cart'])) { $_session['cart'] = array(); } if(array_key_exists($id, $_session['cart'])) { header('location: products.php?action=exists&id=' . $id . '&name=' . $name); } else if($quantity <= 0) { header('location: products.php?action=invalidquantity&id=' . $id . '&name=' . $name); } else { $_session['cart'][$id] = $cart_item; header('location: products.php?action=added&id=' . $id . '&name=' . $name); } ?>
to show products exists in session, use foreach in page cart.php:
$total = 0; foreach($_session['cart'] $id) { echo "<tr>"; echo "<td>{$id['name']}</td>"; echo "<td>${$id['price']}</td>"; echo "<td>{$id['quantity']}</td>"; echo "<td>"; echo "<a href='remove.php?id={$id['id']}&name={$id['name']}'>remove</a>"; echo "</td>"; echo "</tr>"; $total += ($id['price'] * $id['quantity']); }
as said earlier, have returned values of id, name , quantity, price remains empty. what's wrong?
you're not setting price right in js believe.
window.location.href = "add.php?id=" + id + "&name=" + name + "&price" + price + "&quantity=" + quantity; return false;
should
window.location.href = "add.php?id=" + id + "&name=" + name + "&price=" + price + "&quantity=" + quantity; return false;
Comments
Post a Comment