I have a jQuery dropdown list that has two dropdowns. The first one is a list of cars and the second one shows the car's models. This is all working fine. Now I would like to use the selection selected by the user to search a database (MySQL) and find a specific element within the database. This just gives me the else within my code and once it gave me a undefined variable error message.
How can I code this so that I can access the variables and display a search result? Here is my code:
This is just an example of the jQuery I'm using - I'm actually using cars but this shows cities:
<select class="select" id="province" onchange="filterCity();">
<option value="1">RM</option>
<option value="2">FI</option>
</select>
<select class="select" id="city" disabled>
<option data-province="RM" value="1">ROMA</option>
<option data-province="RM" value="2">ANGUILLARA SABAZIA</option>
<option data-province="FI" value="3">FIRENZE</option>
<option data-province="FI" value="4">PONTASSIEVE</option>
</select>
<span id="option-container" style="visibility: hidden; position:absolute;"></span>
<script>
function filterCity(){
var province = $("#province").find('option:selected').text(); // stores province
$("#option-container").children().appendTo("#city"); // moves <option> contained in #option-container back to their <select>
var toMove = $("#city").children("[data-province!='"+province+"']"); // selects city elements to move out
toMove.appendTo("#option-container"); // moves city elements in #option-container
$("#city").removeAttr("disabled"); // enables select
};
</script>
And here is the code where I'm trying to access the searched for variable (this code is echoing wrong (the else)): I used the name="car" in the second select:
if (isset($_POST['car'])) {
$_SESSION['car'] = $_POST['car'];
$car = $_SESSION['car'];
}
function find_tire(){
if (isset($_POST['car'])) {
$_SESSION['car'] = $_POST['car'];
$car = $_SESSION['car'];
}
global $db;
if (isset($car)) {
$query = $db->prepare("SELECT idtires FROM vehicle WHERE idcarmodel = $car");
$query->execute();
$tire = $query->fetchAll();
var_dump($query);}
if (isset($tire)) {
echo "<ul>";
foreach ($tire as $name) {
echo "<li id='tiresearch'>";
echo "Tire Size is Available: " . $name ['idtires'];
echo "</li>";
}echo "</ul>";
} else {
echo "Wrong";
}
}
you can use ajax for this to send your variable to another page and get response from there .something like this
I had to put all the select inside form tags, use a different page for the search and add the function to the bottom of the search page instead of accessing it from a different page.
Thank you to anyone who posted answers and tried to help me with this. It's working (with a few bugs) but I'm so happy it's finally working.