【发布时间】:2015-08-14 09:05:32
【问题描述】:
我正在尝试制作一个从弹性搜索数据库中搜索名称的搜索框,但是当我运行它时,它总是给我一个错误----
注意:未定义索引:行中的值 --> $query = $_GET['search_keyword'];
但从我的脚本中,我相信它应该得到“search_keyword”。
搜索框 --
<form method="GET" action="/latest/helloTestData">
<input type="text" name="sample_search" id="sample_search" onkeyup="search_func(this.value);">
</form>
脚本 --
<script>
$(function () {
var minlength = 3;
$("#sample_search").keyup(function () {
var that = this,
value = $(this).val();
if (value.length >= minlength ) {
$.ajax({
type: "GET",
url: "/latest/helloTestData", // address to the php function
data: {
'search_keyword' : value
},
dataType: "text",
success: function(msg){
//we need to check if the value is the same
if (value==$(that).val()) {
//Receiving the result of search here
}
}
});
}
});
});
</script>
PHP ---
public function helloTestDataAction() {
$paramss = array('hosts' => array('localhost:9200'));
$client = new Elasticsearch\Client($paramss);
$query = $_GET['search_keyword'];
if ($query != "") {
$params = array();
$params['size'] = 1000000000;
$params['index'] = 'myindex';
$params['type'] = 'mytype';
$params['body']['query']['bool']['must'] = array(
array('match' => array('name' => $query)), // search data by input data
);
$esresult = $client->search($params);
if ($esresult < 1) {
echo "Your search did not match any documents. Please try different keywords.";
} else {
echo $esresult; //results found here and display them
}
}
return new Response('ok');
}
谁能知道如何解决这个问题。非常感谢。
【问题讨论】:
-
试试
$_GET['search_keyword']; -
没错,你在获取请求中没有
'value',你只有'search_keyword'。 -
注意:未定义索引:search_keyword
-
你为什么使用 jQuery keyup 函数并将 keyup 事件分配给表单?什么是 search_func?
-
我相信我在您的
$paramss定义中看到了语法错误