Page 1 of 1

Preserving User Input: Checkboxes, Radio Buttons, Etc.

Posted: Mon Sep 12, 2005 1:54 pm
by cfytable
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.

Posted: Mon Sep 12, 2005 2:02 pm
by shiznatix
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

Posted: Mon Sep 12, 2005 2:12 pm
by feyd
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.

Posted: Mon Sep 12, 2005 6:33 pm
by ast3r3x
You could also do verification with javascript, which normally works out well.

Re: Preserving User Input: Checkboxes, Radio Buttons, Etc.

Posted: Mon Sep 12, 2005 8:39 pm
by harrisonad
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?
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.

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>";
}
This way you can construct your form with this input and give it's value according to what is passed

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;
hope i helped.