Forum Moderators: open

Message Too Old, No Replies

Javascript dynamic select

         

Oldsoldier

8:26 pm on Jan 8, 2015 (gmt 0)

10+ Year Member



Hi am new to javascript.
Am trying to create dependent three dropdown select box from the database.
I have been able to manage to create the first two but can't figure out how to create the third box
$(document).ready(function() {
$('#school').change(function(){
var code = $(this).val();
//alert(code);
var data = 'code='+ code;

$.ajax({
type: "POST",
url:"cert.php",
data: data,
cache: false,
success: function(html)
{
$('#cert').html(html);
}
});
});

physics

10:13 pm on Jan 8, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please provide more information, including the relevant HTML you want to work with and maybe some sample data.

Oldsoldier

11:09 pm on Jan 8, 2015 (gmt 0)

10+ Year Member



Hi Physics
This is what i have.
Index.php
<form action="" method="post">
<select name="school" id="school">
<?php
include 'db.php';
$sql = "SELECT DISTINCT tier_one FROM three_drops";
$query = mysqli_query($db,$sql);
while($row = mysqli_fetch_array($query)){
echo '<option value="'.$row['tier_one'].'">'.$row['tier_one'].'</option>';
}
?>
</select>
</br>
<select name="cert" id="cert">
<option value="" selected="selected">Select your Certification</option>
</select>
</br>
<select name="prog" id="prog">
<option value="" selected="selected">Select your Programme</option>
</select>
</form>

[edited by: Oldsoldier at 11:13 pm (utc) on Jan 8, 2015]

Oldsoldier

11:11 pm on Jan 8, 2015 (gmt 0)

10+ Year Member



Ps. I changed code to code 2 because of the styling option in posting on the forum
Cert.php
<?php
include 'db.php';
if(isset($_POST['code2'])){
$sql = "SELECT DISTINCT tier_two FROM three_drops WHERE tier_one='" . $_POST[code2] . "'";
$query = mysqli_query($db,$sql);
if(mysqli_num_rows($query)==0){
echo '<option>No Records</option>';
} else {
while($row = mysqli_fetch_array($query)){
echo '<option value="' . $row['tier_two'] . '">' . $row['tier_two'] . '</option>';
}
}
}else{
echo '<option>An error Occured</option>';
}

if(isset($_POST['code1'])){
$sql = "SELECT * FROM three_drops WHERE tier_two= '" . $_POST[code1] . "'";
$query = mysqli_query($db,$sql);
if(mysqli_num_rows($query)==0){
echo '<option>No Records</option>';
} else {
while($row = mysqli_fetch_array($query)){
echo '<option value="'.$row['tier_three'].'">'.$row['tier_three'].'</option>';
}
}
}else{
echo '<option>An error Occured</option>';
}
?>


sql database
CREATE TABLE IF NOT EXISTS `two_drops` (
`id` int(11) NOT NULL auto_increment,
`tier_one` varchar(255) NOT NULL,
`tier_two` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM ;

--
-- Dumping data for table `two_drops`
--

INSERT INTO `two_drops` (`id`, `tier_one`, `tier_two`) VALUES
(1, 'Colors', 'Red'),
(2, 'Colors', 'Blue'),
(3, 'Colors', 'Green'),
(4, 'Colors', 'Yellow'),
(5, 'Colors', 'Black'),
(6, 'Shapes', 'Square'),
(7, 'Shapes', 'Circle'),
(8, 'Shapes', 'Triangle'),
(9, 'Shapes', 'Rectangle'),
(10, 'Shapes', 'Oval');