Why the Answer doesn't show up?

I am a new kid in propramming and self-learning. Can anyone tell why “the answer doesn’t show up”.

<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>
<html>
<head></head>
<body>
Your favorite author is:
<?php
echo $Author;
?>
</body>
</html>

This time the web page shown up as:

Your favorite author is:

BUT, WITHOUT the name I input on the HTML form.

However, the brower URL got the correct answer.

The author of the book says it works, but I couldn’t

Please advise!!!

Admin Edit: Added the [code] tags to segment Code sections from rest of post. Please see http://phphelp.com/guidelines.php for posting guidelines.

[php]echo $_GET[‘Author’];[/php]

Superglobal variables are probably disabled (and with good reason). See also the link in my signature.

On a sidenote: it’s better to use the POST method for forms.

Ok… You did set the variable correctly, however, you did not get the variable correctly.

Example:

Say your url looks like - http://www.mysite.com/index.php?Author=John

In order to return author you need -
[php]<?php
$author = $_GET[‘Author’];

echo $author; //OUTPUTS: John

//or you could simply go -
echo $_GET[‘Author’]; //OUTPUTS: John
?>
[/php]

Now, that solves your current problem, but I would be inclined to ask how come you are using the get method? When dealing with forms, the post method is primarly used.

Example:
[php]<?php

Who is your favourite author?

Your favorite author is: <?php $author = $_POST['Author'];

echo $author; //OUTPUTS: John

echo $_POST[‘Author’]; //OUTPUTS: John
?>

[/php]

@Ragster: ugh, you make my reply look scrawny :P I should put more effort into giving examples and explaining stuff -.-

I think I was typing mine up while you had submitted yours! :)

Otherwise I might not have replied if I had seen yours already out there.

Well I believe the answer to the GET versus POST is because it’s an example in a book. Unless I miss my guess, the book is Beginning PHP4 from WROX press

I am pretty sure I have seen that exact example before and had the same problem.

It’s also used to demonstrate the concept of the GET method versus the POST method (Which would be coming up soon in the book).

Good call! :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service