使用jQuery和PHP填充数组

时间:2012-06-20 04:36:14

标签: php jquery

我绝对在与jquery斗争。 基本上,我有一个搜索栏,它查询我的数据库,并动态填充下拉框与相关的搜索(像任何体面的搜索栏。)

无论如何,我实际上不知道如何允许用户点击元素来将数据添加到数组中。现在,它只是

 echo '<a href="index.php?action='.$result->game_id.'">';

这样可行,但我希望能够允许用户从搜索栏中选择多个内容。

如果有人可以发布一些代码,或者指向一个教程来帮助我解决这个可怕的问题。

感谢。

[编辑]

对不起,我认为我不够清楚,感谢整个jquery自动完成的信息,但我已经有了!

if(isset($_POST['queryString'])) {
    $queryString = mysqli_real_escape_string($GLOBALS["___mysqli_ston"],$_POST['queryString']);

    // Is the string length greater than 0?
    if(strlen($queryString) >0) {
        $query = mysqli_query($GLOBALS["___mysqli_ston"],"SELECT * FROM gamelist WHERE name LIKE '%" . $queryString . "%' LIMIT 8");

        if($query) {
            // While there are results loop through them - fetching an Object.

            while ($result = $query ->fetch_object()) {
                echo '<a href="index.php?action='.$result->game_id.'">';
                echo "<img src = ".$result->image_thumb." height=46 width=46 />";

                $name = $result->name;
                echo '<span class="searchheading">'.$name.'</span>';

                 $description = $result->aliases;
                 if(strlen($description) > 80) { 
                     $description = substr($description, 0, 80) . "...";
                 }                  
                 echo '<span>'.$description.'</span></a>';
            }
            echo '<span class="seperator"><a href="http://www.marcofolio.net/sitemap.html" title="Sitemap">Nothing interesting here? Try the sitemap.</a></span><br class="break" />';
        } else {
            echo 'ERROR: There was a problem with the query.';
        }
    } else {
        // Dont do anything.
    } // There is a queryString.

正如您所看到的,当我通过_POST获得输入时,我可以让用户与这些结果进行交互的唯一方法是单击一个href链接。我想我只是问,什么是一些jQuery代码,让我允许用户点击它,并将其存储回_POST []数组?这样我就可以让它们将多个搜索项存储到数组中,这样我的PHP代码就可以对它进行评估。

我希望更清楚。

1 个答案:

答案 0 :(得分:0)

你可以使用jquery autocomplete有cheeso说

在以下链接

http://jqueryui.com/demos/autocomplete/#remote

  you can click on view source to see the jquery for it .

您还可以查看其他需要传递数据的其他jquery自动完成插件列表

http://www.jqueryrain.com/2012/03/35-best-ajax-jquery-autocomplete-tutorial-plugin-with-examples/