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 valueWill
$_GET['color']
with the valueblue
$_GET['disposition']
with the valuesmiley
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:
- The variable exists (
$_GET['color']
for example). - 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