0

I have the following php page with four dropdown list correctly populated with four differents mysql query. I would populate Categories_2 through a query depending on CATEGORIES_1.c_id and the same nested for others dropdown.

Tried to call myFunction() onChange but it seems not working. I don't get the alert.

Does anyone know how I could do this? Any help would be much appreciated.

I know I have to use mysqli but it's an existing page and I will do it later.

<script>
var myDropdown=document.getElementsByName('cat1')[0];
function myFunction(){
    alert('option changed : '+myDropdown.value);
}
</script>



<form enctype="multipart/form-data" method="post" action="import.php">

        <label for="cat_name">Categories_1</label>
        <select name = "cat1" onChange="myFunction()">
        <?php
            $s = mysql_query("SELECT * FROM `CATEGORIES_1`");
            while($row = mysql_fetch_assoc($s)) {
            echo ('<option value="' . $row['c_id'] . '">' . $row['c_name'] . '</option>'); } ?>
        </select>
        <br>            

        <label for="cat_nam">Categories_2</label>
        <select>
        <?php
            $s = mysql_query("SELECT c_name FROM `CATEGORIES_2`");
            while($row = mysql_fetch_assoc($s)) {
            echo ('<option value="0">' . $row['c_name'] . '</option>'); }            ?>
        </select>
        <br>            

        <label for="cat_nae">Categories_3</label>
        <select>
        <?php
            $s = mysql_query("SELECT c_name FROM `CATEGORIES_3`");
            while($row = mysql_fetch_assoc($s)) {
            echo ('<option value="0">' . $row['c_name'] . '</option>'); } ?>
        </select>
        <br>            

        <label for="cat_ame">Categories_4</label>
        <select>
        <?php
            $s = mysql_query("SELECT c_name FROM `CATEGORIES_4`");
            while($row = mysql_fetch_assoc($s)) {
            echo ('<option value="0">' . $row['c_name'] . '</option>'); }            ?>
        </select>

4 Answers 4

1

You need to use ajax for that .. i hope this helps ..

JS

$(document).ready(function(){
    $(document).on('change','#cat1',function(){
        $.ajax({
            cache: false,
            url: 'getSecondSelect.php',
            type: 'post',
            data: 'input=' + $(this).val(),
            success: function(data){
                $('#cat2').html(data)
            }
        })
    })
}

FORM

<form enctype="multipart/form-data" method="post" action="import.php">
    <label for="cat_name">Categories_1</label>
    <select id="cat1" name="cat1">
    <?php
        $s = mysql_query("SELECT * FROM `CATEGORIES_1`");
        while($row = mysql_fetch_assoc($s)) {
        echo ('<option value="' . $row['c_id'] . '">' . $row['c_name'] . '</option>'); }
    ?>
    </select>
    <br />

    <label for="cat_name">Categories_2</label>
    <select id="cat2" name="cat2"></select>
    <br />

getSecondSelect.php

$s = mysql_query("SELECT c_name FROM `CATEGORIES_2` WHERE CATEGORIES_1.c_id='".$_POST['input']."'");
while($row = mysql_fetch_assoc($s)) {
echo ('<option value="0">' . $row['c_name'] . '</option>'); }
Sign up to request clarification or add additional context in comments.

Comments

0

This is wrong method to call your dropdoen value

var myDropdown=document.getElementsByName('cat1')[0];

Your myFunction would be

<script>
function myFunction(){
var myDropdown=document.getElementById('cat1');
  var myDropdowval= myDropdown[myDropdown.selectedIndex].value;
   alert(myDropdowval);
}
</script>

here you assign id id="cat1" to your dropdown

 <select name = "cat1" id="cat1" onChange="myFunction()">
    <?php
    $s = mysql_query("SELECT * FROM `CATEGORIES_1`");
    while ($row = mysql_fetch_assoc($s)) {
        echo ('<option value="' . $row['c_id'] . '">' . $row['c_name'] . '</option>');
    }
    ?>
</select>

Comments

0

Use ajax call and different php pages to generate response based on AJAX call, send selected drop-down id as parameter to php page. and set that incoming response to your below drop-downs. If your using Jquery then use

 $('[name=cat1]').val()

Comments

0
<select id="myselect">
    <option value="1">Mr</option>
    <option value="2">Mrs</option>
    <option value="3">Ms</option>
    <option value="4">Dr</option>
    <option value="5">Prof</option>
</select>

alert($( "#myselect option:selected" ).text());

and here is working JSFIDDLE

2 Comments

Nothing is in your provided link
sorry @milankyada now added link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.