Jump to content


Photo

Radio buttons - when no choice is selected


  • Please log in to reply
5 replies to this topic

#1 Bhaal

Bhaal
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 17 April 2006 - 05:03 PM

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':

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

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

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;
}

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?

#2 Crashthatch

Crashthatch
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 17 April 2006 - 06:35 PM

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.

#3 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 17 April 2006 - 07:11 PM

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:
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;
}
And surround the part you are updating the db with:
if(!month===FALSE)
{//everything here}
Orio.
Think you're smarty?

(Gone until 20 to November)

#4 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 17 April 2006 - 08:09 PM

You could also use empty:
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;
}
}


#5 Bhaal

Bhaal
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 17 April 2006 - 08:52 PM

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...)

#6 Bhaal

Bhaal
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 17 April 2006 - 11:19 PM

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!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users