0

i'm trying to make a dependent combobox with ajax but i don't no how to resolve the error (Undefined index: faculty_id) i think there is no typo or something wrong about the code and the query is working properly when i test it on sqlyog

this is my ajax code ,,

$(document).ready(function(){

    $('#faculty').change(function(){
        var faculty_id = $(this).val();

        $.ajax({
            type:'POST',
            url:'option.php',
            data:'faculty_id='+faculty_id,
            success: function(response){
                    $('#department').html(response);
                }

            }); 



    });
});

and this is my php code

<?php
include"../connection.php";
$faculty_id=$_POST['faculty_id'];
$query=mysql_query('select * from department where faculty_id=$faculty_id');
echo "<option value='' disabled selected>Choose One</option>";
while($departement=mysql_fetch_array($query))
{   
echo"<option values=".$departement[1].">".$departement[1]."</option>";  
}
?>

and this is my select code

<select name='faculty' id='faculty'>
<?php
include"../connection.php";
$query=mysql_query('select * from faculty');
while($faculty=mysql_fetch_array($query))
{
echo"<option value=".$faculty[0].">".$faculty</option>";                   
}
echo"<option value='' disabled selected>ChooseOne</option>";   
?>
</select>

<select name='departement' id="department">                  
</select>

when i change the data

data:faculty_id; or data:{'faculty_id':faculty_id}

still cant resolve the error please help me T_T

1 Answers1

1

You are sending data:'faculty_id='+faculty_id in ajax and you are receiving $faculty_id=$_POST['faculty']; in php. Change

$faculty_id=$_POST['faculty'];

to

$faculty_id=$_POST['faculty_id'];
Sanchit Patiyal
  • 4,910
  • 1
  • 14
  • 31