Page 1 of 1

using checked and unchecked values from radio buttons

Posted: Mon Jun 02, 2003 3:27 pm
by deejay
Hi

what i have is a series of questions and 1 radio button next to each one. So i need to be able to tell whether a button is checked or unchecked. i thought this would be as easy checking to see if its NULL but it isnt so.

any tips would be appreciated. my code looks a bit like

Code: Select all

<?php
  if(isset($_POST['submit'])){

     if ($_POST['first_question'] = true && $_POST['second_question'] = true){
      echo tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL');
     }
     else
      echo ' sorry you are not suitable to receive our product';

}
?>
and the form

Code: Select all

&lt;form name="form1" method="post" action="&lt;?php echo $PHP_SELF?&gt;"&gt;
&lt;table border="0" cellpadding="3" cellspacing="1" width="100%" class="forumline"&gt;
  &lt;tr&gt;
    &lt;td class="row3" colspan="3" height="25" valign="middle"&gt; Please Confirm All
      Statements &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td class="row1" width="95%"&gt; Please confirm 1st question
    &lt;/td&gt;
    &lt;td class="row2" width="5%"&gt;

        &lt;input type="radio" name="first_question" value="0"&gt;

    &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td class="row1"&gt;Please confirm second question
    &lt;td class="row2"&gt;

        &lt;input type="radio" name="second_question" value="0"&gt;

    &lt;/td&gt;
  &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td class="row1" colspan="2"&gt; &lt;input type="submit" value="Confirm" name="submit"&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;  &lt;/form&gt;
Thanks

Posted: Mon Jun 02, 2003 3:32 pm
by Sevengraff
I think radio buttons are like check boxes, where if it isn't clicked then no data will be sent, and $_POST['first_question'] simply wont exist. You should create a second radio button, and have them check yes/no for each question.

Code: Select all

<input type="radio" name="first_question" value="1" CHECKED>Yes
<input type="radio" name="first_question" value="0">No
If you make one checked by default, you can be assured of something being sent to php.

Posted: Mon Jun 02, 2003 6:18 pm
by redhair
Using this example given, you SHOULD use checkboxxes.

Radio butons are for more options under the same name...
Or do you like the round ones better?
Then you should do the other example given to you by
Sevengraff

Posted: Tue Jun 03, 2003 2:19 am
by deejay
cheers

checkboxes thats what i needed

Posted: Tue Jun 03, 2003 5:48 am
by deejay
erm.. no actually i still cant get it to work. the problem is with the what i am expecting to recieve as a value, i think

Code: Select all

<?php
if ($_POST['first_question'] = true && $_POST['second_question'] = true){ 
      echo tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'); 
     } 
     else 
      echo ' sorry you are not suitable to receive our product'; 

?>
what happens is that page is dierected whether the boxes are ticked or not. i have tried using 'false' instead of 'true' in which case the 'sorry' message is printed. I have also tried with '0' and '1' in which case I get the same as true. so the problem is i cannot seem to get different results depending on if the boxes are ticked.

Thanks

Posted: Tue Jun 03, 2003 5:58 am
by twigletmac
One thing that would definitely be a problem in the code you posted is that using just one equal sign in an if statement sets the variable to the value - two equal signs is a comparison.

However, for checking for the existance of a variable you should really be using isset():

Code: Select all

<?php 
if (isset($_POST['first_question']) && isset($_POST['second_question'])){ 
      echo tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'); 
} else {
      echo ' sorry you are not suitable to receive our product';
}
?>
Or if you are checking for 1's and 0's empty() (which also conviniently checks that the variable is set):

Code: Select all

<?php 
if (!empty($_POST['first_question']) && !empty($_POST['second_question'])){ 
      echo tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'); 
} else {
      echo ' sorry you are not suitable to receive our product';
}
?>
Often it is also useful to use:

Code: Select all

echo '<pre>';
print_r($_POST);
echo '</pre>';
to check what's coming in from the form.

Mac