Page 1 of 1

Can't get data from submitted forms

Posted: Fri Dec 19, 2003 8:19 pm
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

Posted: Fri Dec 19, 2003 10:05 pm
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>

Posted: Fri Dec 19, 2003 11:05 pm
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()

Posted: Fri Dec 19, 2003 11:55 pm
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

Posted: Sat Dec 20, 2003 11:46 am
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>.

Posted: Sun Jan 18, 2004 2:34 am
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

Posted: Sun Jan 18, 2004 3:04 am
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