【问题标题】:Server side scripting onkeyup PHP服务器端脚本 onkeyup PHP
【发布时间】: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 定义中看到了语法错误

标签: php ajax symfony onkeyup


【解决方案1】:

$_GET['value']修改为$_GET['search_keyword']

所以

public function helloTestDataAction() {
    [...]
    $_GET['search_keyword'];
    [...]
}

您正在搜索一个不会进入 $_GET 数组的键,因为在您的 ajax 请求中,您正在传递一个名为 search_keyword 的键,所以这是错误

【讨论】:

  • 注意:未定义索引:search_keyword
【解决方案2】:

简单替换

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

【讨论】:

  • 没错,你的回答对我和 cmets 有什么补充?
  • 注意:未定义索引:search_keyword
【解决方案3】:

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

在这个测试中,我将 2 个文件放在同一个文件夹中。根据需要进行更改。

前端:

<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>

后端(back.php):

<?php 
echo "received: " . $_GET["var"] . ' <br>at ' . gmdate('Y-m-d h:i:s \G\M\T', time());
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-27
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多