Page 1 of 1

(solved) Why is $_POST suddenly empty/not working at all?

Posted: Wed May 27, 2009 9:09 pm
by bschaeffer
I don't know what I could have possibly done, but has anybody ever had this problem:

I am passing form data from one page to another, and up until now the $_POST data was being sent without any problems. Now, even trying to echo out the $_POST data displays nothing. I've tried this basic code and I get nothing.

submit.html

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
</head>
<body>
<form method="post" action="echo.php">
    <p>
        <label for="url">URL</label>
        <input type="text" name="url" />
    </p>    
    <p>                  
        <input type="submit" name="submit" value="Submit" />                
    </p>        
</form>
</body>
</html>
echo.php

Code: Select all

<?php echo $_POST['url']; ?>
And when you hit submit, echo.php echoes absolutely nothing. I know this should work. It has been working. But now, all of a sudden, it just stopped carrying over the input. It's empty as sh.. WTF? WTFWTFWTF?

Has anybody ever had this problem?

Re: Why is $_POST suddenly empty/not working at all?

Posted: Wed May 27, 2009 9:47 pm
by califdon
I don't see anything wrong in your code, as shown. When I have these imponderable problems, it usually turns out that I was uploading the latest script file to the wrong server directory or something like that. Check the file modified timestamp on your files on the server and be sure you are actually running the file that you edited! You could also insert another plain echo in your echo.php file, again to make certain that you're running the file you think you are. One thing you might want to try is to install Tamper Data add-on for Firefox, with which you can view what is actually being sent from a form to the server, before it goes to the server. https://addons.mozilla.org/en-US/firefox/addon/966

Re: Why is $_POST suddenly empty/not working at all?

Posted: Wed May 27, 2009 10:20 pm
by bschaeffer
Yeah... I couldn't see anything wrong with it either, but I figured out what was going on.

I have a sub-folder called submit with a file inside it called index.php, and instead of setting the action to submit/index.php, I was just sending it to submit and that fixed it. I must have changed it without thinking.

Wondering why the index.php file wouldn't automatically pick up the POST data though? It was sending it to submit, running the functions, but just returning a bunch of "you forget to fill in...." errors.

Oh well...

Thanks for the quick response anyway.

Re: Why is $_POST suddenly empty/not working at all?

Posted: Wed May 27, 2009 11:04 pm
by anand
bschaeffer wrote:Yeah... I couldn't see anything wrong with it either, but I figured out what was going on.

I have a sub-folder called submit with a file inside it called index.php, and instead of setting the action to submit/index.php, I was just sending it to submit and that fixed it. I must have changed it without thinking.

Wondering why the index.php file wouldn't automatically pick up the POST data though? It was sending it to submit, running the functions, but just returning a bunch of "you forget to fill in...." errors.

Oh well...

Thanks for the quick response anyway.
code seems to be perfect. why don't you try"$_REQUEST['url']"???

POST doesnt work on many occassions.

Re: Why is $_POST suddenly empty/not working at all?

Posted: Sun May 31, 2009 12:15 pm
by bschaeffer
I figured out how to get it to work how I wanted it to.

I wanted it to go to http://mysite.com/submit/ instead of http://mysite.com/submit/index.php to keep the URLs clean, so I originally had the form set up like this, which doesn't work:

Code: Select all

<form method="post" action="submit" >
To get it to work, all I did was ad a '/' to the end of the action, like this:

Code: Select all

<form method="post" action="submit/" >
Now, it picks up the $_POST data just fine.

Thanks for the tips though. I didn't know that $_POST could be so quirky. Still, I feel like this was one of those little errors you make that will really help you out, experience wise.

Thanks again.

Re: Why is $_POST suddenly empty/not working at all?

Posted: Sun May 31, 2009 12:34 pm
by califdon
I don't know of any situation in which $_POST is quirky or doesn't work, when the syntax is correct. Like any other part of programming, it's easy to make mistakes, but when it is used in accordance with the manual, it is 100% reliable, in my experience.

Re: Why is $_POST suddenly empty/not working at all?

Posted: Wed Jun 03, 2009 10:38 am
by lmg
When $_POST doesn't work for me, I stick it in html and it seems to work fine (so far).

