Echo-ing names of text inputs

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Echo-ing names of text inputs

Post by Steveo31 »

Hey guys-

In a tut, it said to have a form.htm page with the following code:

Code: Select all

<form action="formhandler.php" method="post">
<input type="text" name="firstElement">
<input type="text" name="secondElement">
<input type="submit" value="Submit Form">
</form>
And then the php form would be:

Code: Select all

<?

echo ($firstElement);
echo ($secondElement);

?>
But I get an error that the vars are undefined. Whats going on?
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

The end PHP tag is not showing up.... but its there :)
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

This is what it should be.

FormHandler.php

Code: Select all

<?php
$first = $_Post['firstElement'];
$second = $_Post['secondElement'];
echo $first;
echo $second;
?>
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

Post by uberpolak »

Or simply

Code: Select all

<?php
echo $_POST['firstElement'];
echo $_POST['secondElement'];
?>
The important thing here, though, is that your tutorial is out of date. If you have similar problems doing what it suggests, refer to http://www.php.net/manual/en/language.v ... efined.php, and check out the "Superglobal Arrays" section.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

adding to the above posts

i think i should explain that if you are using the get method in your form you should use

Code: Select all

<?php 
echo $_GET['firstElement']; 
echo $_GET['secondElement']; 
?>
worth noting for beginners
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

Yeah. The reason I put them into variables is because often you will do things with the data, not just echo it. To do things with it, it is easier to put it into a shorter variable first.
Post Reply