Can't get data from submitted forms

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
User avatar
TimM
Forum Newbie
Posts: 7
Joined: Fri Dec 19, 2003 8:19 pm
Location: Canberra, Australia

Can't get data from submitted forms

Post by TimM »

Hi,

I've just started using PHP this morning, learning from the TechiWarehouse tutorial (http://www.techiwarehouse.com/PHP/PHP_Tutorial.html). I'm running PHP 4 on Microsoft Personal Web Server on a Win98 platform.

I was going fine..until I found out that I can't get the data submitted through HTML forms.

The HTML code I am using for testing purposes is as follows:

Code: Select all

<html> 
<body> 
<form action="result.php" method="get"> 
<input type="text" name="colour"> 
<input type="submit" name="submit"> 
</form> 
</body> 
</html>
Then in the result.php file I have the following:

Code: Select all

<html> 
<body> 
<?php 
echo "The colour is "; 
echo $colour 
?> 
</body> 
</html>
I fill out the form with a colour name, and submit it, but the output only shows this:
The colour is
(without the colour name that I submitted).

I have tried it using both form methods (GET and POST), but still no difference. Is there something I am missing in the PHP coding? In ASP you can't reference a form value with a plain variable, you either need to use request.form() or request.querystring(), but I assuming you don't need to do that in PHP because of the examples given in the tutorial mentioned above.

Is anyone able to shed some light on this?

Thanks :)
- Tim
User avatar
William
Forum Contributor
Posts: 332
Joined: Sat Oct 25, 2003 4:03 am
Location: New York City

Post by William »

Try:
Code:

Code: Select all

<html> 
<body> 
<form action="result.php" method="post"> 
<input type="text" name="colour"> 
<input type="submit" name="submit"> 
</form> 
</body> 
</html>
result.php:

Code: Select all

<html> 
<body> 
<?php 
echo "The colour is $colour"; 
?> 
</body> 
</html>
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

I presume your using the most up to date PHP version...

Try using the proper superglobal array for the variable. In this case you are sending the page a GET method, so use the $_GET superglobal array.

Code: Select all

<?php
if (isset($_GET['colour']))
{
   $colour = $_GET['colour'];
}
echo $colour;
?>
This should display the proper text.

Reference material: superglobals, [php_man]isset[/php_man](), [php_man]array[/php_man](), if()
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Just expanding on Sami's post a little, if you are using the line

Code: Select all

&lt;form action="result.php" method="get"&gt;
in your form then the result.php page will have the URL of http://www.example.com/result.php?colour=green where green is what ever the user inputted in the text box. If you do not want this to show up in the URL, then use this:

Code: Select all

&lt;form action="result.php" method="post"&gt;
If you use post you will have to change the result.php code to:

Code: Select all

<?php
if (isset($_POST['colour']))  //if something was entered into the colour box
{
   $colour = $_POST['colour'];  // make $colour = to whatever was entered
   echo $colour;  //print out the result
}
else
{
   echo "You did not input a colour, moron!";  //if $_POST['colour'] is empty, berate the user
}

?>
References:
$_POST
else
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

Post by uberpolak »

Damn outdated tutorials. I say someone should syndicate the tutorials on PHPDN. Of course I'd like a cut of the royalties for suggesting the idea.

Note: I'll accept payment in <span style='color:red;text-decoration:blink' title='Alert a moderator!'>grilled spam</span>.
User avatar
TimM
Forum Newbie
Posts: 7
Joined: Fri Dec 19, 2003 8:19 pm
Location: Canberra, Australia

Post by TimM »

Hi all,

Sorry I have taken so long to reply, I forgot to "watch this topic" so that I get an e-mail when anyone replies - and I just suddenly remembered about it today :)

Anyway, thanks for your replies, but I did find out somewhere else anyway. The superglobal array example that Sami gave was the one that I found elsewhere, and it worked fine - I've been using it with both get and post methods.

Thanks again,
Cheers
Tim
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

uberpolak wrote: Note: I'll accept payment in <span style='color:red;text-decoration:blink' title='Alert a moderator!'>grilled spam</span>.
PM Jason about that, he's a gazillionaire ;)

-Nay
Post Reply