Preserving User Input: Checkboxes, Radio Buttons, Etc.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cfytable
Forum Commoner
Posts: 29
Joined: Thu May 12, 2005 3:36 pm

Preserving User Input: Checkboxes, Radio Buttons, Etc.

Post 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.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
ast3r3x
Forum Commoner
Posts: 95
Joined: Thu Aug 19, 2004 8:36 pm

Post by ast3r3x »

You could also do verification with javascript, which normally works out well.
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

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

Post 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.
Post Reply