Bhaal Posted April 17, 2006 Share Posted April 17, 2006 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? Quote Link to comment Share on other sites More sharing options...
Crashthatch Posted April 17, 2006 Share Posted April 17, 2006 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. Quote Link to comment Share on other sites More sharing options...
Orio Posted April 17, 2006 Share Posted April 17, 2006 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. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 17, 2006 Share Posted April 17, 2006 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] Quote Link to comment Share on other sites More sharing options...
Bhaal Posted April 17, 2006 Author Share Posted April 17, 2006 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...) Quote Link to comment Share on other sites More sharing options...
Bhaal Posted April 17, 2006 Author Share Posted April 17, 2006 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.