I have this drop down menu but I want to select its value and run a SQL-query with php. How do I do that?

            <select name="SelectMake">
                <option value="make">Make</option>
                <option value="toyota">Toyota</option>
                <option value="honda">Honda</option>
            </select>

Recommended Answers

All 7 Replies

// assuming you use method=post in your form
if(isset($_POST['SelectMake'])) {

    $make = mysql_real_escape_string($_POST['SelectMake']);

    $query = "SELECT * FROM cars WHERE make=$make";
    ...
}

I did this to show the data in a table but this didnt work. Can somebody help me.

<?php
            $dynamicList = "";
            if(isset($_POST['SelectMake'])) {
                $make = mysql_real_escape_string($_POST['SelectMake']);
                // Show data in a table

                $sql = mysql_query("SELECT * FROM inventory WHERE Make = '$make'"); 
                $productCount = mysql_num_rows($sql); // count the output amount

            if($productCount > 0){
                while($row = mysql_fetch_array($sql)){
                    $sku = $row["sku"];
        //          $make = $row["Make"];
                    $model = $row["Model"];
                    $year = $row["Year"];
                    $part = $row["Part"];
                    $dynamicList .= '<tr>
                    <td style="width: 120px"> ' . $sku . '</td>
                    <td style="width: 120px"> ' . $make . '</td>
                    <td style="width: 180px"> ' . $model . '</td>
                    <td style="width: 120px"> ' . $year . '</td>
                    <td style="width: 120px"> ' . $part . '</td>
                    </tr>';
                }   
            }else{
            $dynamicList = "We have no persons listed in our store yet";
            }

What is the output of your code (any error messages) and how does it differ from what you expect?

Put this code in the first line and post here the output:

die($_POST);

When I put that code I got this error:

Notice: Array to string conversion in C:\wamp\www\FilterView.php on line 47

The thing is that the data doesnt filter when I choose the dropdown menu. Nothing happens.
I also wonder if i don't need any submit? <input type="submit" value="submit" name="submit">. But I don't want any submit.

Sorry, my mistake, it should be die(print_r($_POST, 1)); but never mind. If you do not have submit button then you have to submit on change of the select element (drop down). Just add a onchange event:

<select name="SelectMake" onchange="this.form.submit()">
    <option value="make">Make</option>
    <option value="toyota">Toyota</option>
    <option value="honda">Honda</option>
</select>

But it might be good to have a submit button for those who do not have javascript or disabled it.

Thank you for the help. How can I make so nothing should happen when I click the first thing in the drop down menu.

The solution would be somewhere alog the following lines (untested). Write a javascript function, something like:

function submitTheForm() {
    // assign the form to a variable
    var theForm = document.getElementById('FormMake')
    // assign a selected option to a variable (0 -> first option)
    var selectedOption = document.getElementById('SelectMake').selectedIndex;
    // if any option but the first (index = 0) is selected submit the form
    if(selectedOption > 0) {
        theForm.submit();
    }
}

You will need to give your form and select ID attributes and call the above function on onchange event:

<form id="FormMake" onchange="submitTheForm()" method=...>
    <select id="SelectMake" name="SelectMake" onchange="this.form.submit()">
        <option value="make">Make</option>
        <option value="toyota">Toyota</option>
        <option value="honda">Honda</option>
    </select>
</form>

This is what I could quickly come up with. Hope it works OK for you.

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.