Auto-Fill submitted form values


A time-saving PHP function

When I first started developing websites in PHP, refilling forms with their $_POST values after a submission was always a copy-paste headache. For little forms, it doesn’t add much time, but for a 30 or 40 field form (think real-estate website with tons of fields and options for posting a property) it becomes a mess. To make things easier for myself, I wrote a simple PHP function for filling in form values, check-boxes, radio buttons, etc.

Before writing this function I would have done something like the following for every field in a form.


<input type="text"> value="<?php if(isset($_POST['first_name'])){ echo htmlspecialchars($_POST['first_name']); } ?>" name="first_name">
<input type="text"> value="<?php if(isset($_POST['last_name'])){ echo htmlspecialchars($_POST['last_name']); } ?>" name="last_name">

That lead to some very repetitive code, especially when it came to drop-down selections where my code would look something like this:


<select name="color_option">
    <option value="#" <?php if(isset($_POST['color_option']) && $_POST['color_option'] == '#'){ echo ' selected = "selected" ';} ?>> Please Choose a Color</option>
    <option value="Red" <?php if(isset($_POST['color_option']) && $_POST['color_option'] == 'Red'){ echo ' selected = "selected" ';} ?>>Red</option>
    <option value="Blue" <?php if(isset($_POST['color_option']) && $_POST['color_option'] == 'Blue'){ echo ' selected = "selected" ';} ?>>Blue</option>
    <option value="Green" <?php if(isset($_POST['color_option']) && $_POST['color_option'] == 'Green'){ echo ' selected = "selected" ';} ?>>Green</option>
</select>

My Solution

To replace the above style of auto-filling in form values, I wrote the following function that covers most of my use-cases. You can simply save this function in it’s own file and include it when necessary.

One design decision I made was to have the function return the text rather than echo it. I did this so that I could use this function when working with wordpress shortcodes, which require that the output be buffered to a string rather than being echoed. If you’re not using this function in a wordpress environment, then you could easily modify it to echo the text.

function form_value($name, $type='text', $select_value = ''){

   if(isset($_POST[$name])){

      switch($type){

         case 'text':{
            return ' value="'.htmlspecialchars($_POST[$name]).'" ';
            break;
         }

         case 'textarea':{
            return htmlspecialchars($_POST[$name]);
            break;
         }

         case 'checkbox':{
            return ' checked="checked" ';
            break;
         }

         case 'radio':{
             if($_POST[$name] == $select_value){
               return ' checked="checked" ';
            }
            break;
         }

         case 'select':{
            if($_POST[$name] == $select_value){
               return ' selected="selected" ';
            }
            break;
         }

      }//switch

   }

   return '';
}

Using this function is pretty straightforward. When you want to fill in a form value after a form has been submitted (eg. when a field fails validation) you simply call the function with the input name, input type, and if it’s a select option, you can include the value to test against.

Below are some examples of the function being used for various input types.

Text input

<input type="text" name="name"  <?php echo form_value('name', 'text'); ?>>

You don’t actually have to pass the ‘text’ type to the function, since the function will default to a text type if no type is provided.

<input type="text" name="name"  <?php echo form_value('name'); ?>>

Checkbox

<label><input type="checkbox" name="terms_agreement" value="1" <?php echo form_value('terms_agreement', 'checkbox'); ?> > I agree to the terms of service.</label>

Radio
When using the radio option, you’ll need to pass the value of the radio button to check against the value of the submitted field.

<label><input type="radio" name="color" value="Red" <?php echo form_value('color', 'radio', 'Red'); ?> >Red</label><br>
<label><input type="radio" name="color" value="Blue" <?php echo form_value('color', 'radio', 'Blue'); ?> >Blue</label><br>
<label><input type="radio" name="color" value="Green" <?php echo form_value('color', 'radio', 'Green'); ?> >Green</label><br>

Select Options
Like the radio button, you’ll need to pass a value for the select options to be checked against. Below is the select example from earlier, but reworked using the form_value function.

<select name="color_option">
    <option value="#" <?php echo form_value('color_option', 'select', '#'); ?> > Please Choose a Color</option>
    <option value="Red" <?php echo form_value('color_option', 'select', 'Red'); ?> >Red</option>
    <option value="Blue" <?php echo form_value('color_option', 'select', 'Blue'); ?> >Blue</option>
    <option value="Green" <?php echo form_value('color_option', 'select', 'Green'); ?> >Green</option>
</select>