Pass variable to input

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

Super simple code

<html>
<head>
<title>Test</title>
</head>
<body >
 <input type=text name="test1">
</body>
</html>

      

0


source to share


4 answers


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

Then there might be 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 is left blank in your example is because the PHP code does not put a value in the field. It's not automatic. Also, on most servers (but not always) a .html file will not be parsed by PHP.

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

+3


source


HTML is another file extension for the web server, it won't handle any operations unless you have done something to do so. Are you expecting to open http://site.com/foo.txt?contents=helloworld and see "helloworld" in a browser?



I suggest you google some tutorials (w3schools are usually good for this kind of thing) in PHP, then on "query strings" and how server side scripting works. You should get up and running quickly with basic site scripts.

+1


source


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

0


source


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

      

0


source







All Articles