简体   繁体   中英

PHP _POST array is empty

I just don't see the problem. Need some new eyes to review it. Two files included test.php and test2.php to var_dump. Help!! Cut, Paste and Run - Thanks

test.php:
<?php
ini_set('error_reporting', E_ALL );
ini_set('display_errors', "1");
?>

<style type="text/css">
div.tabcontent{
  visibility: hidden;
  position:absolute;
  left:20px;
}
</style>

<script type="text/javascript">
  function switch_div(){
    val=document.form1.a_type.selectedIndex;
    if (val < 3){
      val="a0";
      data=document.getElementById("data_types").innerHTML+document.getElementById(val).innerHTML;
    }else if (val==3){
      val="a"+document.form1.a_type.selectedIndex;
      data=document.getElementById("data_types").innerHTML+document.getElementById(val).innerHTML;
    }else{
      val="a"+document.form1.a_type.selectedIndex;
      data=document.getElementById(val).innerHTML;
    }
    document.getElementById('anw_wksp').innerHTML=data;
  }
</script>

<html>
<body>
<form name="form1" action="test2.php" method="post" >
<table>
  <th >Enter Anwsers</th>
  <tr>
    <td>Anwser Type</td>
  </tr>
  <tr>
    <td><select name="a_type" onChange="switch_div()"/>
        <option value='radio'>radio</option>
        <option value='checkbox'>checkbox</option>
        <option value='select'>select</option>
        <option value='text'>text</option>
        <option value='textarea'>textarea</option>
      </select>
    </td>
  </tr>

      <div id="anw_wksp" style="border:1px solid gray; margin-bottom: 1em; padding: 10px">
      </div>
        <div id="data_types" class="tabcontent">
          <table>
          <th colspan="3">Data type</th>
            <tr><td>
            <input type="radio" name="a_data_type" value="text" selected>text<br>
            <input type="radio" name="a_data_type" value="int">int </td>
            </td>
          </tr>
          </table>
        </div>
        <div id="a0" class="tabcontent"> <!--radio/checkbox/select button-->
          <table>
            <th>Readable Anwser</th><th>DB Value</th><th>Default</th>
              <? $i=0;
                while($i < 10){
                  echo "<tr><td><input type=\"text\" name=\"a_r_$i\" /></td>
                      <td><input type=\"text\" name=\"a_db_$i\" /></td>
                       <td><input type=\"radio\" name=\"default\" value=\"$i\" /></td></tr>";
                  $i++;
                }
              ?>
          </table>
        </div>

        <div id="a3" class="tabcontent">
          <table>
            <th>Readable Anwser</th><th>DB Value</th><th>Default</th>
            <tr><td><input type="text" name="a_r_text"></td>
              <td><input type="text" name="a_db_text"></td>
              <td><input type="text" name="default_text" ></td></tr>
          </table>
        </div>

        <div id="a4" class="tabcontent">
          <table>
            <th>Readable Anwser</th><th>Default</th>
            <tr><td><input type="text" name="a_r_textarea"></td>
              <td><textarea name="default_textarea"  rows="5" cols="30"></textarea></td></tr>
          </table>
        </div>
    </td>
  </tr>
</td>
</tr>
  <tr>
  <td><input type="submit" value="Enter" name="submit">
  </td>
</table>
</form>
</body>
</html>


<script language="javascript">switch_div();</script>
-------------------------------------------------------------------

test2.php:

<?php

$data = file_get_contents('php://input');

if(empty($_SERVER['CONTENT_TYPE'])){

     $type = "application/x-www-form-urlencoded";

     $_SERVER['CONTENT_TYPE'] = $type;

}

print "_POST = <br />";
var_dump($_POST);
print "<br />";
print " DATA = <br />";
var_dump($data);

?>
  1. You don't have a <head>
  2. You can't have <script> tags outside of your <html> . Perhaps you want external JS files ?
  3. You can't have <style> tags outside of your <html> . Perhaps you want external CSS files ?
  4. Your scripts/styles, or their external <link> 's, should probably be inside your <head>
  5. You can't have <th> or <td> outside of a <tr> . Proper example .
  6. You don't declare a doctype .
  7. You can't have <div> 's randomly between table rows.
  8. Your method for echo ing <tr> 's is, well, sloppy...

Instead of:

<? $i=0;
            while($i < 10){
              echo "<tr><td><input type=\"text\" name=\"a_r_$i\" /></td>
                  <td><input type=\"text\" name=\"a_db_$i\" /></td>
                   <td><input type=\"radio\" name=\"default\" value=\"$i\" /></td></tr>";
              $i++;
            }
          ?>

Consider something like:

<? for($i = 0; $i < 10; $i++) { ?>
    <tr>
        <td><input type="text" name="a_r_<?=$i?>" /></td>
        <td><input type="text" name="a_db_<?=$i?>" /></td>
        <td><input type="radio" name="default" value="<?=$i?>" /></td>
    </tr>
<? } ?>

Finally, to try debugging your problem, use a tool like Firebug for Chrome/Firefox to ensure you're submitting POST data as expected.

I know this is a lame answer but just want to throw it out there to make sure we aren't looking over the simplest things. Did you check the form tag in the html to make sure it is set to POST?

I would recommend checking that the request method is in fact a post on the test2.php script.

<?php
if (strtolower($_SERVER["REQUEST_METHOD"]) == "post") {
    $data = file_get_contents('php://input');

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

    print "_POST = <br />";
    var_dump($_POST);
    print "<br />";
    print " DATA = <br />";
    var_dump($data);
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM