所以我需要声明我在php中使用ajax传递的值,因为当我使用例如if(!empty($_POST["fsearch"]))
时它没有显示任何内容,当我放置$fsearch=$_POST['fsearch'];
时它给了我未定义的索引错误.I在jquery中有变量search_term,它将值传递给" fsearch"在PHP(这也是我的输入字段的名称)。
我不知道如何声明从Jquery到PHP的这个值。
有代码:
jQuery(document).ready(function ($) {
$("#food_search").keyup(function(event){
var search_term =$(this).val();
console.log(search_term);
$.ajax({
type:"POST",
url:"/Food-Search",
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);
}
});
});
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!----------------------------------------------------------------
HTML
----------------------------------------------------------------->
<form method="POST">
<p>Търсене на храни: <input type="text" name="fsearch" id="food_search"></p>
</form>
<!----------------------------------------------------------------
PHP
----------------------------------------------------------------->
<?php
if(!empty($_POST["fsearch"])) {
echo "True";
} else {
echo "False";
}
?>
&#13;
所以我需要定义&#39; fsearch&#39;来自jquery的价值。 并检查输入字段是否带有名称&#34; fsearch&#34;是空的。 谢谢你的所有建议!
答案 0 :(得分:0)
试试这个..
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);
}
});
});
});