Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "remember" chosen form values in PHP?

If a user fails form validation, then to make it easier for them we can do this:

if(isset($_POST['submit'])) {
    $email = $_POST['email'];
    //....
}

<input type="text" name="phone" value="<?php echo $phone; ?>" />
<input type="text" name="email" value="<?php echo $email; ?>" />
...

So in this case, if the user fails validation because they entered an email but not a phone number, then when the page refreshes upon submit and warns them of the missing phone, the email (which they did fill out) will already be in its place and won't require the user to retype that.

But how can I "remember" the values chosen by the user for select, radio and checkbox inputs?

like image 566
Jacob Avatar asked Mar 04 '11 04:03

Jacob


2 Answers

It works the same, but it'll require some more work:

<select name="whatever">
  <option value="Apple">Apple</option>
  <option value="Banana" selected="selected">Banana</option>
  <option value="Mango">Mango</option>
</select>
Banana is selected here.

<input type="checkbox" name="fruits[]" value="banana" /> Banana
<input type="checkbox" name="fruits[]" value="mango" checked="checked" /> Mango
<input type="checkbox" name="fruits[]" value="apple" checked="checked" /> Apple
Mango and Apple are checked here

So basically you add selected="selected" or checked="checked" to the appropriate fields when you generate the form.

Another option would be using something like jQuery to make these selections once the page is loaded and ready for DOM manipulation. This way you could easily put all the changes in one place without uglifying the code. Of course there's now a downside that you'll need to load jQuery AND your users will need to have JS one.

like image 148
NullUserException Avatar answered Nov 01 '22 22:11

NullUserException


Here's some sample code.

<?php
$options = array('option1' => 'Option 1', 'option2' => 'Option 2', 'option3' => 'Option 3');
$myselect = 'option2';
?>
<select name="myselect">
<?php 
foreach($options as $key => $value) {
    echo sprintf('<option value="%s" %s>%s</option>', $key, $key == $myselect ? 'selected="selected"' : '', $value);
}
?>
</select>

If you are doing things like this regularly it is much tidier in a function or you could even create a Form class helper.

Here's a basic select function:

<?php
function form_select($name, $options, $selected) {
    $html = sprintf('<select name="%s">', $name);
    foreach($options as $key => $value) {
        $html .= sprintf('<option value="%s"', $key);
        if ($selected == $key)
            $html .= ' selected="selected"';
        $html .= sprintf('>%s</option>', $value);
    }
    $html .= '</select>';
    return $html;
}

Then you can create any selects simply by just calling:

echo form_select('myselect', $options, $selected);

You can easiliy make the function handle further attributes like style, class and id.

like image 42
Jacob Avatar answered Nov 01 '22 22:11

Jacob