cosmicsafari Posted January 16, 2009 Share Posted January 16, 2009 Hey all, This is my first post so be gentle lol Im just looking for a hand with a piece of code that im working on, basically its for an online shop. Some of the products have special offers on them which is fine but some products have multiple special offers on them but a customer can only choose one special offer per item. So the user is shown there special offers options and each has a coresponding radio button, but onn submit i am having problems determining which radio button is chosen. Can someone help ??? Heres the 2 pieces of code incolved. This is the piece which in theory should determine which offer is chosen if(isset($_REQUEST['Include']) && $_REQUEST['Include']) { echo $_REQUEST['Include']; $loop = 0; while($loop < 1) { $value = $_REQUEST['Include']; if ($value != ''){ //Add any outfit options $on=0; while(isset($_REQUEST['outfit_'.$on.'_0'])) { $addtocart=""; $p=explode("_",$key); $x=1;$addtocart=$_REQUEST['outfit_'.$on.'_0']; while(isset($_REQUEST['outfit_'.$on.'_'.$x])) { $addtocart.=":".$_REQUEST['outfit_'.$on.'_'.$x]; $x++; } $chapcart->addToCart($addtocart,1); $on++; } } $loop++;} } This is the piece which displays the radio button on each offer. if($spectype==$MULTIBUY) { $specchoice=mysql_query("SELECT aref,name from attribute where ProductID='".$pieces[1]."'") or die(mysql_error()); $specialcount3=mysql_num_rows($specchoice); $choice="Add To Cart : <input type=\"radio\" name=\"Include\" id=\"Include$xt\" value=\"Add\"><br/><input onclick=\"document.getElementById('Include').checked='true';\" type=\"submit\" value=\"Add to cart.\"><br>"; $i=0; while($i != $specrow[5]) { $specchoice=mysql_query("SELECT aref,name from attribute where ProductID='".$pieces[1]."'") or die(mysql_error()); $specialcount3=mysql_num_rows($specchoice); $choice.="Choice ".($i+1).": <select name=\"outfit_".$i."_0\">"; while($test2=mysql_fetch_row($specchoice)) { $choice.="<option value=\"".$test2[0]."\">".$test2[1]."</option>\n"; } mysql_free_result($specchoice); $i++; $choice.="</select><br>\n"; } } Quote Link to comment Share on other sites More sharing options...
abdfahim Posted January 16, 2009 Share Posted January 16, 2009 name each radio button of a product same name (say "offer"). Then just get the value using $_POST['offer'] or $_GET['offer'] depending on your form method. Quote Link to comment Share on other sites More sharing options...
cosmicsafari Posted January 16, 2009 Author Share Posted January 16, 2009 They are already called "Include" and $_REQUEST if im not mistaken does the same job as $_POST and $_GET. ??? Quote Link to comment Share on other sites More sharing options...
abdfahim Posted January 16, 2009 Share Posted January 16, 2009 yes u r right.. but you have a long code which include some class variables... so I just give you an idea and ask u to check whether u apply the same logic. <?php echo $_POST['offer']; //This will return diff value depending on the button user selects //This is form portion echo "<form method='post'>"; echo "<input type='radio' name='offer' value='1'>"; echo "<input type='radio' name='offer' value='2'>"; echo "<input type='radio' name='offer' value='3'>"; echo "<input type='submit' name='submit' value='Submit'>"; echo "</form>"; ?> Quote Link to comment Share on other sites More sharing options...
Zephyr_Pure Posted January 16, 2009 Share Posted January 16, 2009 They are already called "Include" and $_REQUEST if im not mistaken does the same job as $_POST and $_GET. ??? On-topic: It looks like you'd need to use a checkbox or separate forms for each item, depending on your intent. Radio buttons should be used to capture a single choice (or question) only, not a single choice that references a whole block of other choices (item name, etc.). If you're looking for a multi-select cart, use checkboxes. If you're looking to allow editing or adding or whatever from a list of multiple things, use separate forms. Off-topic: Even though $_REQUEST contains both $_POST and $_GET (and $_COOKIE), it is not a good idea to use it as it can be a security risk. Basically, it exposes all of those superglobals above so that someone could inject values through the URL query string (since a $_GET variable would look no different than a $_POST or $_COOKIE). This could cause unexpected results if you were insufficiently protecting what you thought were "safe" values (which, of course, no inputs are safe). Rule of thumb: - Use $_POST when performing transactions. - Use $_GET mainly when displaying referenced data... or to supplement (but not entirely be) a form request. - Use $_REQUEST only as a shortcut to sanitizing your inputs with the same method. Quote Link to comment Share on other sites More sharing options...
abdfahim Posted January 16, 2009 Share Posted January 16, 2009 It looks like you'd need to use a checkbox or separate forms for each item if his intent is to allow user to select one offer per item, he can use one radio button set (same name) per item. So, if he has 5 items, he can just use 5 radio set with 5 name, each set contains radio buttons of offers. Quote Link to comment Share on other sites More sharing options...
Zephyr_Pure Posted January 16, 2009 Share Posted January 16, 2009 if his intent is to allow user to select one offer per item, he can use one radio button set (same name) per item. So, if he has 5 items, he can just use 5 radio set with 5 name, each set contains radio buttons of offers. In which case he will need to name them differently for each item, or the option chosen in the last will overwrite all of the rest. Radio buttons should be named the same within a single choice, but differently between questions. Quote Link to comment Share on other sites More sharing options...
abdfahim Posted January 16, 2009 Share Posted January 16, 2009 In which case he will need to name them differently for each item, or the option chosen in the last will overwrite all of the rest. Radio buttons should be named the same within a single choice, but differently between questions. Yes, that's wat i said.. if he has 5 item, then he has to make 5 radio set with 5 name ... so he can get 1 value for each item. Quote Link to comment Share on other sites More sharing options...
cosmicsafari Posted January 16, 2009 Author Share Posted January 16, 2009 It needs to be radio buttons as its only one special offer per item sold, it was originally however done with a check box but this was taken out in favor of radio buttons as multiple special offers where added. It basically boils down to this. Item Special Offer 1 (radio button) Special Offer 2 (radio button) (Add To Cart) I have started editing this code which may make it confusing in its original form it was if(isset($_REQUEST['Include']) && $_REQUEST['Include']) //Did I miss this check, or am I getting this wrong? { //Add any outfit options $on=0; while(isset($_REQUEST['outfit_'.$on.'_0'])) { $addtocart=""; $p=explode("_",$key); $x=1;$addtocart=$_REQUEST['outfit_'.$on.'_0']; while(isset($_REQUEST['outfit_'.$on.'_'.$x])) { $addtocart.=":".$_REQUEST['outfit_'.$on.'_'.$x]; $x++; } $chapcart->addToCart($addtocart,1); $on++; } } Quote Link to comment Share on other sites More sharing options...
abdfahim Posted January 16, 2009 Share Posted January 16, 2009 I m totally confused. In ur 1st post, i can find only one radio button !! there should be one radio for each offer. Why u are using select option instead? Quote Link to comment Share on other sites More sharing options...
cosmicsafari Posted January 16, 2009 Author Share Posted January 16, 2009 Each offer has a radio button to select that offer but certain offers have a drop down menu for additional options. Quote Link to comment Share on other sites More sharing options...
abdfahim Posted January 16, 2009 Share Posted January 16, 2009 but I can find only one Radio button in ur whole code !! or u don't post that portion?? Lemme understand .. You have 10 products, each have 6 offers. So you have 6 radio button for each of the 10 products, rite? So, you should have 10 different names for 60 radio buttons. Quote Link to comment 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.