Dynamic Car Make, Model, Year, Battery dropdown form
Hello,
I recenly started with a dropdown form that get car Make, Model and Year. I have a database called "Vehicles" with 3 Tables "year", "make", "model". When you select a year the optionfield make is comming up and you can select a make but after selecting for example "Toyota" it is stuck on Please Wait...
Can somebody tell me what I am missing or what is wrong with this script?
<?php
//**************************************
// Page load dropdown results //
//**************************************
include_once('dbconnect.php');
function getTierOne()
{
$result = mysql_query("SELECT DISTINCT year FROM vehicles order by year desc")
or die(mysql_error());
while($tier = mysql_fetch_array( $result ))
{
echo '<option value="'.$tier['year'].'">'.$tier['year'].'</option>';
}
}
//**************************************
// First selection results //
//**************************************
if(isset($_GET['func']) && ($_GET['func'] == "drop_1")) {
drop_1($_GET['drop_var']);
}
function drop_1($drop_var)
{
include_once('dbconnect.php');
$result = mysql_query("SELECT DISTINCT make FROM vehicles WHERE year='$drop_var' ORDER BY make asc")
or die(mysql_error());
echo '<select name="drop_2" id="drop_2">
<option value=" " disabled="disabled" selected="selected">Select Make</option>';
while($drop_2 = mysql_fetch_array( $result ))
{
echo '<option value="'.$drop_2['make'].'">'.$drop_2['make'].'</option>';
}
echo '</select>';
echo "<script type=\"text/javascript\">
$('#wait_2').hide();
$('#drop_2').change(function(){
$('#wait_2').show();
$('#result_2').hide();
$.get(\"dropdown.php\", {
func: \"drop_2\",
drop_var: $('#drop_2').val(),
drop_var2: $('#drop_1').val()
}, function(response){
$('#result_2').fadeOut();
setTimeout(\"finishAjax('result_2', '\"+escape(response)+\"')\", 400);
});
return false;
});
</script>";
}
?>
In the future I will get a data file with car battery's that should match the selected make, model and year after a submit... Is this possible and how should I adjust my database and script?
The reason why it "gets stuck" on loading is because I don't see any real Ajax happening, and you have no code to extract your list of make-related models.
This script is not finished, that is your problem. Nothing is wrong, albeit it's not functioning the way you think it is
//**************************************
// First selection results //
//**************************************
if (isset($_GET['func'])&& $_GET['func'] == "drop_1" ) {
drop_1($_GET['drop_var']);
}
function drop_1($drop_var)
{
include_once('db.php');
$result = mysql_query("SELECT DISTINCT model FROM vehicles WHERE make='$drop_var'")
or die(mysql_error());
DO you have any type of identifier that corresponds between the model of the car and the battery that works on it? What I mean by that is, is there some way of matching the model to the battery? You would need a column in the database with the batteries that match up to this if you are finding the battery in the database and don't know it off hand. If you either give a screenshot or text based layout of the table and it's columns (at least important ones to find it), it would be easier to assist you.
As an example if you do not wish to do that, try something like this:
Say I have the following combination:
Make: Volvo
Model: XC90
Year: 2013
Now I need to find the battery, and for the example we'll just say it's 246 (I don't know much about automotive things).
Since you said you have a table for the batteries, I'm assuming it's going to have at least a column with the name in it. You need some other column that holds a value that you can match it on (ex. serial, the make/model/year combination, or something that is unique to that battery). All you need to do is perform a query that will select that battery from the table with the given information.
Example 1:
PHP Code:
mysql_query('SELECT * FROM batteries WHERE `serial`=\''.$batterySerial.'\'');
Example 2:
PHP Code:
//THis variable would be set by the user's input $car = array( 'make' => 'Volvo', 'model' => 'XC90', 'year' => 2013 ); mysql_query('SELECT * FROM batteries WHERE `make`=\''.$car['make'].'\' AND `model`=\''.$car['model'].'\' AND `year`=\''.$car['year'].'\'');
If you need any more assistance, feel free to ask here.
I almost got it working but still got one problem...
My script is like this now:
PHP Code:
<?php //************************************** // Page load dropdown results // //************************************** function getTierOne() { $result = mysql_query("SELECT DISTINCT make FROM vehicles ORDER BY make ASC") or die(mysql_error());
//************************************** // First selection results // //************************************** if (isset($_GET['func'])&& $_GET['func'] == "drop_1" ) { drop_1($_GET['drop_var']); }
function drop_1($drop_var) { include_once('db.php'); $result = mysql_query("SELECT DISTINCT model FROM vehicles WHERE make='$drop_var' ORDER BY model") or die(mysql_error());
//************************************** // Second selection results // //************************************** if (isset($_GET['func'])&& $_GET['func'] == "drop_2" ) { drop_2($_GET['drop_var']); }
function drop_2($drop_var2) { include_once('db.php'); $result = mysql_query("SELECT DISTINCT year FROM vehicles WHERE model='$drop_var2'") or die(mysql_error());
</form>
</p>
<p>
<?php if(isset($_POST['submit'])){
$drop = $_POST['drop_1'];
$drop_2 = $_POST['drop_2'];
$drop_3 = $_POST['drop_3'];
$drop_4 = $_POST['drop_4'];
echo "U heeft een ";
echo $drop." ".$drop_2." geselecteerd uit ".$drop_3." en u heeft daar deze accu voor nodig ".$drop_4;
?>
<table border="1" bordercolor="#B5B5B5" style="background-color:#FFFFFF" width="250" cellpadding="3" cellspacing="3">
<tr>
<td>Merk:</td>
<td><?php echo $drop;?></td>
</tr>
<tr>
<td>Model:</td>
<td><?php echo $drop_2;?></td>
</tr>
<tr>
<td>Bouwjaar:</td>
<td><?php echo $drop_3;?></td>
</tr>
<tr>
<td>Accu:</td>
<td><?php echo $drop_4;?></td>
</tr>
</table>
<?php
}
?>
</body>
</html>
Func.php
PHP Code:
<?php
//**************************************
// Page load dropdown results //
//**************************************
function getTierOne()
{
$result = mysql_query("SELECT DISTINCT make FROM vehicles ORDER BY make ASC")
or die(mysql_error());
//**************************************
// First selection results //
//**************************************
if (isset($_GET['func'])&& $_GET['func'] == "drop_1" ) {
drop_1($_GET['drop_var']);
}
function drop_1($drop_var)
{
include_once('db.php');
$result = mysql_query("SELECT DISTINCT model FROM vehicles WHERE make='$drop_var' ORDER BY model")
or die(mysql_error());
//**************************************
// Second selection results //
//**************************************
if (isset($_GET['func'])&& $_GET['func'] == "drop_2" ) {
drop_2($_GET['drop_var'], $_GET['drop_var2']);
}
function drop_2($drop_var, $drop_var2)
{
include_once('db.php');
$result = mysql_query("SELECT DISTINCT year FROM vehicles WHERE make='$drop_var' AND model='$drop_var2'")
or die(mysql_error());