tags:

views:

267

answers:

2

Given that when using an html radio box, to send values to a php script, only the selected values is sent to the php script, still holds true.

Do I still need to check that the user has selected something, when I am only interested in the selected value being sent to the php script, if the user does not select anything we do nothing or I guess I could prompt the user to select something.

If so, whats the best way to handle this with regards to radio boxes?

 <!--Using the radio box, a single selected value is sent to the php script for processing-->
    <td width="100px" height="30px">  
    <input type = "radio"
    name = "selection"
    value = "Lays" />Lays Chips 0.99c<br />

    <input type = "radio"
    name = "selection"
    value = "Ruffles" />Ruffles $1.85<br />
A: 

Even if the radiobox is not set, you can still post the form back to the server. Unless, of course, you have Javascript that prevents one from clicking on the submit form if the radiobox is not set.

So, you still need to check whether the radiobox is set or not, before working on it.

Ngu Soon Hui
+2  A: 

The user will be able to click the "submit" button even without selecting an option from your radiobox.

If it's ok to send the form without a selection and you just want to do something different (or ignore it) when nothing is selected, you can do this:

<?php
  if (isset(($_POST['selection'])) {
    //Use it.. and make sure to validate user input.
  } else {
    //nothing was selected. Do something or just ignore?
  }
?>

OTOH, if you want to prevent submiting without a selection, you will need to use some JavaScript to do it.

Carlos Lima
Thank you, Carlos Lima.
Newb
I'm happy to help! :)
Carlos Lima