PHP Problem

  • StevCarls
  • Novice
  • Novice
  • User avatar
  • Posts: 27

Post 3+ Months Ago

I am learning PHP and trying to make HTML forms that work. However, it will not seem to work on my computer. Here is the code of the two pages. I am pretty sure that they are not the problem because these are downloaded code that I got from the website of the PHP book i am learning from.



Code: [ Select ]
This is text.html:

<HTML>
<HEAD>
</HEAD>
<BODY>
<FORM METHOD=GET ACTION="text.php">
Who is your favourite author?
<INPUT NAME="Author" TYPE="TEXT">
<BR>
<BR>
<INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>

This is text.php:

<HTML>
<HEAD>
</HEAD>
<BODY>
Your favorite author is:
<?php
echo $Author;
?>
</BODY>
</HTML>
  1. This is text.html:
  2. <HTML>
  3. <HEAD>
  4. </HEAD>
  5. <BODY>
  6. <FORM METHOD=GET ACTION="text.php">
  7. Who is your favourite author?
  8. <INPUT NAME="Author" TYPE="TEXT">
  9. <BR>
  10. <BR>
  11. <INPUT TYPE=SUBMIT>
  12. </FORM>
  13. </BODY>
  14. </HTML>
  15. This is text.php:
  16. <HTML>
  17. <HEAD>
  18. </HEAD>
  19. <BODY>
  20. Your favorite author is:
  21. <?php
  22. echo $Author;
  23. ?>
  24. </BODY>
  25. </HTML>




Here is the error message I get when I run this:

Quote:
Your favorite author is:
Notice: Undefined variable: Author in c:\inetpub\wwwroot\beginningphp4\form fun\text.php on line 7


I am really out of ideas on what to try to make this work because I am completely stumped. I am running this through IIS on my own computer. Any ideas...?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • b_heyer
  • Web Master
  • Web Master
  • User avatar
  • Posts: 4581
  • Loc: Maryland

Post 3+ Months Ago

try $_GET['Author'] for the variable.
  • b_heyer
  • Web Master
  • Web Master
  • User avatar
  • Posts: 4581
  • Loc: Maryland

Post 3+ Months Ago

$_REQUEST['Author'] Might also work.
  • Scorpius
  • Proficient
  • Proficient
  • User avatar
  • Posts: 401
  • Loc: Scorpion Hole

Post 3+ Months Ago

Also, the form method should not be GET, it should be POST.
  • StevCarls
  • Novice
  • Novice
  • User avatar
  • Posts: 27

Post 3+ Months Ago

b_heyer wrote:
$_REQUEST['Author'] Might also work.


I tried this and it worked. However, I tried the other one you suggested, $_GET['Author'], and it did not work. I am still wondering why that variable worked and the other things I tried didn't work. Also, can you explain what kind of variable that was exactly since I am still just starting to learn PHP. Thanks!
  • Scorpius
  • Proficient
  • Proficient
  • User avatar
  • Posts: 401
  • Loc: Scorpion Hole

Post 3+ Months Ago

You can look here:
http://us2.php.net/reserved.variables
for an explanation on why the variable worked.
  • rjstephens
  • Professor
  • Professor
  • User avatar
  • Posts: 774
  • Loc: Brisbane, Australia

Post 3+ Months Ago

in this case the correct method is GET, because nothing is being changed on the server. I can't find the page but the HTTP specification says that GET should be used when there is a point in bookmarking the page, and POST should be used when something on the server (other than a log file) is chagned.
  • jerryscs
  • Novice
  • Novice
  • User avatar
  • Posts: 27
  • Loc: Okinawa, Japan

Post 3+ Months Ago

$_GET['Author'] should be fine. This is what I did and it worked.

text.php
Code: [ Select ]
<HTML>
<HEAD>
</HEAD>
<BODY>
Your favorite author is:
<?php
$Author = $_GET['Author'];
echo $Author;
?>
</BODY>
</HTML>
  1. <HTML>
  2. <HEAD>
  3. </HEAD>
  4. <BODY>
  5. Your favorite author is:
  6. <?php
  7. $Author = $_GET['Author'];
  8. echo $Author;
  9. ?>
  10. </BODY>
  11. </HTML>



The following works also:

text.php
Code: [ Select ]
<HTML>
<HEAD>
</HEAD>
<BODY>
Your favorite author is:
<?php
echo $_GET['Author'];
?>
</BODY>
</HTML>
  1. <HTML>
  2. <HEAD>
  3. </HEAD>
  4. <BODY>
  5. Your favorite author is:
  6. <?php
  7. echo $_GET['Author'];
  8. ?>
  9. </BODY>
  10. </HTML>


Jerry
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

The reason you have to physically request the variables (or GET the variables) is because your PHP.ini file has register global variables set to false (or off).

the code you original tried was assuming it was set to true
PHP Code: [ Select ]
 
$_REQUEST['form variable Name']
 
 
  1.  
  2. $_REQUEST['form variable Name']
  3.  
  4.  

is sent when the form method is set to POST

PHP Code: [ Select ]
$_GET['variable name']

is a variable set in the query string (the code that get sent with the url request).

The lower versions of PHP use (which is compatible with the newer versions
PHP Code: [ Select ]
 
$HTTP_POST_VARS['form variable Name']
 
$HTTP_GET_VARS['variable Name']
 
 
  1.  
  2. $HTTP_POST_VARS['form variable Name']
  3.  
  4. $HTTP_GET_VARS['variable Name']
  5.  
  6.  


Just a question to everyone here.
Has anyone started fiddling with PHP 5?
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

$_REQUEST should work for either post or get. If $_REQUEST worked and $_GET didn't then perhaps you had already changed to method=post in your script?

Quote:
Request variables: $_REQUEST

Note: Introduced in 4.1.0. There is no equivalent array in earlier versions.

Note: Prior to PHP 4.3.0, $_FILES information was also included in $_REQUEST.

An associative array consisting of the contents of $_GET, $_POST, and $_COOKIE.


Quote:
HTTP POST variables: $_POST

Note: Introduced in 4.1.0. In earlier versions, use $HTTP_POST_VARS.

An associative array of variables passed to the current script via the HTTP POST method. Automatically global in any scope.



Quote:
HTTP GET variables: $_GET


Note: Introduced in 4.1.0. In earlier versions, use $HTTP_GET_VARS.

An associative array of variables passed to the current script via the HTTP GET method. Automatically global in any scope.


PHP manual reference

Post Information

  • Total Posts in this topic: 10 posts
  • Users browsing this forum: No registered users and 65 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.