0

I am writing a simple web page to demonstrate how to use forms in PHP. The page works fine when run on the university's web server; but, when I try to run it locally through IntelliJ 16, $_POST is always empty (but php://input does contain the correct post string). The other aspects of the demo work fine.

I've read many other posts about $_POST being empty; and, from what I can tell, those situations solutions don't apply here.

I'm assuming that there is a configuration problem with my local copy of php; but, I don't know what could be mis-configured that would cause $_GET to work, but not $_POST.

This is the relevant html form:

 <fieldset>
     <legend>POST form</legend>
     <form action="formDemo.php" method="post">
       "text" type input named <code>theFirstPost</code>
       <input type="text" name="theFirstPost" value="Value for first POST input"/><br/>    
       <input type="submit" name="postSubmit2" value="Submit Button 2"/>            
     </form>
 </fieldset>

The complete (working) example is here: http://www.cis.gvsu.edu/~kurmasz/StackExchange/formDemo.php

  • Adding <?php error_reporting(E_ALL); ?> produces no errors or warnings.
  • $_SERVER["HTTP_CONTENT_TYPE"] = "application/x-www-form-urlencoded"

What should I check next?


Complete PHP source:

<!-- This file demonstrates
    (1) How to set up a simple HTML form
    (2) How to use PHP to access the data
-->
<?php error_reporting(E_ALL); ?>
<html>
<head>
    <title>Form Demo</title>
    <style type="text/css">
        #get, #post {
            vertical-align: top;
        }

    </style>
</head>
<body>

<h1>Form Demo</h1>

<h2>Questions:</h2>

<ul>
    <li>Can you get data in both <code>$_GET</code> and <code>$_POST</code> at the same time? If so, how. If not, why
        not?
    </li>
    <li>What happens if you leave a text box empty?</li>
    <li>Can you "unselect" all the radio buttons? If so, how. If not, why not?</li>
    <li>Can you select "Fred" and "Barney" at the same time? Can you select "Barney" and "Trumpet" at the same time?
        What's the difference?
    </li>
    <li>What happens if you unselect all the check boxes?</li>
    <li>How does PHP treat checkboxes differently when doing a POST than when doing a GET?</li>
    <li>How can you tell which submit button was pushed?</li>
</ul>


<table>
    <tr>
        <td id="get">

            <fieldset>
                <legend>GET form</legend>
                <form action="formDemo.php" method="get">
                    "text" type input named <code>theTopOne</code>
                    <input type="text" name="theTopOne" value="Value for Top input"/><br/>

                    "text" type input named <code>theSecondOne</code>
                    <input type="text" name="theSecondOne" value="Value for Input #2"/><br/>

                    Radio buttons named "flintstones":
                    Fred <input type="radio" name="flintstones" value="iChooseFred" checked="checked"/>
                    Barney <input type="radio" name="flintstones" value="iChooseBarney">
                    Wilma <input type="radio" name="flintstones" value="iChooseWilma"></br>

                    Radio buttons named "instruments":
                    Saxophone <input type="radio" name="instrument" value="sax"/>
                    Trumpet <input type="radio" name="instrument" value="tpt">
                    Piano <input type="radio" name="instrument" value="piano" checked="checked"></br>

                    Checkboxes named "courses":
                    451 <input type="checkbox" name="course451" value="451"/>
                    452 <input type="checkbox" name="course452" value="452"/>
                    457 <input type="checkbox" name="course457" value="457"/><br/>

                    <input type="submit" name="getSubmit1" value="Submit Button 1"/>
                    <input type="submit" name="getSubmit2" value="Submit Button 2"/>

                </form>
            </fieldset>

            <table>
                <tr>
                    <th colspan=2>Contents of <code>$_GET</code></th>
                </tr>
                <tr>
                    <th>Key</th>
                    <th>Value</th>
                </tr>
                <?php
                foreach ($_GET as $key => $value) {
                    echo "<tr><td>$key</td><td>$value</td></tr>\n";
                }
                ?>
            </table>


        </td>
        <td id="post">

            <fieldset>
                <legend>POST form</legend>
                <form action="formDemo.php" method="post">
                    "text" type input named <code>theFirstPost</code>
                    <input type="text" name="theFirstPost" value="Value for first POST input"/><br/>
                    "text" type input named <code>theSecondPost</code>
                    <input type="text" name="theSecondPost" value="Value for Post #2"/><br/>
                    Radio buttons named "interest":
                    Cool <input type="radio" name="interest" value="itsCool" checked="checked"/>
                    OK <input type="radio" name="interest" value="itsOK">
                    Boring <input type="radio" name="interest" value="itsBoring"></br>

                    Radio buttons named "bestState":
                    Georgia <input type="radio" name="bestState" value="GA"/>
                    Michigan <input type="radio" name="bestState" value="MI">
                    California <input type="radio" name="bestState" value="CA" checked="checked"></br>

                    Checkboxes named "IScourses":
                    260 <input type="checkbox" name="IScourses[]" value="cis260" checked="checked"/>
                    333 <input type="checkbox" name="IScourses[]" value="cis333"/>
                    463 <input type="checkbox" name="IScourses[]" value="cis463"/><br/>

                    <input type="submit" name="postSubmit1" value="Submit Button 1"/>
                    <input type="submit" name="postSubmit2" value="Submit Button 2"/>

                </form>
            </fieldset>

            <table>
                <tr>
                    <th colspan=2>Contents of <code>$_POST</code></th>
                </tr>
                <tr>
                    <th>Key</th>
                    <th>Value</th>
                </tr>
                <?php
                foreach ($_POST as $key => $value) {
                    $printMe = $value;
                    if (is_array($value)) {
                        $printMe = "[" . implode($value, ", ") . "]";
                    }
                    echo "<tr><td>$key</td><td>$printMe</td></tr>\n";
                }
                ?>
            </table>


        </td>
