views:

215

answers:

5

Hello all ,

I want to write a Yes/No form with radio buttons. By default none of the buttons would be checked.

On submit, I want to check that at least one of then is checked!

function draw2($postid, $postName, $canId, $canName) // for Yes/No candidates 
{        
  colspan='2' img src='images/".$canId.".jpg'".$canName."

  input type='radio' name='".$postId . "' value='" . $postId . "_yes' />Yes

  input type='radio' name='".$postId . "' value='" . $postId . "_no' />No
}

The sceneario is that each person has Yes/No against him.

How do I record either a yes or no answer and ensure that at least one is chosen?

A: 

You can write a whitelist of required fieldsto check the input you want and then loop through.

$requiredFields = array('question-1', 'question-2', 'question-n');
$errors = array();

foreach ($_POST as $fieldName => $fieldValue)
{
   if (in_array($fieldName, $requiredFields)
   {
     if (empty($fieldValue) $errors[] = $fieldName . 'is required';
   }
}


if (count($errors) > 0)
{
   echo 'There are some errors : <br />';
   foreach ($errors as $error)
   {
       echo $error;
   }
}
Boris Guéry
A: 

Firstly, a user will not be able to check both yes and no when using radio buttons, so you only need to check one of them. If you specifically want to check both yes and no (why do you want to do this?) then you need to use checkboxes.

For radio buttons:

if (in_array('<whatever $postId is>', $_POST))
{
    // if $_POST['<whatever $postId is>'] == '<whatever $postId is>_yes>' -> user selected yes
    // if $_POST['<whatever $postId is>'] == '<whatever $postId is>_no>' -> user selected no
}
else
{
    // user did not select either yes or no
}

For checkboxes - do not give the same name to both checkboxes, call one $postId_yes and one $postId_no:

if (in_array('<whatever $postId is>_yes', $_POST) && $_POST['<whatever $postId is>_yes'] == 'on'))
{
    // user checked yes
}
else if (in_array('<whatever $postId is>_no', $_POST) && $_POST['<whatever $postId is>_no'] == 'on'))
{
    // user checked no
}
else
{
    // user did not check either yes or no
}

Editor hint:

Please edit your post, select the text in your post that's actually code, and hit the "code" button (the one with 101010 on it.) It'll look better then and more people may be inclined to answer.

Andy Shellam
sravankumar
A: 

Something like:

if(isset($_POST[$postId]))
dale
A: 

For a boolean (yes/no) value, a checkbox is more appropriate:

function print_checkbox($name, $value) {        
  echo '<input type="checkbox" name="'.$name.'"';
  if ($value) {
    echo ' checked="checked"';
  }
  echo ' />';
}

On the server side, you use isset to check if a checkbox is on or off. Eg.:

isset($_POST[$name])
troelskn
+2  A: 

No offense, really, but looking at the function above, I wouldn't even know where to start explaining. I don't think it is a good idea to give you the code, when you are apparently lacking fundamental basic knowledge in PHP and HTML.

I strongly suggest to get to grips with the basics first. Check out the PHP manual, do a few tutorials on sitepoint, understand how forms are build with HTML, learn how to separate PHP from HTML, etc.

Gordon