$_POST empty on form submission

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
LordXzi
Forum Newbie
Posts: 7
Joined: Sat Feb 26, 2011 5:29 pm
Location: Reading, UK

$_POST empty on form submission

Post 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
gooney0
Forum Commoner
Posts: 56
Joined: Fri Jan 21, 2011 1:40 pm
Location: Reston, VA

Re: $_POST empty on form submission

Post 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."
LordXzi
Forum Newbie
Posts: 7
Joined: Sat Feb 26, 2011 5:29 pm
Location: Reading, UK

Re: $_POST empty on form submission

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: $_POST empty on form submission

Post 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)
LordXzi
Forum Newbie
Posts: 7
Joined: Sat Feb 26, 2011 5:29 pm
Location: Reading, UK

Re: $_POST empty on form submission

Post 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
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: $_POST empty on form submission

Post by Jonah Bron »

You tried the simple test on the server, right?
LordXzi
Forum Newbie
Posts: 7
Joined: Sat Feb 26, 2011 5:29 pm
Location: Reading, UK

Re: $_POST empty on form submission

Post 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.
Post Reply