Jump to content

Determining State Of Radio Buttons With PHP


cosmicsafari

Recommended Posts

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";
				}
			}

Link to comment
Share on other sites

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>";
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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++;
						}
	}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.