Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GET works, POST doesn't...why?

Status
Not open for further replies.

dbrb2

Instructor
Jul 19, 2004
121
GB
Hi,

I've been playing around with PHP after a bit of an break. I am using PHP version 5.2, with Apache server version 2.2.

Everything seemed fine, but when playing around with using POST and GET to fill variables in a php script GET works fine, but POST does not. This seems odd... any suggestions?
Code:
<html>
<form action="post.php" method="post">
Test: <input type="text" name="test"><br>
<input type="Submit">
</form>
</html>
And...
Code:
<?php
$input=$_POST['test'];
echo $query;
?>
Does not work. However...

This...
Code:
<html>
<form action="get.php" method="get">
Test: <input type="text" name="test"><br>
<input type="Submit">
</form>
</html>
And this
Code:
<?php
$input=$_POST['test'];
echo $query;
?>
Does work. I can't see why...

Any suggestions? I suspect it is something obvious!
 
Why do you use
Code:
$input=$_POST['test'];
echo $query;
instead of
Code:
$input=$_POST['test'];
echo $input;
? If you want to display the contents of the variable, you need to use the correct variable name.

Lee
 
Oops... I typed the code samples out incorrectly. As you have both suggested, I should have put $test instead of $query, and
in the second case used $_GET instead of $POST.

However, in the actual code I did both of these things.... (but somehow managed to mess it up when posting here!)




 
Another prime example where copying and pasting your code is SO important. Please copy and paste your code here so we can see what we're REALLY working with.

Lee
 
Try outputting your entire $_POST and $_GET arrays.

Post usually works better with long data strings than get because the browser can lop off the url after so many characters. Post maintains all the data.

 
Assuming your code is correct, your webserver is likely misconfigured. Not likely, but it's what it would take to have this happen.

As suggested by Miros, just dump them both to screen...
Code:
echo '<pre>GET:';
print_r($_GET);
echo '<hr>POST:';
print_r($_POST);

Then you'll at least rule out the typo possibilities.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top