Log in

View Full Version : Need Help with VERY Simple Forms and $_GET and $_REQUEST variables



MrRSMan
07-23-2007, 09:20 PM
Yep, it's me again.

I've just started learning PHP, and I've already come across something that I can't make work...it should be pretty simple, or at least that's what this tutorial makes out!

---

I am trying to create a simple 2-field form where you input your name and age. You click submit and on the next screen is displayed;


"Welcome (*name inputed*).
You are (*age inputed*) years old!"

---

I have been given the following code add to a page;


<form action="welcome.php" method="get">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

Then, I'm told, when the user clicks the "Submit" button, the URL sent could look something like this;


http://www.w3schools.com/welcome.php?name=Peter&age=37

The "welcome.php" file can now use the $_GET variable to catch the form data;


Welcome <?php echo $_GET["name"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!

---

I have tried renaming the page with .HTML and .PHP extensions, and adding <?php ... ?> around the whole page, and just the code...and so on. The best I have managed to achieve so far is have the form working fine, and be redirected to the correct URL like the one shown above, but alas, all that shows is the empty "Welcome", etc. template with the actual name and age missing!

Please, try to sort me out- I suspect it's just me being dim. Oh, and when you reply, talk to me as if I'm a 5-year-old :o.

MrRSMan.

djr33
07-24-2007, 01:15 AM
All of the code looks correct to me, so you must be placing it incorrectly.

Using GET is acceptable, but doesn't seem needed, since you don't want this in the address bar. Using $_POST[] and method="post" will hide the data when sent, rather than placing it in the address bar. This isn't why it isn't working, but just a suggestion.

I would suggest manually typing the URL to see if that works (for this, you would need to leave it as GET, for the time being, since you can't directly enter POST data). If so, it's the second page. If not, it's the first.