Jump to content

Testing whether a radio button was checked


burntheblobs

Recommended Posts

I have a set of 10 radio buttons, all with the same name, and I am trying to write something that checks to see if any button has been checked. The simple $value == "" doesn't seem to be working. I also would like to try to avoid checking to see if there isn't a match for every possible value.

				<tr>
			<td>
				<input type='radio' name='experienceRating' value='0'>
			</td><td>
				<input type='radio' name='experienceRating' value='1'>
			</td><td>
				<input type='radio' name='experienceRating' value='2'>
			</td><td>
				<input type='radio' name='experienceRating' value='3'>
			</td><td>
				<input type='radio' name='experienceRating' value='4'>
			</td><td>
				<input type='radio' name='experienceRating' value='5'>
			</td><td>
				<input type='radio' name='experienceRating' value='6'>
			</td><td>
				<input type='radio' name='experienceRating' value='7'>
			</td><td>
				<input type='radio' name='experienceRating' value='8'>
			</td><td>
				<input type='radio' name='experienceRating' value='9'>
			</td><td>
				<input type='radio' name='experienceRating' value='10'>
			</td></tr>


	if ($field == "experienceRating")
		{
			if ($value == "")
		{
			echo "<font color='red'>You have not filled in a ";
			convertLabel($field);
			echo "!<br></font>";
			$badform = "yes";
		}

Thank you for the reply. I modified the code in a way that it SHOULD work, but it still doesn't work for some reason. Here is my code.

 

		if ($field == "Age" or $field == "firstRating"  
			or $field == "secondRating"  or $field == "thirdRating")
		{
			if (empty($_POST[$field]))
		{
			echo "<font color='red'>You have not filled in a ";
			convertLabel($field);
			echo "!<br></font>";
			$badform = "yes";
		}

wait sorry that's wrong.  Okay what I meant to say is, your inner condition will only execute if your outer one is true, and your outer one will only evaluate true if $field == one of those other things, like $_POST['Age'], not $_POST['experienceRating']. 

 

Where are you assigning something to $field?

All of this is inside a foreach loop. $field is the field name. $value is what is entered into the field.

foreach ($_POST as $field => $value)

where is $field being generated?  as far as i can see, $field has no value.

 

and as far as your foreach loop, $field will be set as the key in the array.

I have a form in another spot in the program that generates the $field. I was under the impression that $_POST was an array that you could assign a key and a value to. It seems to be working for all my other processes. Sorry for not including code earlier, I realize this it's kind of hard to help me when I'm being stingy with code.  :) Anyway, I was thinking that "experienceRating" would be the $field and $value would be whatever the value was.

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.