ie: printing variable v in an html page:

Code: Select all

<html>
Variable v= <?php echo $_POST['v']; ?>
</html>

Re: Why is $_POST suddenly empty/not working at all?

Posted: Wed Jun 03, 2009 11:59 am
by califdon
lmg wrote:When $_POST doesn't work for me, I stick it in html and it seems to work fine (so far).

ie: printing variable v in an html page:

Code: Select all

<html>
Variable v= <?php echo $_POST['v']; ?>
</html>
That really makes no sense at all.

Re: Why is $_POST suddenly empty/not working at all?

Posted: Wed Jun 03, 2009 1:33 pm
by Chalks
anand wrote:code seems to be perfect. why don't you try"$_REQUEST['url']"???
I would strongly recomend NOT using REQUEST. You should stick with GET and POST using GET for user set variables and POST for things that you don't want the user to mess with.

Re: Why is $_POST suddenly empty/not working at all?

Posted: Wed Jun 03, 2009 1:45 pm
by califdon
I agree with ~chalks. $_REQUEST is a bad habit to get into. Sooner or later you will encounter a name-space conflict. Let me repeat: THERE IS ABSOLUTELY NOTHING WRONG OR "FLAKY" ABOUT $_POST AND $_GET. They work exactly as advertised. If you are having trouble using them, it is because you have made an error. We all make errors and that's no disgrace, but there is no justification for not using a perfectly standard and reliable feature of a language just because you are having trouble using it correctly.

Re: Why is $_POST suddenly empty/not working at all?

Posted: Wed Jun 03, 2009 1:59 pm
by bschaeffer
Not trying to be a dick or anything, but what's not quirky about this...

As stated before, both the following forms submit to my "submit" sub folder which has an "index.php" file with other included files. They both run the classes and functions, but only one receives the $_POST data and the other doesn't.

This one receives the $_POST data and runs the other php files/functions/classes:

Code: Select all

<form method="post" action="submit/">
This one does not receive the $_POST data, but still runs the files/functions/classes:

Code: Select all

<form method="post" action="submit">
If both methods are running the functions, why is one not seeing the $_POST data and the other is. I don't understand.

Re: Why is $_POST suddenly empty/not working at all?

Posted: Wed Jun 03, 2009 2:02 pm
by Chalks
bschaeffer wrote:If both methods are running the functions, why is one not seeing the $_POST data and the other is. I don't understand.
Try using this snippet of code:

Code: Select all

<?php print_r($_POST); ?>
What does it show?

Re: Why is $_POST suddenly empty/not working at all?

Posted: Wed Jun 03, 2009 2:08 pm
by bschaeffer
Try using this snippet of code:

Code: Select all

<?php print_r($_POST); ?>
For the one that receives the $_POST data (the first example), it prints the $_POST array.

For the one that doesn't receive the $_POST data (the second), it prints nothing.

Re: Why is $_POST suddenly empty/not working at all?

Posted: Wed Jun 03, 2009 2:16 pm
by Chalks
bschaeffer wrote:This one receives the $_POST data and runs the other php files/functions/classes:

Code: Select all

<form method="post" action="submit/">
This one does not receive the $_POST data, but still runs the files/functions/classes:

Code: Select all

<form method="post" action="submit">
This isn't an error I've run into before, but the only difference between those code snippets (didn't notice this before) is the path. One is to submit/ and the other ommits the slash. Found out why it happens too:

the exact same problem
possible explanation

Looks like any url rewriting screws with the post action if there is/isn't a slash at the end. Hope this helps.


edit: so... maybe using htaccess to tell your server to use php5 or something like that would mess with it.
edit2: from the second link: "The answer is: Don't POST to a redirect."

Re: (solved) Why is $_POST suddenly empty/not working at all?

Posted: Wed Jun 03, 2009 3:52 pm
by califdon
That sounds like it's probably the answer. The only quirky part that I see is in the use of a non-specific URL as the action of the form. I have never before seen a form action that was neither a self-reference nor a specific script filename. I haven't looked it up to be able to say that that's wrong, but it's highly unusual and it's something I would never do, if only because I would like to know (and anyone working on my application would surely want to know) the filename of the script that will process my form data. I don't see this in any way to be an issue with $_POST.