0

I searched several other similar questions, but none of them helped me out.

I'm working on a form that will post to a db using PHP, but the post data is not being sent. $_POST is an empty array, and file_get_contents('php://input') returns an empty string.

Here is an MWE.

HTML:

<!DOCTYPE html>
<html>
<head></head>
<body>   
<div class="content">
    <form action="formprocessor.php" method="post">
        <label>Name: </label>
        <input name="name" type="text" />

        <label>Email: </label>
        <input name="email" type="text" />

        <input name="mySubmit" type="submit" value="Submit!" />
    </form>
</div>
</body>
</html>

formprocessor.php:

<?php

ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

var_dump(file_get_contents('php://input')); //returns string(0) ""
//or
var_dump($_POST); //returns array(0) {}

echo $_POST;
?>

The html and php are in separate files in the same directory. After submitting this form, I get this in the browser:

string(0) "" array(0) { } Array

Running PHP 5.3.28 on Windows Server 2008

I am confident there is nothing wrong with the code, but probably something to do with the server or php configuration. Any direction there would be helpful.

I have checked the configuration file. The only setting I was able to identify that is relevant is post_max_size, which has the value 8M.

Here's what the Network tab shows on Chrome's developer tools after submitting the form: enter image description here

CWill85
  • 23
  • 5
  • Not your issue, but do not `echo var_dump()`. Rather, `var_dump()` alone produces output always on its own and never returns a string to `echo`. – Michael Berkowski Jan 12 '18 at 23:27
  • The code as it is posted is all correct. Do you have other PHP forms functioning correctly on this server already? I'm trying to think of a configuration in either PHP or Win Server (where I'm very rusty) that would hinder POST. – Michael Berkowski Jan 12 '18 at 23:29
  • noted and corrected. Thank you. – CWill85 Jan 12 '18 at 23:29
  • This is the first form that uses PHP on this server. – CWill85 Jan 12 '18 at 23:30
  • error reporting shows you what? https://php.net/manual/en/function.error-reporting.php – Funk Forty Niner Jan 12 '18 at 23:31
  • you're also not doing anything with the POST arrays; echo them out and see what happens along with what I posted above. Is this one or two separate files? @CWill85 this comment was edited. – Funk Forty Niner Jan 12 '18 at 23:33
  • I just tested your code on a server, and it works fine , are you sure you are sending it to the correct php file? – Frosty Jan 12 '18 at 23:34
  • @FunkFortyNiner - Even with error_reporting(E_ALL); I get no errors. – CWill85 Jan 12 '18 at 23:35
  • `ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);` use that to catch "and" display. – Funk Forty Niner Jan 12 '18 at 23:36
  • Also tested the code exactly as above, and the form posts correctly to formprocessor.php with $_POST populated. – Michael Berkowski Jan 12 '18 at 23:37
  • 1
    Are the HTML and PHP files on the same folder? If not then you need to either do that or fix the action of the form to the right URL. – Raphael Cunha Jan 12 '18 at 23:37
  • @Innervisions, yes I am sure because it does echo back the empty $_POST and file_get_contents('php://input') – CWill85 Jan 12 '18 at 23:38
  • @FunkFortyNiner string(0) "" array(0) { } Notice: Undefined index: name in C:\inetpub\wwwroot\SODLR\formprocessor.php on line 11 Notice: Undefined index: email in C:\inetpub\wwwroot\SODLR\formprocessor.php on line 12 – CWill85 Jan 12 '18 at 23:40
  • Is it possible that your apache doesn't allow posts for some reason? – Frosty Jan 12 '18 at 23:40
  • @CWill85 this tells me that both your html and php are of the same file, correct? – Funk Forty Niner Jan 12 '18 at 23:41
  • @RBCunhaDesign yes, they are in the same folder. I suspect this is a configuration issue and not anything to do with my code. – CWill85 Jan 12 '18 at 23:42
  • @FunkFortyNiner No, they are separate. HTML is TestForm.html. PHP is formprocessor.php. – CWill85 Jan 12 '18 at 23:43
  • @CWill85 yeah I can see that now that you sent the error report. – Raphael Cunha Jan 12 '18 at 23:43
  • am also betting that you have the php above your html/form, since lines 11 and 12 or something you're not showing us. – Funk Forty Niner Jan 12 '18 at 23:44
  • consult the duplicate. – Funk Forty Niner Jan 12 '18 at 23:44
  • @Innervisions Not sure, I'm a bit of a newb with PHP. I checked the configuration file for PHP and the only relevant setting seems to be the max post size, which is set to 8m – CWill85 Jan 12 '18 at 23:45
  • @FunkFortyNiner No, they are in separate files. I posted the entirety of both files. There is no PHP in the HTML file. – CWill85 Jan 12 '18 at 23:47
  • @FunkFortyNiner - this is not a duplicate. $name and $email are undefined because the $_POST array is empty. If I take those 2 lines of code out, the error is gone and I'm still sitting here with an empty $_POST array. – CWill85 Jan 12 '18 at 23:51
  • Try `var_dump($_REQUEST);` – Grant Jan 13 '18 at 00:28
  • 1
    Check your request with the web developer tool in your browser to check whether anyting gets actualy posted – DarkBee Jan 13 '18 at 00:42
  • @Grant I added var_dump($_REQUEST); to the php file and the browser shows "array(0) { }" – CWill85 Jan 13 '18 at 01:03
  • @DarkBee - does my response to Grant address the same thing you were asking for? Thanks. – CWill85 Jan 13 '18 at 01:04
  • If you use chrome watch [here](https://stackoverflow.com/questions/4423061/view-http-headers-in-google-chrome), for firefox [here](https://stackoverflow.com/questions/33974595/in-firefox-how-do-i-see-http-request-headers-where-in-web-console) – DarkBee Jan 13 '18 at 01:20
  • @DarkBee - I added a screenshot showing the requests on the Network tab in Chrome. Does that tell you anything helpful? Thanks. – CWill85 Jan 13 '18 at 05:42
  • Could you post your `web.config`? It's strange your `POST` get redirected from `phpcontroller.php` to `phpcontroller` – DarkBee Jan 13 '18 at 10:53
  • @DarkBee I have a URL rewrite to mask .html and .php file extensions. Could that be causing problems? I'm going to move the form out of the root directory (where the rewrite rule doesn't apply) and try it there. – CWill85 Jan 15 '18 at 16:12

1 Answers1

0

Credit to DarkBee for pointing me in the right direction, but I figured out the problem. I have URL rewrite rules in place to mask .html and .php extensions. This was causing the post action to redirect from "formprocessor.php" to "formprocessor", and the post data seems to be lost at that point.

I put the following in a web.config in the subdirectory to disable the rules and now it works as expected.

<system.webServer>
<rewrite>
  <rules>
    <remove name="Redirect .php extension" />
    <remove name="hide .php extension" />
  </rules>
</rewrite>

In hindsight, this should have been really obvious. If anyone knows a cleaner way to avoid this conflict all together other than to disable the rewrite rules, that would be helpful. Hope this helps some other people out.

CWill85
  • 23
  • 5