PHP, drop-down menu and search form, how to pass a variable to select a query

advertisements

I have spent over a week trying to get this working and I am stumped. So I have drop down menu with 5 categories, I have a search form next to it where you can enter a keyword to search against a category. I want to have 5 queries which use a keyword to search against each of the categories using php and SQL server. How can I change the query to match the drop down selection? I was thinking passing a variable to php using JavaScript, but I am not the best programmer, and this idea was too much for me. so category one selected will mean the keyword entered into the search form will run query one etc...

Thank you.

Here is my query:

$term = $_POST['term'];

                                                    $tsql = "select * from personnel where SURNAME like '%" . $term . "%'";

                                                    $stmt = sqlsrv_query( $conn, $tsql, array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));

                                                    if( $stmt === false)
                                                    {
                                                         echo "Error in query preparation/execution.\n";
                                                         die( print_r( sqlsrv_errors(), true));
                                                    }

                                                        while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
                                                    {
                                                             echo 'NUMBER: '.$row['SNUM'];
                                                        echo '<br/> Jobtitle: '.$row['JOBTITLE'];
                                                        echo '<br/> Last Name: '.$row['SURNAME'];
                                                        echo '<br/> Manager: '.$row['MANAGER'];
                                                        echo '<br/> Sex: '.$row['SEX'];
                                                        echo '<br/> Join Date: '.$row['JOINDATE'];
                                                        echo '<br/> Division: '.$row['DIV'];
                                                        echo '<br/> Salary: '.$row['SALARY'];
                                                        echo '<br/> Bonus: '.$row['BONUS'];
                                                        echo '<br/><br/>';
                                                    }

My form:

<form  action="searchBar.php" method="post">
                                                        <input id="searchstyle" type="text" name="term" />
                                                        <input id="searchbutton" type="submit" name="submit" value="Submit" />
                                                    </form>

And the drop down menu:

<form name="myform" id="dropdown" action="dropsearch.php" method="POST">
                                            <select name="dropdownmenu">
                                                <option value="0" selected="selected">Select a category</option>
                                                <option value="Men">Men</option>
                                                <option value="Women">Women</option>
                                                <option value="Youth">Youth</option>
                                                <option value="Accessories">Accessories</option>
                                                <option value="Teamwear">Teamware</option>
                                            </select>
</form>


You will have to create a form first of all with POST as method

Here is a good example:

<form name="myform" action="destination.php" method="POST">
<select name="mydropdown">
<option value="1">Fresh Milk</option>
<option value="2">Old Cheese</option>
<option value="3">Hot Bread</option>
</select>
</form>

In your destination.php file you will have to make a switch statement in order to see what value did you pass to your script.

switch ($_POST['mydropdown']) {
    case 1:
        $query = "first mysql query";
        break;
    case 2:
        $query = "2nd mysql query";
        break;
    case 3:
        $query = "3rd mysql query";
        break;
}

After that you will have to execute your query:

$result = mysql_query($query);
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

Note that this is a pretty simple way to do that and however you will have to adapt it to your needs.