无法在下拉框

时间:2017-02-04 00:56:51

标签: php post get echo

我花了一些时间试图让下拉框正常工作,从数据库中获取部门和位置的值。这工作正常,我添加了额外的日期字段,以便客户可以选择特定时期,地点和部门的记录。当我操作运行departmentReport.php的表单时,即使尝试回显每个值,页面也会返回空白,如下所示。

   <form id="departmentReport" action="departmentReport.php" method="get" onsubmit="#">
        <fieldset id="departmentReport">
          <h3>Department Report</h3>
            <br>
          <?php
          include 'includes/DbCon.php';
        $sql = "select department_name from departments";

          echo"<select name = 'departments' value=''>Department Name</option>";

          foreach ($conn->query($sql) as $row){
              echo "<option value=$row[department_name]>$row[department_name]</option>";}
          echo "</select>";

          ?>
          <br></br>
    <?php
          include 'includes/DbCon.php';
        $sql = "select `location_name` from `location`";

          echo  "<select name = 'location' value=''>Location Name</option>";

          foreach ($conn->query($sql) as $row){
              echo "<option value=$row[location_name]>$row[location_name]</option>";}
          echo "</select>";

          ?>
            <br></br><br>
                <label for="date">Date From<br></label>
                <input id="date1" type="date" name="dateF"
                       autofocus="true"/>
            <br>
            <br>
                <label for="date">Date To<br></label>
                <input id="date2" type="date" name="date2"
                       autofocus="true"/>
            <br>
            <br>
            <input type="submit" class="button" value="Submit">

On Submit departmentReport.php已被执行

<?php


include "../includes/dbCon.php";  //* CONNECTION TO DATABASE

$department = mysqli_real_escape_string($conn, $_POST['departments']);  //* CLEAN DATA, THIS DELETES ANY SPECIAL CHARACTERS THAT CAN BE USED FOR MALICIOUS CODE 
$location = mysqli_real_escape_string($conn, $_POST['location']);   //* CLEAN DATA, THIS DELETES ANY SPECIAL CHARACTERS THAT CAN BE USED FOR MALICIOUS CODE 
$date1 = mysqli_real_escape_string($conn, $_POST['date1']); //* CLEAN DATA, THIS DELETES ANY SPECIAL CHARACTERS THAT CAN BE USED FOR MALICIOUS CODE 
$date2 = mysqli_real_escape_string($conn, $_POST['date2']); //* CLEAN DATA, THIS DELETES ANY SPECIAL CHARACTERS THAT CAN BE USED FOR MALICIOUS CODE 

echo "$department";  
echo "$location";
echo "$date1";
echo "$date2";

这会带来以下结果。

department report form

result on submit

然而,在调整帖子时,我会看到我选择的值 http://localhost:8888/departmentReport.php?departments=Marketing&location=London&dateFrom=02%2F01%2F2017

我不确定为什么在回应它们时我看不到这些值!

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

检索帖子数据时的变量是错误的

选择部门HTML代码:

<select name = departments value=''>Department Name</option>

在选择表单中,名称是&#34; departments&#34;,但是当您在PHP中检索它们时,它是:

$_POST['departmant_name']

输入变量名称错误。它应该是:

$_POST['departments']

另外,请在选择名称

上加上引号
<select name = 'departments' value=''>Department Name</option>

所有其他变量也都错了。

您似乎输入了数据库列名,而不是表单输入名称。

编辑: 我还注意到,在你的表单中,形式是&#34;方法&#34;设置为&#34; GET&#34;而您的PHP代码正在使用&#34; POST&#34;