Jump to content

Archived

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

Bhaal

Radio buttons - when no choice is selected

Recommended Posts

Hi all,

I have a form with radio buttons and a Switch Case that writes data to a db depending on the user's choice. However, how can I prevent data from being written if the user doesn't make a selection? The way it is now, if there is no selection made, the existing data is overwritten with 'null'.

Here are the radio buttons - grouped with the same 'name' each with a different 'value':

[code]
<input name="package" type="radio" value="1month" />
<input name="package" type="radio" value="2month" />
<input name="package" type="radio" value="3month" />
[/code]

The switch case modifies a variable based on what was selected (this is inside an "if(isset)" for the form):

[code]
switch($_POST['package']){
     case '1month':
     $month = mktime(0, 0, 0, date("m")+1  , date("d"), date("Y"));
     break;
      case '2month':
     $month = mktime(0, 0, 0, date("m")+2  , date("d"), date("Y"));
      break;
      case '3month':
      $month = mktime(0, 0, 0, date("m")+3  , date("d"), date("Y"));
      break;
}
[/code]

Then an insert query copies the value in $month to a date field.

But, if there's already a value in the date field, it would be optimal if existing data was not overwritten if the user selects nothing - or have a radio button selection for "keep existing data".

I've tried to bracket the switch case inside "if(isset($_POST['package']))" but that didn't work.

Any insight?

Share this post


Link to post
Share on other sites
Perhaps surround the query with "if(isset($_POST['package']))" instead of the switch? That way the query only runs and the field is only updated if one of the radio buttons was selected.

A "case null" where you set $skipquery then only do the query if $skipquery not set would be a slightly longer way of doing it.

Share this post


Link to post
Share on other sites
You can have one radio button selected from the begining and prevent the whole thing, in case that suits you condition.

You can also do something like this:
[code]switch($_POST['package']){
     case '1month':
     $month = mktime(0, 0, 0, date("m")+1  , date("d"), date("Y"));
     break;
      case '2month':
     $month = mktime(0, 0, 0, date("m")+2  , date("d"), date("Y"));
      break;
      case '3month':
      $month = mktime(0, 0, 0, date("m")+3  , date("d"), date("Y"));
      break;
      defualt:
      $month = FALSE;
      break;
}[/code]
And surround the part you are updating the db with:
[code]if(!month===FALSE)
{//everything here}[/code]
Orio.

Share this post


Link to post
Share on other sites
You could also use empty:
[code]
if(!empty($_POST['package'])){
switch($_POST['package']){
     case '1month':
     $month = mktime(0, 0, 0, date("m")+1  , date("d"), date("Y"));
     break;
      case '2month':
     $month = mktime(0, 0, 0, date("m")+2  , date("d"), date("Y"));
      break;
      case '3month':
      $month = mktime(0, 0, 0, date("m")+3  , date("d"), date("Y"));
      break;
}
}
[/code]

Share this post


Link to post
Share on other sites
Thanks for the replies!

I've tried all three:

- if(!empty($_POST['package']))

- default:
$month = FALSE;
The surrond the insert query with:
if(!month===FALSE)

- surround the query with "if(isset($_POST['package']))"

None worked: they all still made the variable $month '0' (not null I believe) and sent the original data packing off to dream land.

Any more insights?

(Thanks again for the replies, though...)

Share this post


Link to post
Share on other sites
Solved!

I run a separate query at the top of the page that gets the existing data. Then I have a ‘checked’ radio button that sets $month (the variable in question) to this existing data with a label that reads "No Change".

Thanks for the replies...again!

Share this post


Link to post
Share on other sites

×

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.