如何在PHP中声明jquery传递的值?

如何在PHP中声明jquery传递的值?,php,ajax,Php,Ajax,所以我需要声明我在php中用ajax传递的值,因为当我使用例如if(!empty($\u POST[“fsearch”])时,它不显示任何内容,当我放入$fsearch=$\u POST[“fsearch”]它给了我未定义的索引错误。我在jquery中有变量search_term,它将值传递给php中的“fsearch”(这也是我输入字段的名称)。 我不知道如何声明这个从Jquery输入PHP的值。。 代码如下: jQuery(文档).ready(函数($){ $(“#食物搜索”).keyup

所以我需要声明我在php中用ajax传递的值,因为当我使用例如
if(!empty($\u POST[“fsearch”])
时,它不显示任何内容,当我放入
$fsearch=$\u POST[“fsearch”]它给了我未定义的索引错误。我在jquery中有变量search_term,它将值传递给php中的“fsearch”(这也是我输入字段的名称)。
我不知道如何声明这个从Jquery输入PHP的值。。
代码如下:

jQuery(文档).ready(函数($){
$(“#食物搜索”).keyup(功能(事件){
var search_term=$(this.val();
console.log(搜索词);
$.ajax({
类型:“POST”,
url:“/食品搜索”,
数据:{fsearch:search_term},
成功:功能(res){
$(“#食品搜索结果”).html(res);
控制台日志(res);
},
错误:函数(xhr、ajaxOptions、thrownError){
警报(xhr.状态);
警报(xhr.responseText);
警报(thrownError);
}
});
});
});

ъаааааааа:

像这样试试

jQuery(document).ready(function ($) {
    $("#food_search").on('blur',function(){ //blur event ...triggres when input field is unfoused
        var search_term =$(this).val();
        console.log(search_term);
$.ajax({
    type:"POST",
    url:"/Food-Search.php",//if you are not using .htaccess or routing give file name with .php extension
    data:{fsearch:search_term},
    success:function(res){
        $("#food_search_result").html(res);
        console.log(res);
    },
    error: function (xhr, ajaxOptions, thrownError) {
           alert(xhr.status);
           alert(xhr.responseText);
           alert(thrownError);
       }
       });
    });
});

最好解释一下答案。变化并不明显,OP可能不明白原因。