0

i have two combo box and of them are having values from database, what i want is that when i choose on of the option in the first combo box the options in the second combo box will change based on the selected box,. i found a piece of code that works as what i intended but the problem is its static what i want is dynamic, here's the code i found, compare it to mine and please correct mine.

HTML:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="type">
    <option value="item1">item1</option>
    <option value="item2">item2</option>
    <option value="item3">item3</option>
</select>

<select id="size">
    <option value="">-- select one -- </option>
</select>  

javascript:

$(document).ready(function () {
$("#type").change(function () {
var val = $(this).val();
if (val == "item1") {
$("#size").html("<option value='test'>item1: test 1</option><option value='test2'>item1: test 2</option>");
 } else if (val == "item2") {
 $("#size").html("<option value='test'>item2: test 1</option><option value='test2'>item2: test 2</option>");
} else if (val == "item3") {
$("#size").html("<option value='test'>item3: test 1</option><option value='test2'>item3: test 2</option>");
    }
});
});  

now here's mine, I have a datase for the first combo box and it works since it's outside the tag. for the first data base i have attributes like sex_id and value with the data (men, women). and for the second combo box which will change based on the selected option I have table name lala with attributes of id, sex_id, category with the values of men1, men2, men3, women1,women2, women3.(the two tables are connected or relatioship. ) when i chose men the second combobox must have this values men1, men2, men3. and for the women we have women1, women2, women3. here's how i did it.
html:

<select class ="form-control" id="age" name="gender">
<option value="" selected> - Gender -</option>
<?php
$sql = "SELECT * from sex";
$q= $conn->query($sql);
$q->setFetchMode (PDO::FETCH_ASSOC);

while($r = $q->fetch())
{
?>
<option value="<?php echo $r['value']?>"> <?php echo $r['value']?>  </option>

<?php

}?>
</select>
<select class ="form-control" id="sex" name="gender">
<option value="" selected>- Age Span -</option>

</select> 

javascript:

<script>
$(document).ready(function () {
$("#age").change(function () {
var val = $(this).val();
global $sql;
if (val == "the value here will be dyna,ic based on the selected option") {
global $sql;
$sql = "SELECT * from sex";
$q= $conn->query($sql);
$q->setFetchMode (PDO::FETCH_ASSOC);
while($r = $q->fetch())
{
$("#sex").html("<option value='test'>" + $r['haha'] + " </option><option value='test2'>item1: test 2</option>");
}
}
} 
});
});
  • You can't execute the php code in your javascript code. The php code is executed on the server, before page load. The javascript is executed on the client side, after page load. So your `$("#sex").html("...")` will always be the same. You need to use `$.ajax()` to retrieve this data. – Sean Jan 17 '16 at 06:42
  • how to do it using ajax? – Anecito Alima Santillan Jan 17 '16 at 06:45
  • do a search here on SO, as there are thousands of examples of ajax. for example http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php – Sean Jan 17 '16 at 06:47

0 Answers0