Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryManaging Website Forms

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Pre-Fill Form With URL Parameters

PHP can use URL parameter information for pre-filling form fields when a page loads.

A URL parameter is the information that follows a "?". Here are a couple examples.

https://example.com/?name=Will
https://example.com/page.php?name=Will&color=blue&disposition=smiley

name=Will is the parameter information of the first example URL. And name=Will&color=blue&disposition=smiley is the parameter information of the second URL.

When the parameter contains name=value pairs, the paired information can be used to populate or pre-fill certain form fields.

The name=value pairs in the above examples are name=Will, color=blue, and disposition=smiley.

A form with fields using that information might look something like this, assuming the information will be used in a text field, a checkbox, and a dropdown list.


Blue

Note: Do not use URL parameters with private or sensitive information — an email address, for example, or an account number. Information in URL parameters can be seen in the browser's address bar. Further, the server's access log contains URL parameter information.

PHP is used to pre-fill form text fields, pre-check radio buttons and checkboxes, and pre-select dropdown and multi-select list options. (If you prefer to use JavaScript to pre-fill the form fields instead of using PHP, see the Pre-Fill and Multi-Page Forms with JavaScript library article.)

Values for hidden form fields can also be populated. However, file upload fields can not be pre-filled. For security reasons, the form user has to specify uploads themself with a file name selected from their hard drive.

When a PHP page is requested, PHP creates $_GET variables from any URL parameter information.

Of the two URLs in the example at the beginning of this article, the first URL would create the variable $_GET['name'] with the value Will. The second URL would create these $_GET variables:

  • $_GET['name'] with the value Will
  • $_GET['color'] with the value blue
  • $_GET['disposition'] with the value smiley

To use the variable and publish its value, PHP uses the variable name. An example:

<?php echo(htmlspecialchars($_GET['name'])) ?> would print the value Will.

However, if the page loads without setting up any $_GET['name'] variables (no URL name=____ parameter information), the above is likely to publish a warning on the web page and/or in the server error log.

To prevent that warning, we need to check if the $_GET['name'] variable is set before trying to publish its value.

<?php if(isset($_GET['name'])) { echo(htmlspecialchars($_GET['name'])); } ?>

Here is an example of how to use that to pre-fill a form field.

<input 
   type="text"
   name="person"
   value="<?php if(isset($_GET['name'])) { echo(htmlspecialchars($_GET['name'])); } ?>"
   style="width:100%">

The above (assuming we're staying with the URL parameter examples), would produce this.

<input 
   type="text"
   value="Will"
   name="person"
   style="width:100%">

In a moment, I'll provide an example form to illustrate pre-filling various types of form fields. But first, let's show how to handle a checkbox and a dropdown list option.

For checks and selections, the text checked="checked" or selected="selected" is published instead of values. They are published only on two conditions:

  1. The variable exists ($_GET['color'] for example).
  2. The value of the $_GET['color'] variable matches the value of the checkbox or option.

Here is an example.

<input 
   type="checkbox"
   name="color"
   value="blue"
   <?php if( isset($_GET['color']) and $_GET['color']=='blue' ) { echo('checked="checked"'); } ?>
   style="margin:0;">

The above PHP code is removed and replaced with checked="checked" only if $_GET['color'] exists and its value is blue. Both of those conditions must be true. Otherwise, the PHP code is still removed, but replaced with nothing.

The code for selecting a list item is similar to the above. The major change is that selected="selected" is published instead of checked="checked".

Now, lets put the examples together. Here are various types of form fields with the PHP pre-fill code.

<input 
   type="text"
   value="<?php if(isset($_GET['name'])) { echo(htmlspecialchars($_GET['name'])); } ?>"
   name="person1"
   style="width:100%">
<input 
   type="hidden"
   value="<?php if(isset($_GET['name'])) { echo(htmlspecialchars($_GET['name'])); } ?>"
   name="person2"
   style="width:100%">
<textarea 
   name="person3"
   style="width:100%">
<?php if(isset($_GET['name'])) { echo(htmlspecialchars($_GET['name'])); } ?>
</textarea>
<input 
   type="checkbox"
   name="color"
   value="blue"
   <?php if( isset($_GET['color']) and $_GET['color']=='blue' ) { echo('checked="checked"'); } ?>
   style="margin:0;"> Blue
<input 
   type="checkbox"
   name="color"
   value="red"
   <?php if( isset($_GET['color']) and $_GET['color']=='red' ) { echo('checked="checked"'); } ?>
   style="margin:0;"> Red
<select>
<option value="">- Select -</option>
<option value="blustery" <?php if( isset($_GET['disposition']) and $_GET['disposition']=='blustery' ) { echo('selected="selected"'); } ?>>Blustery</option>
<option value="frowny" <?php if( isset($_GET['disposition']) and $_GET['disposition']=='frowny' ) { echo('selected="selected"'); } ?>>Frowny</option>
<option value="smiley" <?php if( isset($_GET['disposition']) and $_GET['disposition']=='smiley' ) { echo('selected="selected"'); } ?>>Smiley</option>
<option value="sad" <?php if( isset($_GET['disposition']) and $_GET['disposition']=='sad' ) { echo('selected="selected"'); } ?>>Sad</option>

The above, assuming we're staying with the second URL parameter example at the beginning of this article, would produce this.

<input 
   type="text"
   value="Will"
   name="person1"
   style="width:100%">
<input 
   type="hidden"
   value="Will"
   name="person2"
   style="width:100%">
<textarea 
   name="person3"
   style="width:100%">
Will
</textarea>
<input 
   type="checkbox"
   name="color"
   value="blue"
   checked="checked"
   style="margin:0;"> Blue
<input 
   type="checkbox"
   name="color"
   value="red"
   
   style="margin:0;"> Red
<select>
<option value="">- Select -</option>
<option value="blustery" >Blustery</option>
<option value="frowny" >Frowny</option>
<option value="smiley" selected="selected">Smiley</option>
<option value="sad" >Sad</option>
</select>

As you can see by comparing the above two source code boxes, every PHP statement is removed. In its place is either something published or just a blank spot.

Unless overridden, the following form fields pretend this page was accessed with the values of the second URL parameter example. To override and affect how these fields are pre-filled, access this page with different parameter values for name, color, and/or disposition. See below the form fields for an example URL with different parameter values.

Blue Red

To override the default information in the above form and make it pre-fill other information, a URL like this can be used.

https://www.willmaster.com/library/manage-forms/pre-fill-form-with-url-parameters.php?name=Wanderer&color=red&disposition=frowny

This link contains the above URL. You can also make your own URL.

Any parameter information is assigned to PHP $_GET variables when a PHP web page is loaded. This is automatic, the way PHP works.

PHP can use those $_GET variables to pre-fill form fields, pre-check radio buttons or checkboxes, and pre-select select options.

(This article first appeared with an issue of the Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC