I need a bit of help understanding what I should do here.

I have an .php page called view that has the following drop down form code in it.

        $sql = "SELECT DISTINCT dept FROM sw_lic WHERE dept != ''"; // (1)
        $result2 = mysql_query($sql);
        echo "<form action='NB_sw_lic/filter_view.php?$nt' method='POST'>"; // (2)
        echo "<select name='dept'>"; // (3)
        while($nt=mysql_fetch_array($result2))
        {
        echo "<option value=$nt[dept]>$nt[dept]</option>";
        }
        echo "</select>";
        echo "<input type='submit' name='submit' value='Submit'>";

I This automatically populates the dropdown menu with values from the MySQL database. What I would like to have happen is that when the user selects an option and clicks the select button, the option they selected is transmitted to another page called filtered_View.php that would then populate a table with all records that match that previous pages selection. Here is the code for filter_view.php

// get results from database
        $result = mysql_query("SELECT * FROM sw_lic WHERE dept = $nt") 
                or die(mysql_error());  

        // display data in table
        echo "<table border='1' cellpadding='10' background-color='#000066' color='#FFFFFF'>";
        echo "<tr> <th>Username</th> <th>System Name</th> <th>Dept.</th> <th>Software Name</th> <th>Software Ver.</th> <th>Software Origin</th> <th>LIC Key</th> <th></th> <th></th></tr>";

        // loop through results of database query, displaying them in the table
        while($row = mysql_fetch_array( $result )) {

                // echo out the contents of each row into a table
                echo "<tr>";

                echo '<td>' . $row['username'] . '</td>';
                echo '<td>' . $row['sysname'] . '</td>';
                echo '<td>' . $row['dept'] . '</td>';
                echo '<td>' . $row['sw_name'] . '</td>';
                echo '<td>' . $row['sw_ver'] . '</td>';
                echo '<td>' . $row['origin'] . '</td>';
                echo '<td>' . $row['sw_key'] . '</td>';
                echo '<td><a href="edit.php?sw_id=' . $row['sw_id'] . '">Edit</a></td>';
                echo '<td><a href="delete.php?sw_id=' . $row['sw_id'] . '">Delete</a></td>';
                echo "</tr>"; 
        }
        // close table>
        echo "</table>";

I have no clue how to pass the variable from the dropdown menu to this page, can anyone help me out?

Thanks soo much in advance for your assistance.

Recommended Answers

All 4 Replies

Hello, i redid your coding and took out the $nt in the action and added a name tag to ur option. Also, just noting, you hae to have a ? mark when passing variables through a URL.

 <?php
 $sql = "SELECT DISTINCT dept FROM sw_lic WHERE dept != ''"; // (1)
 $result2 = mysql_query($sql);
 ?>
 <form action="NB_sw_lic/filter_view.php" method="POST"> // (2)
 <select name="dept"> // (3)
 <?php
 while($nt=mysql_fetch_array($result2))
 {
 echo "<option name='dept' value=$nt[dept]>$nt[dept]</option>";
 }
 ?>
 </select>
 <input type="submit" name="submit" value="Submit">

Now when the form gets submitted filter_view.php, use $_POST['dept'] to get the value.

$nt = $_POST["dept"];
$result = mysql_query("SELECT * FROM sw_lic WHERE dept = $nt")

Let me know if this works

commented: Solved my question! +0

$nt = $_POST["dept"];
$result = mysql_query("SELECT * FROM sw_lic WHERE dept = $nt")

That seemed to do it perfectly. I had to tweak a few things to get it just right, such as the action and some formatting preferences, but it definately works. Now I understand it and can re-implement similar functions later in the application.

Thanks again so much for the assistance!!!

OK, I would liek to take this a step further and require assistance once again. I implemented the field above and it works great, but what I wanted to do was add another drop down field that functions similarly. Just as the nt variable is passed so I would like the st variable passed to the .php file to be included in the select command.

Here is the code for my drop down menus.

<?php
       $sql2 = "SELECT DISTINCT dept FROM sw_lic WHERE dept != ''";
       $sql3 = "SELECT DISTINCT sw_name FROM sw_lic WHERE sw_name != ''";
       $result2 = mysql_query($sql2);
       $result3 = mysql_query($sql3);
?>
       <form action="filter_view.php" method="POST">
       <select name="dept">
<?php
       while($nt=mysql_fetch_array($result2))
       {
       echo "<option name='dept' value=$nt[dept]>$nt[dept]</option>";
       }
?>
       </select>
       <select name="sw_name">
<?php
       while($st=mysql_fetch_array($result3))
       {
       echo "<option name='sw_name' value=$st[sw_name]>$st[sw_name]</option>";
       }
?>
<input type="submit" name="submit" value="Submit">

and here is my php script the variables should be passed to.

<?php

        // connect to the database
        include('connect-db.php');

        // get results from database
        $nt = $_POST["dept"];
        $st = $_POST["sw_name"];
        $result = mysql_query("SELECT * FROM sw_lic WHERE dept='$nt' AND sw_name='$st'")
                or die(mysql_error());  

        // display data in table
        echo "<table border='1' cellpadding='10' background-color='#000066' color='#FFFFFF'>";
        echo "<tr> <th>Purchased for</th> <th>Installed On</th> <th>Dept.</th> <th>Software Name</th> <th>Software Ver.</th> <th>Software Origin</th> <th>LIC Key</th> <th></th> <th></th></tr>";

        // loop through results of database query, displaying them in the table
        while($row = mysql_fetch_array( $result )) {

        // echo out the contents of each row into a table
        echo "<tr>";

        echo '<td>' . $row['username'] . '</td>';
        echo '<td>' . $row['sysname'] . '</td>';
        echo '<td>' . $row['dept'] . '</td>';
        echo '<td>' . $row['sw_name'] . '</td>';
        echo '<td>' . $row['sw_ver'] . '</td>';
        echo '<td>' . $row['origin'] . '</td>';
        echo '<td>' . $row['sw_key'] . '</td>';
        echo '<td><a href="edit.php?sw_id=' . $row['sw_id'] . '">Edit</a></td>';
        echo '<td><a href="delete.php?sw_id=' . $row['sw_id'] . '">Delete</a></td>';
        echo "</tr>"; 
        }
        // close table>
        echo "</table>";
 ?>

I understand that my code is probably the sloppiest around and that is largely due to me being self taught. If there is a better way to condense the code then by all means lay it on me, or link me to a resource that might shed some light on this subject.

Thanks in advance!

C.S.

So any errors yet?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.