Page 1 of 1

$_POST empty on form submission

Posted: Sat Feb 26, 2011 5:50 pm
by LordXzi
Hi,

I've been experiencing this strange issue with a couple of sites I've developed that whenever a form is submitted (any form on any of the sites) the $_POST variable appears to be empty. Which can be seen whenever I try to print it out. What appears to be happening is that the form is being submitted with a GET response rather than a POST, but I've definitely set the form method to POST (as shown) and this has only happened since my web host changed their server, and does not happen on my personal development box.

Code: Select all

<form id="catgeory_new" method="post" action="categoryform.php"
				onsubmit="return checkCategory('new');">
</form>
If I directly print out the results from the input with the following, I can see that the results are being sent but the $_POST variable is not being populated.

Code: Select all

$data = file_get_contents('php://input');
Another forum I found with my searching indicated the content type may be incorrect and suggested I tried the following, but this also didn't work.

Code: Select all

if(empty($_SERVER['CONTENT_TYPE'])){
       
     $type = "application/x-www-form-urlencoded";
     $_SERVER['CONTENT_TYPE'] = $type;

}
I've currently found a way around it, but will not work for file uploads unsurprisingly. It's a pretty hacky way of fixing it and I was hoping someone may be able to shed some light on what might be the root cause so I can solve that rather than curing the symptom!

Code: Select all

parse_str(file_get_contents('php://input'), $_POST);
Cheers!
Tom

Re: $_POST empty on form submission

Posted: Sat Feb 26, 2011 6:00 pm
by gooney0
This is most bizarre.

Here are a few ideas:

Create a dead simple form with no javascript and retest. You can also use a browser addon to debug your submit. Safari will do this nicely.

You could setup a page with:

phpinfo();

A see what the server settings are. Perhaps there is a setting there you can override.

Just to clarify... $_POST is an array not a variable. You'll want to use print_r($_POST) to see it's contents. If you just use print the output will be "array."

Re: $_POST empty on form submission

Posted: Sat Feb 26, 2011 6:08 pm
by LordXzi
Interesting... as suggested I tried a simple example and the value is being returned correctly.

Code: Select all

<?php
	print_r($_POST);
?>

<form name="test" method="post" action="test.php">
	<input type="submit" value="Test!" name="test_submit" />
</form>
As this is a full site there must be something else coming in between. I do use the .htaccess file for Apache to redirect using the RewriteEngine, anyone know of any reason that would upset the HTTP method?

Cheers
Tom

Re: $_POST empty on form submission

Posted: Sat Feb 26, 2011 6:18 pm
by Weirdan
LordXzi wrote: As this is a full site there must be something else coming in between. I do use the .htaccess file for Apache to redirect using the RewriteEngine, anyone know of any reason that would upset the HTTP method?
Only if you use non-local redirects (R flag in a rewrite rule)

Re: $_POST empty on form submission

Posted: Sun Feb 27, 2011 6:45 am
by LordXzi
Looks like I'm not using that:

Code: Select all

# start the rewrite engine
RewriteEngine On
RewriteBase /

# only redirect files that do not exist
RewriteCond %{REQUEST_FILENAME} !-f

# everything else to start.php
RewriteRule (.*) start.php [L]
I moved the previous test example into the folder where everything is redirected to and it still works after the redirection, shame that would have been a relatively easy fix.
Any other suggestions as to what point the method may be changing, I would post the code but there is a lot of stuff going on. I'm happy to post whatever may be needed to find a solution.

Cheers
Tom

Re: $_POST empty on form submission

Posted: Sun Feb 27, 2011 9:23 pm
by Jonah Bron
You tried the simple test on the server, right?

Re: $_POST empty on form submission

Posted: Mon Feb 28, 2011 1:05 pm
by LordXzi
If you're referring to the simple example I put in an earlier post I have tried that on the problem server in the redirected folder and that is working fine.