0

Using a simple PHP form I am trying to generate a report only displaying the values selected in a dynamically populated select box.

   <div id="container">
      <div>
        <form action="report.php" method="post">
            <label>Select Devices: </label>
            <select name="devices[]" multiple>
                <?php
                    require 'config/db.php';
                    $pdo = Database::connect();
                    $sql = 'SELECT alarm_device_description FROM alarm_device';
                    foreach($pdo->query($sql) as $row) {
                        echo '<option>'. $row['alarm_device_description'] . '</option>';
                    }
                    Database::disconnect();
                ?>
            </select>
            <button type="submit">Get Report</button>
        </form>
      </div>
    </div>

Then I have a php file:

<div id="container">
  <div>
    <table>
      <thead>
        <tr>
          <th>Date</th>
          <th>Alarm Device Description</th>
        </tr>
      </thead>
      <tbody>
        <?php
          require 'config/db.php';
          $pdo = Database::connect();

          var_dump($_POST);
          echo '<hr />';

          $devices = $_POST['devices'];
          echo $devices . '<hr />';

          $sql = 'SELECT date, alarm_device_description
                  FROM alarm_log
                  NATURAL JOIN alarm_device
                  ORDER BY date ASC';

          print_r($_POST['devices']);
          echo '<hr />';

          foreach ($pdo->query($sql) as $row) {
            echo '<tr>';
            echo '<td>'. $row['date'] . '</td>';
            echo '<td>'. $row['alarm_device_description'] . '</td>';
            echo '</tr>';
          }
          Database::disconnect();
        ?>
      </tbody>
    </table>
  </div>
</div>

The report by default is supposed to generate all the alarm_devices, and that works fine. However, I need to be able to select for example: iPhone 5, and Nexus and only display those results.

I have tried to pull in the value as an array which in the POST tests on my reports.php page show - it does seem to grab the selected values.

The $devices = $_POST['devices'] only produces an empty array however.

I need to figure out how to modify my query to post the selected values, and by default all of the values.

Thanks in advance and I hope that makes sense. I am a front-end developer and not very well versed in PHP/MySQL

nykc
  • 175
  • 12
  • Have u tried using print_r($_POST) – coder Aug 07 '16 at 14:21
  • print_r($_POST['devices']); echo '
    '; This actually displays the selected values, I just can't get it to work in the MySQL query for some reason
    – nykc Aug 07 '16 at 14:23
  • Sidenote: You might like to look at this post [Difference between natural join and inner join](http://stackoverflow.com/questions/8696383/difference-between-natural-join-and-inner-join) – Funk Forty Niner Aug 07 '16 at 14:25

2 Answers2

1

<option> should have a value attribute which will be passed to server:

For example:

<select name="select_name">
    <option value="2">Text 2</option>
</select>

On server side after you select Text 2 option, it will be:

echo $_POST['select_name'];    // 2

In case with multiple:

<select name="select_name" multiple>
    <option value="2">Text 2</option>
    <option value="3">Text 3</option>
</select>

On server side after you select both options it will be:

print_r $_POST['select_name'];    // array(2,3);
print_r $_POST['select_name'];    // array(2); if you select one option

As you select alarm_device_description field - you can try:

foreach($pdo->query($sql) as $row) {
    echo '<option value="' . $row['alarm_device_description'] . '">'. $row['alarm_device_description'] . '</option>';
}
u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • The option is being generated by a MySQL query pulling in the values $row['alarm_device_description'] in the form.. How could I assign a value to the option from that? I can pull the data from the array, I just can't pull the selected values from the query at this point. – nykc Aug 07 '16 at 14:25
  • So this works for adding the value to the option of course. But in MySql query, It is still not pulling in the selected values. $devices = ($_POST['devices']; still echoes Array (with nothing else) and in my actual SQL query, I am having a hard time getting the correct syntax to only find those selected values. Thanks – nykc Aug 07 '16 at 16:21
0

You are missing the value attribute while generating the select box.

change

echo '<option>'. $row['alarm_device_description'] . '</option>';

to

echo '<option value="' . $row['alarm_device_description'] . '">' . $row['alarm_device_description'] . '</option>';

This way your $_POST['devices'] array would be poplulated with items

Zayn Ali
  • 4,765
  • 1
  • 30
  • 40