Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.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
服务器端脚本onkeyup-PHP_Php_Ajax_Symfony_Onkeyup - Fatal编程技术网

服务器端脚本onkeyup-PHP

服务器端脚本onkeyup-PHP,php,ajax,symfony,onkeyup,Php,Ajax,Symfony,Onkeyup,我试图创建一个搜索框,从弹性搜索数据库中搜索名称,但当我运行时,它总是给我一个错误---- 注意:未定义索引:行中的值-->$query=$\u GET['search\u keyword'] 但是从我的脚本中,我相信它应该得到“search_关键字” 搜索框-- <form method="GET" action="/latest/helloTestData"> <input type="text" name="sample_search" id="sample_s

我试图创建一个搜索框,从弹性搜索数据库中搜索名称,但当我运行时,它总是给我一个错误----

注意:未定义索引:行中的值-->$query=$\u GET['search\u keyword']

但是从我的脚本中,我相信它应该得到“search_关键字”

搜索框--

<form method="GET" action="/latest/helloTestData">

    <input type="text" name="sample_search" id="sample_search" onkeyup="search_func(this.value);">

</form>
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');
    }

剧本--


$(函数(){
var minlength=3;
$(“#示例搜索”).keyup(函数(){
var=这个,
value=$(this.val();
如果(value.length>=最小长度){
$.ajax({
键入:“获取”,
url:“/latest/helloTestData”,//php函数的地址
数据:{
“搜索关键字”:值
},
数据类型:“文本”,
成功:功能(msg){
//我们需要检查值是否相同
if(value=$(that).val()){
//正在此处接收搜索结果
}
}
});
}
});
}); 
PHP---

<form method="GET" action="/latest/helloTestData">

    <input type="text" name="sample_search" id="sample_search" onkeyup="search_func(this.value);">

</form>
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');
    }
公共函数helloTestDataAction(){
$paramss=array('hosts'=>array('localhost:9200');
$client=new-Elasticsearch\client($paramss);
$query=$\u GET['search\u keyword'];
如果($query!=“”){
$params=array();
$params['size']=100000000;
$params['index']='myindex';
$params['type']='mytype';
$params['body']['query']['bool']['must']=array(
数组('match'=>array('name'=>$query)),//按输入数据搜索数据
);
$esresult=$client->search($params);
如果($esresult<1){
echo“您的搜索与任何文档都不匹配。请尝试其他关键字。”;
}否则{
echo$esresult;//在此处找到并显示结果
}
}
返回新的响应(“ok”);
}

谁都知道如何解决这个问题。非常感谢高级版。

$\u GET['value']
修改为
$\u GET['search\u keyword']

所以

您正在搜索一个不会进入
$\u GET
数组的键,因为在ajax请求中,您正在传递一个名为
search\u keyword
的键,因此这是一个错误

只需替换即可

 $_GET['value'] to $_GET['search_keyword']

快速简单!前端通过GET by url传递键入的文本

在这个测试中,我将这两个文件放在同一个文件夹中。根据需要改变

前端:

<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>

<div id="my_header" style="background-color:lightblue">
</div>

<input type="text" id="foo" value="bar" onkeyup="loadlink(this)" />

<script>
function loadlink(e){
    $('#my_header').load('back.php?var='+e.value,function () {
         $(this).unwrap();
    });
}
</script>

函数加载链接(e){
$('#my_header').load('back.php?var='+e.value,函数(){
$(this.unwrap();
});
}
后端(Back.php):



尝试使用
$\u获取['search\u keyword']
确切地说,get请求中有'nt
'value'
,只有'search\u keyword'。注意:未定义索引:search\u关键字为什么要使用jQuery keyup函数并将keyup事件分配给表单?什么是搜索功能?我相信我在你的
$paramss
定义上看到了语法错误。确切地说,你的答案添加到我的答案和评论中了什么?注意:未定义索引:搜索关键字注意:未定义索引:搜索关键字
<?php 
echo "received: " . $_GET["var"] . ' <br>at ' . gmdate('Y-m-d h:i:s \G\M\T', time());
?>