Jump to content

Radio buttons - when no choice is selected


Bhaal

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?
Link to comment
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.
Link to comment
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.
Link to comment
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]
Link to comment
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...)
Link to comment
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!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.