Page 1 of 1

Echo-ing names of text inputs

Posted: Sat Feb 14, 2004 1:42 pm
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?

Posted: Sat Feb 14, 2004 1:43 pm
by Steveo31
The end PHP tag is not showing up.... but its there :)

Posted: Sat Feb 14, 2004 1:45 pm
by Straterra
This is what it should be.

FormHandler.php

Code: Select all

<?php
$first = $_Post['firstElement'];
$second = $_Post['secondElement'];
echo $first;
echo $second;
?>

Posted: Sat Feb 14, 2004 1:53 pm
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.

Posted: Sat Feb 14, 2004 1:54 pm
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

Posted: Sat Feb 14, 2004 1:55 pm
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.