0

So I am trying to sort information based the selected item in the combo box. I am familiar with parts of PHP but still learning.

I was able to fill the combo box with the 'Brand_Name' Column information from my DB.

This is my current output: image

It is not filtering though, it is just listing all items in the DB

See code below:

<?php
$hostname = "localhost";
$username = "root";
$password = "root";
$databaseName = "Clients";
$connect = mysqli_connect($hostname, $username, $password, $databaseName);
$queryDropdown = "SELECT * FROM `Brands` WHERE 1";
$result = mysqli_query($connect, $queryDropdown);
$queryGrid = "SELECT * FROM `Brands` ";
$SearchResult = mysqli_query($connect, $queryGrid);
?>
<html>
<head>
    <title>PokerPass</title>
      <link rel="stylesheet" type="text/css" href="styles/Main.css">
      <link rel="stylesheet" type="text/css" href="styles/ComboBox.css">
      <link rel="stylesheet" type="text/css" href="styles/TableGrid.css">
</head>
<body >
    <div id="Container" class="Container">
        <div id="Header" class="Header">

        </div>
        <div id="Body" class="Body">
            <div name="start" id="Filter_Bar" class="Filter_Bar">
                <select class="soflow" id="soflow" Size="1">
                  <?php while($row1 = mysqli_fetch_array($result)):; ?>
                  <option><?php echo $row1[1]; ?></option>
                <?php endwhile; ?>
                </select>
                <button class="Submit" ><span><strong>Search<strong></span></button>
                <!--<input class="Submit" type="submit" name="search" value="Search </span>">-->
            </div>
            <div id="Data_Grid" class="Data_Grid">
                  <table>
                    <tr>
                      <th>Server ID</th>
                      <th>Client</th>
                      <th>Operator</th>
                      <th>Username</th>
                      <th>Password</th>
                    </tr>
                    <?php while ($row = mysqli_fetch_array($SearchResult)):; ?>
                    <tr>
                      <td><?php echo $row['ServerID']; ?></td>
                      <td><?php echo $row['Brand_Name']; ?></td>
                      <td><?php echo $row['Operator_Name']; ?></td>
                      <td><?php echo $row['Username']; ?></td>
                      <td><?php echo $row['Password']; ?></td>
                    </tr>
                    <?php endwhile; ?>
                  </table>
            </div>
        </div>
        <div id="Footer" class="Footer">

        </div>
    </div>
</body>
</html>
Zypherr
  • 53
  • 10
  • So what did you try to make the filter work? You've tagged the question with JavaScript but don't actually show any JavaScript code. – Björn Tantau Sep 04 '17 at 11:47
  • You can use the onChange() function to get the value from the – Granny Sep 04 '17 at 12:04

2 Answers2

1

In your HTML code, your select element has no name attribute, so you won't be able to pick up its value when the form is submitted. Additionally, your option elements have no value attribute so even if the form was submitted and the select had a name, the value would be empty. See this guide.

Your select element isn't inside a form tag, which means that the page has no idea where to go when the button is pressed. You need a form that specifies the action (the URL you are going to) and the method (post/get). See this guide.

Finally, if you want PHP to know what your query was, you need to get it from the GET or POST parameters. Once you have the variable, you can make your SQL query dynamic - but be careful not to introduce an SQL injection vulnerability - validate the user input carefully (for instance, only allowing numbers as values in your select list) and/or use parameterized queries.

If you want the form to be submitted automatically when the dropdown list is changed, you can use a quick JS attribute to auto-submit the form.

BoffinBrain
  • 6,337
  • 6
  • 33
  • 59
0

You need to apply the filter value to your query, for your example you need to construct a new query depending on your selection.

"SELECT * FROM Brands WHERE Client=" + $selectedClient;

Edit: Here's a reference on how to pass a js variable to php: How to pass JavaScript variables to PHP?

anteAdamovic
  • 1,462
  • 12
  • 23