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?
You're in the wrong forum, this is a javascript question.
And as such, only the generated output is relevant. This: <?php getTierOne(); ?> doesn't say anthing about your problem.
At this moment i only see an onchange event for drop_1. You haven't given the situation of what it looks like when the response is added to the DOM, but i assume there will be a drop_2 ? you will need to include code to handle that one too.
//**************************************
// 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());
For your next posts (here or on any other developers forum), please keep in mind the following etiquette:
1. When you post a problem and manage to solve it yourself: always post the answer in the topic. The whole idea of a forum is that other people can also benefit, but if you don't post the solution your post becomes almost meaningless
2. Please keep 1 threat focussed to 1 problem. If you bump in to other problems with your project, post them in a new topic.
Regarding your problem however, take these steps:
1. write a sql statement that retrieves the desired product and verify on the mysql command line (or with phpmyadmin) that it works
2. substitute where necessary parts of the query for php-variables and write some php-code around it to execute it on the database, capture the output and echo it to the screen for verification
3. when you've determined the query still gives the desired result, write the rest of the code with it (like, for example, echoing a message for the user
If you get stuck, post the code where you get stuck describing the problem .. this way you show that you are putting an effort into yourself as well, instead of simply asking someone to do it for you
</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;
}
?>
</body>
</html>
func.php:
PHP Code:
<?php
//**************************************
// Page load dropdown results //
//**************************************
function getTierOne()
{
$result = mysql_query("SELECT DISTINCT make FROM vehicles")
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'")
or die(mysql_error());
//**************************************
// Second selection results //
//**************************************
if (isset($_GET['func'])&& $_GET['func'] == "drop_2" ) {
drop_2($_GET['drop_var']);
}
function drop_2($drop_var)
{
include_once('db.php');
$result = mysql_query("SELECT DISTINCT year FROM vehicles WHERE model='$drop_var'")
or die(mysql_error());
After I selected the year I get another dropdown for the battery's and when I select my Volvo - XC90 - 2013 I also can select the accu that is matched with the rest... But I also get a lot of empty records in my dropdown...
I fixed the empty records but I see another problem in my last function...
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());
When I can select an "accu" it will display all accu's from 2013 and not only the Volvo - XC90 from 2013...
I have to edit my last function...
I allready tried this:
PHP Code:
function drop_3($drop_var, $drop_var2, $drop_var3)
{
include_once('db.php');
$result = mysql_query("SELECT * FROM vehicles WHERE make='$drop_var' AND model='$drop_var2' AND year='$drop_var3'")
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());
Bookmarks