0

i have a drop down list populated with values from database. What i am trying to achieve is assign a select statement to each value.

menu

Populate menu code

<?php
require 'conn.php';

$filter=mysql_query("select distinct fuel_type from car");
while($row = mysql_fetch_array($filter)) {
$options .="<option>" . $row['fuel_type'] . "</option>";

$menu="<form id='filter' name='filter' method='post' action=''>
  <p><label>Filter</label></p>
    <select name='filter' id='filter'>
      " . $options . "
    </select>
</form>";
}
echo $menu;
?>

Now this menu will be used to filter out car listing and will be placed on the sidebar cars

How can i assign each value from the drop down list a different select statement like; select * from car where fuel_type = (dropdown list value selected) without having to submit the form.

perhaps something like the following?

    if($_GET["filter"] == 'Petrol'){
$query=mysql_query("select * from car where fuel_type = 'Petrol'");
header("Location: cars.php");
}
3
  • You're gonna need Ajax for that Commented Apr 16, 2012 at 2:32
  • he doesnt need to use ajax, he can use a link and $_GET or $_POST Commented Apr 16, 2012 at 2:33
  • but with post i need to include a submit buttom perhaps _get but i thought that was to pass data through url links. im quite new to this Commented Apr 16, 2012 at 2:34

2 Answers 2

1

First, when you're building your option tag, assign the value that you'd like to filter on in the SQL query to the value attribute:

$options .="<option value='".$row['fuel_type']."'>" . $row['fuel_type'] . "</option>";

Second, assign unique ID's to your form and your select element. Then assign the form submit action to the select elements onchange event.

$menu="<form id='filterForm' name='filterForm' method='post' action=''>
  <p><label>Filter</label></p>
    <select name='filter' id='filter' onchange='document.getElementById("filterForm").submit()'>
      " . $options . "
    </select>
</form>";
}

Now when you change the select option, the form will be submitted and the option value that is selected will be populated as that value of the select element #filter.

On the php page that your form posts to, check for the value of the select element with the id #filter to build the query.

Sign up to request clarification or add additional context in comments.

Comments

0

you need javascript to open a php page with some parameters.

in php you receive those parameters from post or get super globals, then have a switch statement to perform a different query depending on the parameters you received.

in regards to 'without having a submit button' you can set up a listener in javascript to submit the form or trigger a function when the value changes. since its a dropdown a simple inline onmouseup="javascript:submitform();" will suffice

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.