Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Ajax响应错误_Php_Mysql_Jquery - Fatal编程技术网

Php Ajax响应错误

Php Ajax响应错误,php,mysql,jquery,Php,Mysql,Jquery,我需要根据下拉列表的值进行mysql查询。在这里,我使用ajax向服务器发送下拉值。我想这部分对我很有用。但问题是我不能把它放到php上。注:两者在同一页中 这是我的Jquery代码: $('#filter-value').change(function(){ var filterValue = $(this).val(); //console.log(filterValue); $.ajax({ type: 'post', dataT

我需要根据下拉列表的值进行mysql查询。在这里,我使用ajax向服务器发送下拉值。我想这部分对我很有用。但问题是我不能把它放到php上。注:两者在同一页中

这是我的Jquery代码:

$('#filter-value').change(function(){
    var filterValue = $(this).val();
    //console.log(filterValue); 

    $.ajax({
        type: 'post',
        dataType: 'html',
        data: {filter: filterValue},
        success:function(data){ 
            alert(data); 
        }, 
        error:function (xhr, ajaxOptions, thrownError){
            //On error, we alert user
            alert(thrownError);
        }, 
        complete: function(){
            //alert('update success'); 
        }
    });
});
这是HTML表单

    <form method="post" action="">
        <select id="filter-value" name="filter">
            <option value="10">10</option>
            <option value="20">20</option>
            <option value="30">30</option>              
        </select>
    </form>
但这段php代码总是会被其他部分分割并打印“bad”

谁能告诉我哪里出了问题


多谢各位

您没有指定脚本的URL。请确保您正在从AJAX查询正确的文件

$.ajax({
    type: 'post',
    url: 'yourpage.php', // This one 
    //.....
});
将php更改为(并说另存为xyz.php):

和ajax调用的url:

$.ajax({
    url: "xyz.php",
    type: 'post',
    dataType: 'html',
    data: {filter: filterValue},
    success:function(data){ 
        alert(data); 
    }, 

echo json\u encode($\u POST)
添加到else子句中。您的代码将签出。告诉我如果你做
var\u dump($\u POST),你会得到什么;退出位于PHP@Starx我尝试了它,并在屏幕上显示了这个
数组(0){}
page@Barmar它打印这个
bad[]
@TNK,它不是值Dude,现在就指定url,即使它在同一个页面上。您需要通过ajax callRAJ将此php脚本发布到文件中。请告诉我您在上面的评论中的意思。请注意,您在ajax调用中发布到的url符合您的php脚本我是否需要将php添加到新页面?不一定。您可以发布到同一页面。但是,您需要确保在ajax中有相应的url参数
if($_POST) {
if (isset($_POST['filter'])) {
    $filter = $_POST['filter']; 
    echo $filter; 
    exit;
} else {
    echo 'bad';
}
}
$.ajax({
    url: "xyz.php",
    type: 'post',
    dataType: 'html',
    data: {filter: filterValue},
    success:function(data){ 
        alert(data); 
    },