using checked and unchecked values from radio buttons

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

using checked and unchecked values from radio buttons

Post 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
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post 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.
User avatar
redhair
Forum Contributor
Posts: 300
Joined: Fri May 30, 2003 4:36 pm
Location: 53.23N-6.57E
Contact:

Post 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
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

Post by deejay »

cheers

checkboxes thats what i needed
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply