Page 1 of 1
Using GET to put a variable in a form field?
Posted: Thu Jul 16, 2009 8:58 am
by azylka
Hi. So I'm trying to use GET to put a name in a form. Here's my code.
send_name.php:
Code: Select all
<form action="alias.php" method="GET">First Name:
<br><input name="name" size="13">
<br><input type="submit" value="Continue..."></form>
and
form.php:
Code: Select all
<form action="create.php" method="GET">
First Name/ Email Address: <input name="name" value="<?php $name = $_GET['$name']; echo "$name" ?>">
<br>Link to Shorten: <input name="url" size="50"><br>
<input type="submit" value="Create Short URL"></form>
This simply doesn't work. I've tried echoing the entire form and replacing the name field's value with $name, but the field just stays blank. Can anyone help?
Cheers,
Alex
Re: Using GET to put a variable in a form field?
Posted: Thu Jul 16, 2009 9:05 am
by Jammerious
value="<?php echo $_GET['name']; ?>"
So it's name - not $name. Also there's no need to assign to something and then echo, just echo directly.
Re: Using GET to put a variable in a form field?
Posted: Thu Jul 16, 2009 9:09 am
by spider.nick
azylka wrote:Hi. So I'm trying to use GET to put a name in a form. Here's my code.
send_name.php:
Code: Select all
<form action="alias.php" method="GET">First Name:
<br><input name="name" size="13">
<br><input type="submit" value="Continue..."></form>
and
form.php:
Code: Select all
<form action="create.php" method="GET">
First Name/ Email Address: <input name="name" value="<?php $name = $_GET['$name']; echo "$name" ?>">
<br>Link to Shorten: <input name="url" size="50"><br>
<input type="submit" value="Create Short URL"></form>
This simply doesn't work. I've tried echoing the entire form and replacing the name field's value with $name, but the field just stays blank. Can anyone help?
Cheers,
Alex
Perhaps a better way:
Code: Select all
First Name/ Email Address: <input name="name" value="<?=$_GET['name']?>">
The <? is the short version of <?php, which your sever probably supports. The = is equivalent to 'echo'. The ?> closes the PHP, as always.
Try that, and let me know if it does not work.
Nick
Re: Using GET to put a variable in a form field?
Posted: Thu Jul 16, 2009 9:18 am
by azylka
Jammerious: Thanks! That was very helpful.
Spider.nick: That didn't work. Sorry. But it's the thought that counts.
If you want to see this tiny piece of code in action, visit:
http://www.zylka.us and use Alias from the sidebar.
Cheers,
Alex!
Re: Using GET to put a variable in a form field?
Posted: Thu Jul 16, 2009 9:44 am
by spider.nick
azylka wrote:Jammerious: Thanks! That was very helpful.
Spider.nick: That didn't work. Sorry. But it's the thought that counts.
If you want to see this tiny piece of code in action, visit:
http://www.zylka.us and use Alias from the sidebar.
Cheers,
Alex!
Sorry about that. I, copied your code, and overlooked that you had put the '$' in the $_GET array. If you want, my solution would still work.
Nick
Re: Using GET to put a variable in a form field?
Posted: Thu Jul 16, 2009 9:46 am
by spider.nick
azylka wrote:
If you want to see this tiny piece of code in action, visit:
http://www.zylka.us and use Alias from the sidebar.
You may want to do some checks, i.e. - if the user submits a blank form.
Nick
Re: Using GET to put a variable in a form field?
Posted: Thu Jul 16, 2009 10:24 am
by azylka
I'm working on that. Oh yeah, while I'm talking about security, I log the IP address of every user on my site, and then have a script to check the persons name and location.
Goodbye.
Re: Using GET to put a variable in a form field?
Posted: Thu Jul 16, 2009 7:40 pm
by Benjamin
spider.nick wrote:The <? is the short version of <?php, which your sever probably supports. The = is equivalent to 'echo'. The ?> closes the PHP, as always.
Using PHP's short tags is not good coding practice and shouldn't be recommended.
Re: Using GET to put a variable in a form field?
Posted: Fri Jul 17, 2009 10:57 am
by spider.nick
astions wrote:spider.nick wrote:The <? is the short version of <?php, which your sever probably supports. The = is equivalent to 'echo'. The ?> closes the PHP, as always.
Using PHP's short tags is not good coding practice and shouldn't be recommended.
A matter of preference, but thank you for stating it as fact ...
Nick
Re: Using GET to put a variable in a form field?
Posted: Fri Jul 17, 2009 2:07 pm
by Benjamin
spider.nick wrote:A matter of preference, but thank you for stating it as fact ...
Nick
No, it's not a matter of personal preference.
- It will be depreciated in PHP 6
- You'll get parse errors with embedded XML
- Your application is less portable because a majority of servers won't parse them
- Other programmers looking through your code will view you as a newb
Re: Using GET to put a variable in a form field?
Posted: Fri Jul 17, 2009 2:12 pm
by spider.nick
astions wrote:spider.nick wrote:A matter of preference, but thank you for stating it as fact ...
Nick
No, it's not a matter of personal preference.
- It will be depreciated in PHP 6
- You'll get parse errors with embedded XML
- Your application is less portable because a majority of servers won't parse them
- Other programmers looking through your code will view you as a newb
Interesting that php.net
does not mention that it is not a part of PHP 6.
Re: Using GET to put a variable in a form field?
Posted: Sat Jul 18, 2009 1:43 am
by McInfo
astions wrote:It will be depreciated in PHP 6
I am of the opinion that short open tags should not be used, but I would like to see some evidence for your claim. I have found evidence that the "asp_tags" setting will be deprecated or removed, but not "short_open_tag". I think that is a rumor that was started by confusion between ASP-style short tags (<%) and the short open tag (<?).
IBM.com - The Future of PHP - Nathan A. Good wrote:Microsoft Active Server Pages (ASP)-style tags — the shorter version of the PHP tags — are no longer supported. To make sure this is not an issue for your scripts, verify that you aren't using the <% or %> tags in your PHP files.
Source:
http://www.ibm.com/developerworks/opens ... hp-future/
Edit 1: The definitive answer, from the
PHP developers' meeting minutes, is that PHP 6 will remove "<%", but keep "<?".
Edit 2: The date on the document is November 2005...
Edit: This post was recovered from search engine cache.