aussie Posted July 18, 2009 Share Posted July 18, 2009 Hi, Have added product options to my script. I have basic php skills. Have finally been able to get the product option prices to appear in the cart. BUT all the options are being posted instead of the one selected. I've adapted other parts of of my script and examples from google and this forum. I have the options displaying on the page in their rows. If a product has 3 options all 3 rows are posted into the cart when I only want one. I do want to add the ability for multiple select but just need to get one for now. I think its a $row issue... // get available options $sql= "SELECT * FROM tbl_pd_options_link opt, tbl_pd_attribute pda WHERE opt.pd_id = $pdId AND opt.attribute_id = pda.attribute_id ORDER BY pda.attribute_id"; $result = dbQuery($sql); $options = ''; while ($row = dbFetchAssoc($result)) { extract($row);{ $options .= "<option value=\"$attribute_price\""; //Maybe this line below not sure what == means 7 million google references if ($attribute_id == $pdId) { $options .= " selected"; } $options .= ">$attribute_name $attribute_value $attribute_price</option>\r\n"; } } This is my combo box displaying the options. <form action="<?php echo $cart_url; ?>" method="post"> <select name="cboOption" id="cboOption" class="box"> <?php echo $options;?> </select> <input type="submit" name="Submit" value="Submit"> </form> The form action echo $cart_url is <form action="cart.php?action=add&p=114" method="post"> and calls this function. function getCartContent() { $cartContent = array(); $sid = session_id(); //statement probably shouldn't been written like this but it works. $sql= "SELECT * FROM tbl_cart ct, tbl_product pd, tbl_category cat, tbl_pd_options_link opt, tbl_pd_attribute pda WHERE ct_session_id = '$sid' AND ct.pd_id = pd.pd_id AND cat.cat_id = pd.cat_id AND opt.pd_id = pd.pd_id AND opt.attribute_id = pda.attribute_id AND opt.attribute_checked = 1 ORDER BY pda.attribute_id"; $result = dbQuery($sql); while ($row = dbFetchAssoc($result)) { $cartContent[] = $row; } return $cartContent; } Quote Link to comment https://forums.phpfreaks.com/topic/166391-solved-help-combo-box-posting-all-values-instead-of-selected/ Share on other sites More sharing options...
aussie Posted July 18, 2009 Author Share Posted July 18, 2009 I am really stuck and not able to go any further with this, would really appreciate some help thanks. Have a asked the wrong question? Quote Link to comment https://forums.phpfreaks.com/topic/166391-solved-help-combo-box-posting-all-values-instead-of-selected/#findComment-877470 Share on other sites More sharing options...
Psycho Posted July 18, 2009 Share Posted July 18, 2009 Have you checked the HTML output of your form? I'm guessing that all the options may be set as selected. Also, in your loop that creates the options, have you verified the values of $attribute_id and $pdId which are used to determine if the fields default to selected? Have you verified the actual POST data on the receiving page? Run a print_r($_POST) to verifiy the data is what you expect. When something doesn't work you need to use critical thinking skills to find the problem. Determine where an error could be occuring and find a way to validate/investigate that particular scenario. Then once you find where the problem is occuring you can determine why it is occuring and, lastly, determine how to fix it. Quote Link to comment https://forums.phpfreaks.com/topic/166391-solved-help-combo-box-posting-all-values-instead-of-selected/#findComment-877481 Share on other sites More sharing options...
sniperscope Posted July 18, 2009 Share Posted July 18, 2009 Why don't you try something like this <select name="cboOption" id="cboOption" class="box"> <?php while ($row = dbFetchAssoc($result)) { echo "<option value=\"$attribute_price\">$attribute_price</option>"; } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/166391-solved-help-combo-box-posting-all-values-instead-of-selected/#findComment-877487 Share on other sites More sharing options...
aussie Posted July 18, 2009 Author Share Posted July 18, 2009 Have you checked the HTML output of your form? I'm guessing that all the options may be set as selected. Yes over and over for the last 2 days. Although this time have noticed. the select name ="cboOptions" (I think I may have wrongly named it??) And all the options are selected which I don't know how to change to fix it. In html there would be a <option value="size small" selected>small</option> //The html output <form action="cart.php?action=add&p=114" method="post"> <select name="cboOption" id="cboOption" class="box"> <option value="1">Small 2.5 x 3.5 $5.00</option> <option value="2">Medium 5 x 7 $7.50</option> <option value="3">Large 8 x 10 $12.95</option> </select> <input type="submit" name="Submit" value="Submit"> </form> ///// The PHP <form action="<?php echo $cart_url; ?>" method="post"> <select name="cboOption" id="cboOption" class="box"> <?php echo $options; ?> </select> <input type="submit" name="Submit" value="Submit"> </form> Also, in your loop that creates the options, have you verified the values of $attribute_id and $pdId which are used to determine if the fields default to selected? Sorry could you explain a bit more what this means and maybe how to do it? Have you verified the actual POST data on the receiving page? Run a print_r($_POST) to verifiy the data is what you expect. print_r($_POST) Show array() on the page which means that all in the array is being posted???? )(Which is not what I want) Quote Link to comment https://forums.phpfreaks.com/topic/166391-solved-help-combo-box-posting-all-values-instead-of-selected/#findComment-877493 Share on other sites More sharing options...
aussie Posted July 18, 2009 Author Share Posted July 18, 2009 Why don't you try something like this <select name="cboOption" id="cboOption" class="box"> <?php while ($row = dbFetchAssoc($result)) { echo "<option value=\"$attribute_price\">$attribute_price</option>"; } ?> </select> thanks this is much easier to understand, Just tried <form action="<?php echo $cart_url; ?>" method="post"> <select name="cboOption" id="cboOption" class="box"> <?php while ($row = dbFetchAssoc($result)) { echo "<option value=\"$attribute_id\">$attribute_name $attribute_value $attribute_price</option>"; echo ""; } }?> </select> <input type="submit" name="Submit" value="Submit"> </form> <?php print_r($_POST) ; ?> Now I get one option 3 times in the combo box and on submit, all the available product options in the database are still added to the cart So I tried this changed it to: <form action="<?php echo $cart_url; ?>" method="post"> <select name="cboOptions" class='box'> <? $sql = "SELECT * FROM tbl_pd_options_link opt, tbl_pd_attribute pda WHERE opt.pd_id = $pdId AND opt.attribute_id = pda.attribute_id ORDER BY pda.attribute_id"; $result = dbQuery($sql); while ($row = dbFetchAssoc($result)) { //changed here: echo "<option value=".$row['attribute_id'].">".$row['attribute_name'].$row['attribute_price']."</option>"; } ?> </select> <input type="submit" name="Submit" value="Submit"> </form> This gives me the exact same result as I started with although, I would rather have the code like this (easier to understand) But all options are still being posted into the cart. How do I add the select bit and where do I put it.????? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/166391-solved-help-combo-box-posting-all-values-instead-of-selected/#findComment-877501 Share on other sites More sharing options...
sniperscope Posted July 18, 2009 Share Posted July 18, 2009 //changed here: echo "<option value="\".$row['attribute_id']."\">".$row['attribute_name'].$row['attribute_price']."</option>"; you missed there some backslashes and some double quotes. Quote Link to comment https://forums.phpfreaks.com/topic/166391-solved-help-combo-box-posting-all-values-instead-of-selected/#findComment-877549 Share on other sites More sharing options...
sniperscope Posted July 18, 2009 Share Posted July 18, 2009 in your code, your sending form with post method but i can not see anything to recieve that posted data in chart.php file. function getCartContent(){ $cartContent = array(); $sid = session_id(); //statement probably shouldn't been written like this but it works. $sql= "SELECT * FROM tbl_cart ct, tbl_product pd, tbl_category cat, tbl_pd_options_link opt, tbl_pd_attribute pda WHERE ct_session_id = '$sid' AND ct.pd_id = pd.pd_id AND cat.cat_id = pd.cat_id AND opt.pd_id = pd.pd_id AND opt.attribute_id = pda.attribute_id AND attribute_checked = 1 ORDER BY pda.attribute_id"; $result = dbQuery($sql); while ($row = dbFetchAssoc($result)) { $cartContent[] = $row; } return $cartContent; } Quote Link to comment https://forums.phpfreaks.com/topic/166391-solved-help-combo-box-posting-all-values-instead-of-selected/#findComment-877550 Share on other sites More sharing options...
aussie Posted July 18, 2009 Author Share Posted July 18, 2009 No I think that is one of issues. in your code, your sending form with post method but i can not see anything to recieve that posted data in chart.php file. //changed here: echo "<option value="\".$row['attribute_id']."\">".$row['attribute_name'].$row['attribute_price']."</option>"; you missed there some backslashes and some double quotes. Thanks just tried that got parse errors This bit is working echo "<option value=".$row['attribute_id'].">".$row['attribute_name'].$row['attribute_price']."</option>"; The options are all displaying on the products page in the combo box. After many hours and more searching. The issue is when I hit the submit button to <form action="cart.php?action=add&p=114" method="post"> (product 114 is the $pdId) I'm not doing a $_POST anywhere. and not sure where to do it. I tried doing $attribute_price = $_POST['attribute_price'] I tried that on my product_detail.php, cart.php and cartfunctions.php at all different times. So if that is the right bit of code I'm supposed to add I must be putting it in the wrong place. I don't think I am processing the form in cart.php What kinda of thing would I need to add. Also nothing is selected. I made up a mock combo box in html. When you click on item in combo, beside it in the html source is says SELECTED. This is not happening in my html source as it is. I want the attribute_price posted to the cart. Which is happening, just every single choice in the combo box is being posted Added to the cart is 5 items instead of one. I am also wondering if this maybe needs a javascript for the SELECTED option. Originally posted to the cart.php and cartfunctions.php was the product_price. If that helps. Quote Link to comment https://forums.phpfreaks.com/topic/166391-solved-help-combo-box-posting-all-values-instead-of-selected/#findComment-877554 Share on other sites More sharing options...
aussie Posted July 24, 2009 Author Share Posted July 24, 2009 Hi I am STILL working on this. One line for days..................... if ($attribute_id == What do I put here) I can get it to either selected everything or select nothing. I have tried everything I can think of that might work. I am missing something???????????? I have tried if ($attribute_id == $attribute_id) (this selects everything and I understand why) if ($attribute_id == ' ') (this doesn't select anything) I thought this would set it like to a null value so when I choose in drop down it would change. NO if ($attribute_id == " ") selects nothing if ($attribute_id == "cboOptions ") selects nothing if ($attribute_id == $_GET['attribute_id']) throws error. tried post and get the form name.... $options =''; while ($row=dbFetchAssoc($result)) { extract ($row); $options .="<option value=\"$attribute_id\""; if ($attribute_id == "") THIS LINE********* { $options .= "selected"; } $options .=">$attribute_name $attribute_value $$attribute_price</option>\r\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/166391-solved-help-combo-box-posting-all-values-instead-of-selected/#findComment-882372 Share on other sites More sharing options...
aussie Posted July 24, 2009 Author Share Posted July 24, 2009 I have got it to work. I did this if ($attribute_id == "3") So option value 3 is selected. when I choose a different option, it is not selected on the html source view, But the new value selected is posted to the next page. Don't understand why. Quote Link to comment https://forums.phpfreaks.com/topic/166391-solved-help-combo-box-posting-all-values-instead-of-selected/#findComment-882388 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.