sum_guy Posted July 11, 2009 Share Posted July 11, 2009 Hi, I have used a while loop to loop through my database to display products. I want to place a "buy" button in the loop so when clicked the details would be stored into a session array. I want to use hidden fields but it doesn't work. This is the code I have so far please help. <?php session_start(); /** * @author Grant Kinsman * @copyright 2009 */ $dbhost = 'localhost'; $dbuser = 'root'; $dbpwd = '****'; $conn = mysql_connect($dbhost,$dbuser,$dbpwd) or die ('Error connecting'); $dbname = 'karensKards'; mysql_select_db($dbname); $result = mysql_query("SELECT * FROM birthdays"); if ($_POST['submit']) { $_SESSION['total'] = $_SESSION['total'] + $_POST['submit']; $_SESSION['prod']++; $_SESSION['basket'] = $_POST['submit']; } ?> <html> <head> <title>Mums Cards</title> <link rel="stylesheet" href="style.css" style="text/css" /> </head> <body> <div id="titleBar"> <p class="shopCar"><b>Cart</b><br> Items - <?php echo $_SESSION['prod']; ?><br> Total - £<?php echo $_SESSION['total']; ?> <a href="checkout.php" style="padding-left=0px">Checkout</a></p> </div> <div id="menu"> <a href="index.php">Home</a> <a href="products.php">Products</a> </div> <div id="main"> <div style="overflow:auto;height:630"> <form method="post" action="<?php $_SERVER['PHP_SELF'] ?>"> <?php while ($row = mysql_fetch_array($result)) { echo "<div class='prodInd'><img src='" .$row['imgPath']. ".jpg' style='width:125px;height:125px;margin-right:10px;float:left;cursor:pointer;' onclick='this.style.width=300;this.style.height=300' onmouseout='this.style.width=125;this.style.height=125'><br>"; echo $row['imgPath']. "<br>"; echo "Price:£" .$row['price']. "<br>"; echo "Details:" .$row['details']; echo "<input type='submit' name='submit' value='buy' style='float:right'></div>"; } ?> </form> </div> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/165614-solved-hidden-fields-dont-work/ Share on other sites More sharing options...
ignace Posted July 11, 2009 Share Posted July 11, 2009 if ($_POST['submit']) If the form wasn't submitted yet this will yield a warning, thus should be: if (!empty($_POST)) or if (isset($_POST['submit'])) Where are these hidden input fields? Quote Link to comment https://forums.phpfreaks.com/topic/165614-solved-hidden-fields-dont-work/#findComment-873575 Share on other sites More sharing options...
sum_guy Posted July 11, 2009 Author Share Posted July 11, 2009 They were to be placed above the submit button with the value='" .$row['price']. "' but when the button is clicked it will display 4 which is not the exact price. Quote Link to comment https://forums.phpfreaks.com/topic/165614-solved-hidden-fields-dont-work/#findComment-873582 Share on other sites More sharing options...
ignace Posted July 11, 2009 Share Posted July 11, 2009 Can you please post the code with the hidden fields + involved processing (and use code tags please). Quote Link to comment https://forums.phpfreaks.com/topic/165614-solved-hidden-fields-dont-work/#findComment-873646 Share on other sites More sharing options...
sum_guy Posted July 12, 2009 Author Share Posted July 12, 2009 <?php session_start(); /** * @author Grant Kinsman * @copyright 2009 */ $dbhost = 'localhost'; $dbuser = 'root'; $dbpwd = 'wanker'; $conn = mysql_connect($dbhost,$dbuser,$dbpwd) or die ('Error connecting'); $dbname = 'karensKards'; mysql_select_db($dbname); $result = mysql_query("SELECT * FROM birthdays"); if ($_POST['submit']) { $_SESSION['total'] = $_SESSION['total'] + $_POST['price']; $_SESSION['prod']++; if ($_SESSION['cart'] == "") { $_SESSION['basket'] = array($numba => array( "quantity" => 1, "image" => $_POST['imgPath'], "price" => $_POST['price'], "info" => $_POST['details'])); } else { $_SESSION['basket'][$numba]= array( 'quantity' => 1, "image" => $_POST['imgPath'], "price" => $_POST['price'], "info" => $_POST['details']); } } ?> <html> <head> <title>Mums Cards</title> <link rel="stylesheet" href="style.css" style="text/css" /> </head> <body> <div id="titleBar"> <p class="shopCar"><b>Cart</b><br> Items - <?php echo $_SESSION['prod']; ?><br> Total - £<?php echo $_SESSION['total']; ?> <a href="checkout.php" style="padding-left=0px">Checkout</a></p> </div> <div id="menu"> <a href="index.php">Home</a> <a href="products.php">Products</a> </div> <div id="main"> <div style="overflow:auto;height:630"> <form method="post" action="<?php $_SERVER['PHP_SELF'] ?>"> <?php while ($row = mysql_fetch_array($result)) { echo "<div class='prodInd'><img src='" .$row['imgPath']. ".jpg' style='width:125px;height:125px;margin-right:10px;float:left;cursor:pointer;' onclick='this.style.width=300;this.style.height=300' onmouseout='this.style.width=125;this.style.height=125' alt='" .$row['imgPath']. "'><br>"; echo $row['imgPath']. "<br>"; echo "Price:£" .$row['price']. "<br>"; echo "Details:" .$row['details']; echo "<input type='hidden' name='image' value='" .$row['imgPath']. "'>"; echo "<input type='hidden' name='price' value='" .$row['price']. "'>"; echo "<input type='hidden' name='details' value='" .$row['details']. "'>"; echo "<input type='submit' name='submit' value='buy' style='float:right'></div>"; } ?> </form> </div> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/165614-solved-hidden-fields-dont-work/#findComment-873813 Share on other sites More sharing options...
ignace Posted July 12, 2009 Share Posted July 12, 2009 Check the below code i have added an row_id field which will pinpoint you wish row was selected, clicked and needs update'ing <?php session_start(); /** * @author Grant Kinsman * @copyright 2009 */ $dbhost = 'localhost'; $dbuser = 'root'; $dbpwd = 'wanker'; $conn = mysql_connect($dbhost,$dbuser,$dbpwd) or die ('Error connecting'); $dbname = 'karensKards'; mysql_select_db($dbname); $result = mysql_query('SELECT * FROM birthdays'); if (isset($_POST['submit'])) { $row_id = $_POST['row_id'];//now update this specific row $_SESSION['total'] += $_POST['price']; $_SESSION['prod']++; if (empty($_SESSION['cart'])) { $_SESSION['basket'] = array($numba => array( 'quantity' => 1, 'image' => $_POST['imgPath'], 'price' => $_POST['price'], 'info' => $_POST['details'])); } else { $_SESSION['basket'][$numba]= array( 'quantity' => 1, 'image' => $_POST['imgPath'], 'price' => $_POST['price'], 'info' => $_POST['details']); } } ?> <html> <head> <title>Mums Cards</title> <link rel="stylesheet" href="style.css" style="text/css" /> </head> <body> <div id="titleBar"> <p class="shopCar"><b>Cart</b><br> Items - <?php echo $_SESSION['prod']; ?><br> Total - £<?php echo $_SESSION['total']; ?> <a href="checkout.php" style="padding-left=0px">Checkout</a></p> </div> <div id="menu"> <a href="index.php">Home</a> <a href="products.php">Products</a> </div> <div id="main"> <div style="overflow:auto;height:630"> <form method="post" action="<?php $_SERVER['PHP_SELF'] ?>"> <?php while ($row = mysql_fetch_array($result)) { echo "<div class='prodInd'><img src='" .$row['imgPath']. ".jpg' style='width:125px;height:125px;margin-right:10px;float:left;cursor:pointer;' onclick='this.style.width=300;this.style.height=300' onmouseout='this.style.width=125;this.style.height=125' alt='" .$row['imgPath']. "'><br>"; echo $row['imgPath']. "<br>"; echo "Price:£" .$row['price']. "<br>"; echo "Details:" .$row['details']; echo "<input type='hidden' name='row_id' value='" .$row['id']. "'>"; // added, modify if necessary to the proper column name echo "<input type='hidden' name='image' value='" .$row['imgPath']. "'>"; echo "<input type='hidden' name='price' value='" .$row['price']. "'>"; echo "<input type='hidden' name='details' value='" .$row['details']. "'>"; echo "<input type='submit' name='submit' value='buy' style='float:right'></div>"; } ?> </form> </div> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/165614-solved-hidden-fields-dont-work/#findComment-874043 Share on other sites More sharing options...
sum_guy Posted July 13, 2009 Author Share Posted July 13, 2009 No that still doesn't work. The hidden fields will bring back a value of 4 which is the amount of items within the table not the price. When I change the submit value to "$row['price']" it works but I want the value to be "buy". I need the hidden fields to update the session cart. Please advise. Quote Link to comment https://forums.phpfreaks.com/topic/165614-solved-hidden-fields-dont-work/#findComment-874472 Share on other sites More sharing options...
ignace Posted July 13, 2009 Share Posted July 13, 2009 What about an a element that you make look like a button: <a href="cart.php?action=add&product=<?php print $row['id']; ?>">ADD TO CART</a> Quote Link to comment https://forums.phpfreaks.com/topic/165614-solved-hidden-fields-dont-work/#findComment-874481 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.