I have an HTML form with a bunch of radio buttons, checkboxes, etc. The form either inserts a new record into the DB or it updates an existing one. The form knows which scenario to follow based on various hidden fields.
Here's the thing...In either case, the data has to pass a couple of validation rules contained within the same php file. If the submission fails one of these tests, I want the user to be returned to the form. I assume that, regardless of whether we're dealing with an update or insert, everything becomes accessible via $_POST at that point. But how would I handle checkboxes, radio buttons, and drop-down menus not only in that case but in original case of pre-populating with values from the database? How would I get, for instance, a radio button to highlight either the existing value in the DB or what the user has tried to submit?
Any help would be appreciated.
Preserving User Input: Checkboxes, Radio Buttons, Etc.
Moderator: General Moderators
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
what you can do is just say "this is invalid please go back and fix it" then make them click the back button. you can put in a header on the first page with the form that will make it so they don't get that message that goes somthing like "this page contains post data blah blah blah" and everything will be left the same
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
it's quite simple: you know what values from the post data go where. When you reach a checkbox that's in the post data you emit "checked" into the html. Same for radio buttons. Select boxes are similar, except you look at the options finding the one that was selected and emit "selected"
note: those are the HTML 4 versions, XHTML would use checked="checked" and selected="selected" respectively.
note: those are the HTML 4 versions, XHTML would use checked="checked" and selected="selected" respectively.
- harrisonad
- Forum Contributor
- Posts: 288
- Joined: Fri Oct 15, 2004 4:58 am
- Location: Philippines
- Contact:
Re: Preserving User Input: Checkboxes, Radio Buttons, Etc.
I usually make a function for specific inputs (select box,textbox,radio box,etc.) that takes a value as one of the arguement. for example is a select box.cfytable wrote:But how would I handle checkboxes, radio buttons, and drop-down menus not only in that case but in original case of pre-populating with values from the database?
Code: Select all
function CountrySelectionBox($selected='')
{
$str = "
<select name='country'>";
$countries = array('Alaska','Canada','Philippines','etc.');
foreach ($countries as $country) {
$attr = $country==$selected ? 'SELECTED' : '';
$str .= "
<option value='$country' $attr>$country</option>";
}
$str .= "
</select>";
}Code: Select all
$sel_country = isset($_POST['country']) ? $_POST['country'] : '';
$str = "
<form method=post action='process.php'>
".CountrySelectionBox($sel_country)."
<!-- other inputs -->
</form>";
echo $str;