当我使用ajax时如何解决未定义的索引?

时间:2018-05-04 15:50:32

标签: javascript php jquery html ajax

我试图使用ajax创建一个依赖 for link in doc.xpath("//meta[@property='og:url']") { let urlLink = link["content"] print("Link: \(urlLink ?? "No link")") } ,但我不知道如何解决错误(combobox) 我认为代码没有错字或错误,当我在sqlyog上测试时查询工作正常

这是我的ajax代码,  

Undefined index: faculty_id

这是我的PHP代码

$(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);
                }

            }); 



    });
});

这是我的选择代码

<?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>";  
}
?>

当我更改数据时

<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>

仍然无法解决错误 请帮帮我T_T

1 个答案:

答案 0 :(得分:1)

您正在ajax中发送$faculty_id=$_POST['faculty'];,并且您在php中收到$faculty_id=$_POST['faculty']; 。 改变

$faculty_id=$_POST['faculty_id'];

character