BoroDracula Posted July 20, 2012 Share Posted July 20, 2012 Well been learning php for a few weeks now and this little bit is driving me crazy. I have 2 pages, one is product.php and the other is cart.php In product.php I have a drop down menu (size of the product, small, medium and large) and I want that variable to be passed along to cart.php so in the cart it shows what size the user has selected. Once it is there it should naturally be able to be processed to paypal Following is the base code I have so far for both product.php and cart.php Sorry for the erratic layout of the text, it does not transport well from dreamweaver Product.php <?php // Script Error Reporting error_reporting(E_ALL); ini_set('display_errors','1'); ?> <?php // Check to see the URL variable is set and that it exsists in the database if (isset($_GET['id'])) { // run a select query to get the latest 6 items include "storescripts/connect_to_mysql.php"; $id = preg_replace('#[^0-9]#i','',$_GET['id']); // Use this var to check to see if the ID exsists, if yes then get the product // details, if no then exit then script and show the following $sql = mysql_query("SELECT * FROM products WHERE id='$id' LIMIT 1"); $productCount = mysql_num_rows($sql); // count the output amount if ($productCount > 0){ // Get all the product details while($row = mysql_fetch_array($sql)){ $product_name = $row["product_name"]; $sku = $row["sku"]; $price = $row["price"]; $material = $row["material"]; $description = $row["description"]; $category = $row["category"]; $subcategory = $row["subcategory"]; } } else { echo "This product does not exsist"; exit (); } } else { echo "Data to render this page is missing"; exit (); mysql_close (); }?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Revs Clothing - <?php echo $product_name; ?></title> <link href="style/dropdown/dropdown.css" media="screen" rel="stylesheet" type="text/css" /> <link href="style/dropdown/themes/default.advanced.css" media="screen" rel="stylesheet" type="text/css" /> <link href="style/style.css" rel="stylesheet" media="screen" /> </head> <body> <div align="center" id="mainWrapper"> <?php include_once("template_header.php");?> <div id="pageContent"> <table width="100%" border="0" height="450"> <tr> <td width="15%" valign="top"> <?php include_once("template_left.php");?> </td> <td width="69%" valign="top"> <table width="100%" border="0"> <tr> <td width="54%" height="131" valign="top"><p><img src="inventory_images/<?php echo $id; ?>.jpg" width="300" alt="<?php echo product_name; ?>"/></p> <p><a href="inventory_images/<?php echo $id; ?>.jpg">View full sized image</a></p></td> <td width="46%" valign="top"><p>Name:<br /> <strong><?php echo $product_name; ?></strong></p> <p>Item Code: <br /> <strong><?php echo "$id $sku"; ?></strong></p> <p>Price: <br /> <strong><?php echo "?".$price; ?></strong></p> <p>Size: <br /> <p>Material: <br /> <strong><?php echo $material; ?></strong></p> <p>Description:<br /> <strong><?php echo $description; ?></strong></p> <p> </p> <form id="form1" name="form1" method="POST" action="cart.php"> <input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" /> <select name="size1" value="$size1" <?php echo $size1; ?>> <option value="Small">Small</option> <option value="Medium">Medium</option> <option value="Large">Large</option> </select> <input type="submit" name="button" id="button" value="Add to Basket" /> </form> </td> </tr> </table> </td> <td width="16%" valign="top">More stuff</td> </tr> </table> </div> <?php include_once("template_footer.php");?> </div> </body> </html> Cart.php <?php session_start(); //start session first thing in script // Script Error Reporting error_reporting(E_ALL); ini_set('display_errors','1'); // Connect to mysql database include "storescripts/connect_to_mysql.php"; $size1 = '0'; ?> <?php //////////// // Section - If user attempts to add something to cart on product page if (isset($_POST['pid'])){ $pid = $_POST['pid']; $size1 = $_POST['size1']; $wasFound = false; $i = 0; // if the cart session variable is not set or the cart array is empty if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){ // Run if cart is empty $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "size1" => $size1, "quantity" => 1)); } else { // Run if cart has at least one item foreach ($_SESSION["cart_array"] as $each_item) { $i++; while (list($key, $value) = each($each_item)){ if ($key == "item_id" && $value == $pid) { // That item is in the cart already so lets adjust the its quantity using array_splice() array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "size1" => $size1, "quantity" => $each_item['quantity'] +1))); $wasFound = true; } // close if condition } //close while loop } // close foreach loop if ($wasFound == false) { array_push($_SESSION["cart_array"], array("item_id" => $pid, "size1" => $size1, "quantity" => 1)); } } header("location: cart.php"); exit(); } ?> <?php /////////////////////////// // Section - If User chooses to empty shopping cart if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") { unset($_SESSION["cart_array"]); } ?> <?php /////////////////////////// // Section - If User chooses to adjust item quantity if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") { // execute some code $item_to_adjust = $_POST['item_to_adjust']; $size1 = $_POST['size1']; $quantity = $_POST['quantity']; $quantity = preg_replace('#[^0-9]#i',"",$quantity); // filter out everything but numbers if ($quantity >= 100) { $quantity = 99; } if ($quantity <= 0) { $quantity = 1; } $i = 0; foreach ($_SESSION["cart_array"] as $each_item) { $i++; while (list($key, $value) = each($each_item)){ if ($key == "item_id" && $value == $item_to_adjust) { // That item is in the cart already so lets adjust the its quantity using array_splice() array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "size1" => $size1, "quantity" => $quantity))); } // close if condition } //close while loop } // close foreach loop } ?> <?php /////////////////////////// // Section - If user wants to remove an item from the cart if(isset($_POST['index_to_remove']) && $_POST['index_to_remove'] !="") { // Access the array and run code to remove array index $key_to_remove = $_POST['index_to_remove']; if(count($_SESSION["cart_array"]) <= 1) { unset($_SESSION["cart_array"]); } else { unset($_SESSION["cart_array"]["$key_to_remove"]); sort($_SESSION["cart_array"]); } } ?> <?php /////////////////////////// // Section - render the cart for the user to view $size1 = "0"; $cartOutput = ""; $cartTotal = ""; $pp_checkout_btn = ''; $product_id_array = ''; if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) <1) { $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>"; } else { // start Paypal checkout button $pp_checkout_btn = '<form action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_cart"> <input type="hidden" name="upload" value="1"> <input type="hidden" name="business" value="[email protected]">'; // Start the for each loop $i = 0; foreach ($_SESSION["cart_array"] as $each_item) { $item_id = $each_item['item_id']; $sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1"); while ($row = mysql_fetch_array($sql)) { $id = $row["id"]; $product_name = $row["product_name"]; $price = $row["price"]; $description = $row["description"]; } $pricetotal = $price * $each_item['quantity']; $cartTotal = $pricetotal + $cartTotal; setlocale(LC_MONETARY, "en_GB.UTF-8"); $pricetotal = money_format("%10.2n",$pricetotal); // Dynamic Checkout Btn Assembly $x = $i + 1; $pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '"> <input type="hidden" name="amount_' . $x . '" value="' . $price . '"> <input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '"> '; // Create the product array variable $product_id_array .= "$item_id-".$each_item['quantity'].","; // Dynamic Table Row Assembly $cartOutput .='<tr align="center">'; $cartOutput .='<td><img src="inventory_images/'.$id.'.jpg" alt="' . $product_name . '" height="52" border="0" /></td>'; $cartOutput .='<td><a href="product.php?id=' . $id . '">' . $product_name . '</a></td>'; $cartOutput .='<td>'.$description.'</td>'; $cartOutput .='<td>'.$size1.'</td>'; $cartOutput .='<td>?'.$price.'</td>'; $cartOutput .='<td><form action="cart.php" method="post"> <input name="quantity" type="text" value="'.$each_item['quantity'].'" size="1" maxlength="2" /> <input name="adjustBtn' . $id . '" type="submit" value="change"/> <input name="item_to_adjust" type="hidden" value="' . $id . '" /></form></td>'; //$cartOutput .='<td>'.$each_item['quantity'].'</td>'; $cartOutput .='<td>'.$pricetotal.'</td>'; $cartOutput .='<td><form action="cart.php" method="post"> <input name="deleteBtn' . $id . '" type="submit" value="Remove"/> <input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>'; $cartOutput .='</tr>'; $i++; } setlocale(LC_MONETARY, "en_GB.UTF-8"); $cartTotal = money_format("%10.2n",$cartTotal); $cartTotal = "<div align='right'>".$cartTotal."</div>"; // Finish the Paypal Checkout Btn $pp_checkout_btn .= '<input type="hidden" name="custom" value="' . $product_id_array . '"> <input type="hidden" name="notify_url" value="http://www.revsclothing.co.uk/storescripts/my_ipn.php"> <input type="hidden" name="return" value="http://www.revsclothing.co.uk/checkout_complete.php"> <input type="hidden" name="rm" value="2"> <input type="hidden" name="cbt" value="Return to The Store"> <input type="hidden" name="cancel_return" value="http://www.revsclothing.co.uk/cart.php"> <input type="hidden" name="lc" value="GB"> <input type="hidden" name="currency_code" value="GBP"> <input type="image" src="http://www.paypal.com/en_GB/i/btn/x-click-but01.gif" name="submit" alt="Make payments with PayPal - its fast, free and secure!"> </form>'; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Revs Clothing - Your shopping basket</title> <link href="style/dropdown/dropdown.css" media="screen" rel="stylesheet" type="text/css" /> <link href="style/dropdown/themes/default.advanced.css" media="screen" rel="stylesheet" type="text/css" /> <link href="style/style.css" rel="stylesheet" media="screen" /> </head> <body> <div align="center" id="mainWrapper"> <?php include_once("template_header.php");?> <div id="pageContent"> <div style="text-align:left;"> <br/> <table width="95%" border="0" cellpadding="6" align="center"> <tr bgcolor="00c0ff"> <td width="6%" height="30" align="center">Image</td> <td width="19%" align="center">Product</td> <td width="27%" align="center">Desciption</td> <td width="10%" align="center">Size</td> <td width="8%" align="center">Unit Price</td> <td width="12%" align="center">Quantity</td> <td width="8%" align="center">Total</td> <td width="10%" align="center">Remove</td> </tr> <?php echo $cartOutput; ?> <tr> <td width="6%" height="30" align="center"></td> <td width="19%" align="center"></td> <td width="27%" align="center"></td> <td width="10%" align="center"></td> <td width="8%" align="center"></td> <td width="12%" align="center"><strong>BASKET TOTAL</strong></td> <td width="8%" align="center"><strong><?php echo $cartTotal ?></strong></td> <td width="10%" align="center"></td> </tr> </table> <br /> <div style="text-align:right;"> <table width="95%" border="0"> <tr> <td width="24%"><a href="cart.php?cmd=emptycart">Click here to empty your basket </a></td> <td width="68%"> </td> <td width="8%"><?php echo $pp_checkout_btn; ?></td> </tr> </table> </div> </div><br/> </div> <?php include_once("template_footer.php");?> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/266012-help-passing-a-variable-size-in-drop-down-box-from-product-page-to-cart/ Share on other sites More sharing options...
scootstah Posted July 21, 2012 Share Posted July 21, 2012 So what's the problem? Quote Link to comment https://forums.phpfreaks.com/topic/266012-help-passing-a-variable-size-in-drop-down-box-from-product-page-to-cart/#findComment-1363209 Share on other sites More sharing options...
BoroDracula Posted July 21, 2012 Author Share Posted July 21, 2012 the size option in the drop down menu box on product.php is not being sent through to the cart.php so if the user selects medium, this is not transporting through to the cart and being rendered. I know at the moment its only showing a 0 as I cannot for the life of me figure out how the hell to get this to link to the drop down select form tag in product.php I have tried everything that my limited knowledge of php knows ($_GET, $_POST, S_REQUEST) hence the asking for help Quote Link to comment https://forums.phpfreaks.com/topic/266012-help-passing-a-variable-size-in-drop-down-box-from-product-page-to-cart/#findComment-1363217 Share on other sites More sharing options...
scootstah Posted July 21, 2012 Share Posted July 21, 2012 Get rid of value="$size1" <?php echo $size1; ?> inside the select tag. It should just be <select name="size1"> Quote Link to comment https://forums.phpfreaks.com/topic/266012-help-passing-a-variable-size-in-drop-down-box-from-product-page-to-cart/#findComment-1363275 Share on other sites More sharing options...
BoroDracula Posted July 21, 2012 Author Share Posted July 21, 2012 thanks scootstah, sorted that out, any idea on the rest of it. its driving me to my wits end here Quote Link to comment https://forums.phpfreaks.com/topic/266012-help-passing-a-variable-size-in-drop-down-box-from-product-page-to-cart/#findComment-1363332 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.