The Digital Ninja
The Digital Ninja

Reputation: 1120

Pass variable into an input

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>

Upvotes: 0

Views: 399

Answers (4)

Sydius
Sydius

Reputation: 14257

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.

Upvotes: 3

jmucchiello
jmucchiello

Reputation: 18984

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

Upvotes: 0

recursive
recursive

Reputation: 86084

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

Upvotes: 0

annakata
annakata

Reputation: 75824

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.

Upvotes: 1

Related Questions