Christopher Christopher -4 years ago 141
HTML Question

How to automatically fill form fields

How can you make a form be able to fill a field with the url?

Example: if i have two fields, username and password, and my form is located at form.html how can I make

form.html?username=example
automatically fill in "example" in the username field.

Answer Source

The form would have to read variables from the URL and parse them, and then repost those values into a field. For instance, with PHP, it would be:

<?php $sName = $_GET['name']; ?>
...some HTML goes here...
<input type="text" id="name" name="name" value="<?= $sName ?>" size="60" />

This can also be done in jQuery by using location.href value to get the full URL, then split the URL into parts a few times with the split() function, and then use $('#name').val(sName) in jQuery to post the value into that field.

However, there are several security implications you have to consider. It is no longer advisable any more to take a raw GET value without running it through some XSS prevention steps:

http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet

Here's something that malicious people may use against a site that works with raw GET values:

http://ha.ckers.org/xss.html

So beware.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download