views:

218

answers:

4

I made an html file called test.html then I navigated to it as "http://site.com/test.html?test1=a" but the textbox stayed blank. Why is this?

Super simple code

<html>
<head>
<title>Test</title>
</head>
<body >
 <input type=text name="test1">
</body>
</html>
+3  A: 

The file should be a PHP file, so test.php.

Then maybe something like this:

<html>
<head>
    <title>Test</title>
</head>
<body>
    <input type="text" name="test1" value="<?php echo htmlspecialchars($_GET['test1'], ENT_QUOTES); ?>">
</body>
</html>

The reason it stays blank in your example is because there is no PHP code to put the value into the field. It isn't automatic. Also, on most servers (but not always), a file with an .html extension will not be parsed by PHP.

Also, passing it to the htmlspecialchars function will help prevent cross-site scripting.

Sydius
I thought that the browser read the ?varibles tags that php just had an option to also read them in. Thanks for the insite .
The Digital Ninja
I guess I was sniped. But you really need the htmlspecialchars() around the $_GET['test'].
jmucchiello
-1 - need htmlspecialchars() as pointed out above.
Rob
as well as calling htmlspecialchars() you should check that the variable is also set (e.g. isset($_GET['test1']) ? $_GET['test1'] : '' )
Tom Haigh
I added the htmlspecialchars function. I did not add a check to see if the variable is set, to keep it simple.
Sydius
+1  A: 

HTML is just another file extension to a webserver, it's not going to do any kind of processing unless you've done something to make that so. Would you expect to open http://site.com/foo.txt?contents=helloworld and see "helloworld" in the browser?

I suggest you google up some tutorials (w3schools is usually good for this sort of thing) on PHP, then on "query strings" and how server side scripting works. You should be up and running with basic site scripting pretty fast.

annakata
A: 

It might be possible to read the URL via javascript and populate the textbox that way if you must use static html.

recursive
A: 
<html>
<head>
<title>Test</title>
</head>
<body >
 <input type=text name="test1" value="<?php echo htmlspecialchars($_GET['test1']);?>">
</body>
</html>
jmucchiello