</table>

<?php

$fp = fopen("php://input", 'r+');
echo stream_get_contents($fp);
?>

<hr>
<?php var_dump($_SERVER); ?>


</body>
</html>
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Zack
  • 6,232
  • 8
  • 38
  • 68
  • 5
    so where's the php for this? – Funk Forty Niner May 05 '16 at 13:57
  • What's the request content-type? `$_POST` only gets populated if the content-type is `application/x-www-form-urlencoded` or `multipart/form-data` – Charlotte Dunois May 05 '16 at 13:58
  • 1
    `PhpStorm`'s build-in server has not been functional for `post` for quite a while. Chances are you are dealing with the same issue. Only way to get this working is to run your own fully-qualified server (apache or other) locally on your dev box. See *[here](http://stackoverflow.com/questions/35290133/phpstorm-post-always-empty-solved).* – YvesLeBorg May 05 '16 at 13:58
  • this then http://php.net/manual/en/function.error-reporting.php your php is unknown and whether it's the root of the problem. – Funk Forty Niner May 05 '16 at 14:02
  • PHPStrom **is** IntelliJ plus some plugins less some other, less relevant features, but core is the same. – Marcin Orlowski May 05 '16 at 14:14
  • @MarcinOrlowski, then why was "IntelliJ" an inappropriate tag for this post? (Just curious.) – Zack May 05 '16 at 14:18
  • pooooossssttttt tttthhhheee sssooouuuurrrcccceee – I wrestled a bear once. May 05 '16 at 14:33
  • @Zack after a thought - it wasn't that bad. Yet `phpstorm` is better (and I restored this one instead) – Marcin Orlowski May 05 '16 at 16:41
  • What is the URL you see in the browser when this `$_POST` does not work? – LazyOne May 05 '16 at 17:14
  • IntelliJ/PHPStorm launches its own server. This is the URL: http://localhost:63343/CS371_SampleCode/formDemo.php – Zack May 05 '16 at 17:36
  • @Zack Yes, built-in web server has issues with handling POST requests ATM: already mentioned http://stackoverflow.com/q/35290133/783119 or http://stackoverflow.com/a/34787827/783119 . Use proper web server for now (it's better by many means) -- Apache/nginx/IIS – LazyOne May 06 '16 at 09:04

0 Answers0