View Single Post
Old 01-11-2013, 12:40 PM   PM User | #3
Joepiooo
New Coder

 
Join Date: Jun 2012
Posts: 34
Thanks: 0
Thanked 1 Time in 1 Post
Joepiooo is an unknown quantity at this point
Hello again,

I got it working now but getting stuck with something else I want to create.

This are my two scripts now:

index.php

PHP Code:
<?php 
  
include('db.php');
  include(
'func.php');
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Chained Select Boxes using PHP, MySQL and jQuery</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
    $('#wait_1').hide();
    $('#drop_1').change(function(){
      $('#wait_1').show();
      $('#result_1').hide();
      $.get("func.php", {
        func: "drop_1",
        drop_var: $('#drop_1').val()
      }, function(response){
        $('#result_1').fadeOut();
        setTimeout("finishAjax('result_1', '"+escape(response)+"')", 400);
      });
        return false;
    });
});

function finishAjax(id, response) {
  $('#wait_1').hide();
  $('#'+id).html(unescape(response));
  $('#'+id).fadeIn();
}
function finishAjax_tier_three(id, response) {
  $('#wait_2').hide();
  $('#'+id).html(unescape(response));
  $('#'+id).fadeIn();
}
</script>
</head>

<body>
<p>
<form action="" method="post">
  
    <select name="drop_1" id="drop_1">
    
      <option value="" selected="selected" disabled="disabled">Selecteer Merk</option>
      
      <?php getTierOne(); ?>
    
    </select> 
    
    <span id="wait_1" style="display: none;">
    <img alt="Please Wait" src="ajax-loader.gif"/>
    </span>
    <span id="result_1" style="display: none;"></span>
    <span id="wait_2" style="display: none;">
    <img alt="Please Wait" src="ajax-loader.gif"/>
    </span>
    <span id="result_2" style="display: none;"></span> 
  
</form>
</p>
<p>
<?php if(isset($_POST['submit'])){
    
$drop $_POST['drop_1'];
    
$drop_2 $_POST['drop_2'];
    
$drop_3 $_POST['drop_3'];
    echo 
"U heeft een ";
    echo 
$drop." ".$drop_2." geselecteerd uit ".$drop_3;
}
?>
</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());

      while(
$tier mysql_fetch_array$result )) 
  
        {
           echo 
'<option value="'.$tier['make'].'">'.$tier['make'].'</option>';
        }

}

//**************************************
//     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());
    
    echo 
'<select name="drop_2" id="drop_2">
          <option value=" " disabled="disabled" selected="selected">Selecteer Model</option>'
;

           while(
$drop_2 mysql_fetch_array$result )) 
            {
              echo 
'<option value="'.$drop_2['model'].'">'.$drop_2['model'].'</option>';
            }
    
    echo 
'</select>';
    echo 
"<script type=\"text/javascript\">
$('#wait_2').hide();
    $('#drop_2').change(function(){
      $('#wait_2').show();
      $('#result_2').hide();
      $.get(\"func.php\", {
        func: \"drop_2\",
        drop_var: $('#drop_2').val()
      }, function(response){
        $('#result_2').fadeOut();
        setTimeout(\"finishAjax_tier_three('result_2', '\"+escape(response)+\"')\", 400);
      });
        return false;
    });
</script>"
;
}


//**************************************
//     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 * FROM vehicles WHERE model='$drop_var'"
    or die(
mysql_error());
    
    echo 
'<select name="drop_3" id="drop_3">
          <option value=" " disabled="disabled" selected="selected">Selecteer Jaar</option>'
;

           while(
$drop_3 mysql_fetch_array$result )) 
            {
              echo 
'<option value="'.$drop_3['year'].'">'.$drop_3['year'].'</option>';
            }
    echo 
'</select> ';
    echo 
'<input type="submit" name="submit" value="Submit" />';
}
?>
When you press sumbit you get the following output when selecting:

Make = Volvo
Model = XC90
Year = 2013

"You selected a Volvo XC90 from 2013"


So that part is working allright!

The second thing I want to create is when you select the fields as above you also echo the Battery that belongs to that combination.

I have another table called "battery" in my database for example this record:

Make = Volvo
Model = XC90
Year = 2013
Battery = XXX

How can I echo "battery" too that belongs to the selected fields of the dropdown?

Something like this:

"You selected a Volvo XC90 from 2013 and you should buy the XXX battery"

I got it this far but I'm not that good in php so if somebody could help me here that would be great

Thanks in advance
Joepiooo is offline   Reply With Quote