A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearPass variable into an input - page 1
User InfoPosts
Pass variable into an input#1
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

<body >
<input type=text name="test1">

posted date: 2008-12-11 14:02:00

Re: Pass variable into an input#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-11 14:02:01

Re: Pass variable into an input#3
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.

posted date: 2008-12-11 14:04:00

Re: Pass variable into an input#4
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.

posted date: 2008-12-11 14:11:00

Re: Pass variable into an input#5
It might be possible to read the URL via javascript and populate the textbox that way if you must use static html.

posted date: 2008-12-11 14:15:00

Re: Pass variable into an input#6
<html><head><title>Test</title></head><body > <input type=text name="test1" value="<?php echo htmlspecialchars($_GET['test1']);?>"></body></html>

posted date: 2008-12-11 14:17:00

Re: Pass variable into an input#7
I guess I was sniped. But you really need the htmlspecialchars() around the $_GET['test'].

posted date: 2008-12-11 14:18:00

Re: Pass variable into an input#8
-1 - need htmlspecialchars() as pointed out above.

posted date: 2008-12-11 14:39:00

Re: Pass variable into an input#9
as well as calling htmlspecialchars() you should check that the variable is also set (e.g. isset($_GET['test1']) ? $_GET['test1'] : '' )

posted date: 2008-12-12 02:20:00

Re: Pass variable into an input#10
I added the htmlspecialchars function. I did not add a check to see if the variable is set, to keep it simple.

posted date: 2008-12-13 20:29:00

select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.