I want to use the variables in a query string as the input for a form. Query string URL: http://localhost/add.php?name=site I want when the above page is called, the textbox which currently is named: name has the variable filled in e.g. "site" in this case. I was under the impression that as the variable name in the url is the same as the input name it would work, but it does not. Code for add.php <form method="post"> <table width="400" border="0" cellspacing="1" cellpadding="2"> <tr> <td width="100">Name</td> <td><input name="name" type="text" id="name" size="50"></td> </tr> <tr> <td width="100">URL</td> <td><input name="url" type="text" id="url" size="50"></td> </tr> <td><input name="add" type="submit" id="add" value="Add"></td> </table> </form> HTML:
It's a little more complicated than that, and won't happen automatically, you need something on the server side to feed the GET fields into the value attribute of the inputs. You don't say what server side language you have, or even whether you have one, but in PHP it would go something like this: <input name="name" type="text" id="name" value="<?=htmlentities($_GET['name'])?>" /> Code (markup):
AS 'blueroomhosting' said, you must use that code but let me describe more: as i noticed you used PHP as your server side language, in PHP we have an array called $_GET[]. when you pass some values through method get. you see that variable and their values on query string(a section after '?' mark) that each value on separated with '&' mark. after submitting your form with method get, rather than showing them on query string, php puts them on $_GET[] array. now according to structure of arrays in php for showing the value : <?php echo $_GET['name']; ?> PHP: Above statement prints the value of 'name' ( 'site' in this case). now if you want to print that on your text box as default value, just put that 'value' attribute on text box tag. as below : <?php echo '<input type="text" name="name" id="name" value="'.$_GET['name'].'" size="50">'; ?> PHP:
You are welcome and don't forget, use htmlentities() function to prevent any problem. it's necessary for security.