jquery-ui-1.8.14 使用 php 和 mysql 自动完成

发布于 2024-12-04 21:58:03 字数 1812 浏览 0 评论 0原文

一般来说,我不太喜欢 js 和编程,但我非常坚持一些实际上不应该太难的东西。请随意访问测试页面:

[已删除链接]

我有三个自动完成字段:当前俱乐部、国家和职业统计数据。 自动完成功能非常适合职业统计数据,我还可以添加字段,并且自动完成功能也适用于添加的字段。

但对于当前的俱乐部和国家字段,我在输入时得到结果,但当我单击正确的输出时,它不会显示在输入字段中。

我可以使用其他 js 库使其工作,但它不再适用于添加按钮职业统计字段。

我使用以下库:

<script  type="text/javascript" src="js/jquery-1.6.3.js"></script>
<script  type="text/javascript" src="js/jquery-ui-1.8.14.custom.min.js"></script>
<script  type="text/javascript" src="js/jq-ac-script.js"></script>

当前的俱乐部 html 看起来像:

<p>
    Current club <label>:</label>
    <input type="text" id="currentclub" />
</p>

在定制的 jq-ac-script.js 中(我最初在网上的某个地方找到它 - 不记得在哪里),重要的部分是:

$(document).ready(function(){
    $( "#currentclub" ).autocomplete({
        source: "get_club_list.php",
        minLength: 1    
    })
    .data( "autocomplete" )._renderItem = function( ul, item ) {
       return $( "<li></li>" )
       .data( "item.autocomplete", item )
       .append( item.currentclub )
       .appendTo( ul );
    };
});

“get_club_list.php” 看起来喜欢:

<?php
include ("dbsetup.php");

$return_arr = array();
$param = $_GET["term"];

$fetch = mysql_query("SELECT * FROM FootNews_CLUB 
    WHERE clubShortName LIKE '%$param%'");

/* Retrieve and store in array the results of the query.*/
while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {

    $row_array['currentclub']           = $row['clubShortName'];
    array_push( $return_arr, $row_array );
}

/* Free connection resources. */
mysql_close($conn);

/* Toss back results as json encoded array. */
echo json_encode($return_arr);

?>

任何关于为什么当我点击它时所选俱乐部没有出现的想法都将受到赞赏!

I'm not very much into js and programing in general, but I'm very stuck on something that really shouldn't be too difficult. Feel free to visit the test page:

[REMOVED LINK]

I have three autocomplete fields: Current club, nation and career stats.
Autocomplete works perfectly for the career stats where I can also add fields and the autocomplete also works for the added field.

But for the current club and nation fields, I get results while typing but when I click the correct output it doesn't show up in the input-field.

I can make it work using other js-libraries, but then it no longer work for the add-button career stats fields.

I use the following libraries:

<script  type="text/javascript" src="js/jquery-1.6.3.js"></script>
<script  type="text/javascript" src="js/jquery-ui-1.8.14.custom.min.js"></script>
<script  type="text/javascript" src="js/jq-ac-script.js"></script>

The current club html looks like:

<p>
    Current club <label>:</label>
    <input type="text" id="currentclub" />
</p>

In the custom made jq-ac-script.js (I originally found this somewhere online - don't remember where) the important part is:

$(document).ready(function(){
    $( "#currentclub" ).autocomplete({
        source: "get_club_list.php",
        minLength: 1    
    })
    .data( "autocomplete" )._renderItem = function( ul, item ) {
       return $( "<li></li>" )
       .data( "item.autocomplete", item )
       .append( item.currentclub )
       .appendTo( ul );
    };
});

The "get_club_list.php" looks like:

<?php
include ("dbsetup.php");

$return_arr = array();
$param = $_GET["term"];

$fetch = mysql_query("SELECT * FROM FootNews_CLUB 
    WHERE clubShortName LIKE '%$param%'");

/* Retrieve and store in array the results of the query.*/
while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {

    $row_array['currentclub']           = $row['clubShortName'];
    array_push( $return_arr, $row_array );
}

/* Free connection resources. */
mysql_close($conn);

/* Toss back results as json encoded array. */
echo json_encode($return_arr);

?>

Any ideas whereas to why the selected club doesn't show up when I click it would be appriciated!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮生面具三千个 2024-12-11 21:58:03

哇,用了我的 php 代码。酷,很高兴我能提供帮助。
http://www. jensbits.com/2010/03/29/jquery-ui-autocomplete-widget-with-php-and-mysql/

不确定为什么要使用自动完成代码其中的 _renderItem 。我认为你不需要它。

将 php 代码更改为:

while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {

    $row_array['currentclub']  = $row['clubShortName'];
    $row_array['value']   = $row['clubShortName'];
    array_push( $return_arr, $row_array );
}

并且,将 jquery 更改为:

$( "#currentclub" ).autocomplete({
    source: "get_club_list.php",
    minLength: 1    
});

您可以再次阅读我的教程,但自动完成需要返回 labelvalue 字段。然后,它会使用该值填充选择列表和相应的输入字段。

我留在了 $row_array['currentclub'] = $row['clubShortName']; 中,因为我不知道您稍后是否会抓住它。如果不是,则也不需要该行。

由于您控制返回的数据并且可以在 php 中指定 label 和/或 value 字段,我不明白您为什么使用 _renderItem 用于任何自动完成。

顺便说一句,您应该将 mysql_real_escape_string 添加到您的 php 代码中以进行一些 SQL 注入保护:http://www.php.net/manual/en/function.mysql-real-escape-string.php

Wow, used my php code. Cool, glad I could help.
http://www.jensbits.com/2010/03/29/jquery-ui-autocomplete-widget-with-php-and-mysql/

Not sure why you are using the autocomplete code with _renderItem in it. I don't think you need it.

Change the php code to this:

while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {

    $row_array['currentclub']  = $row['clubShortName'];
    $row_array['value']   = $row['clubShortName'];
    array_push( $return_arr, $row_array );
}

And, the jquery to:

$( "#currentclub" ).autocomplete({
    source: "get_club_list.php",
    minLength: 1    
});

You can read through my tutorial again but the autocomplete needs a label or value field returned. It then populates the select list and the corresponding input field with that value.

I left in $row_array['currentclub'] = $row['clubShortName']; because I don't know if you are grabbing that later on. If you are not, you don't need that line either.

Since you control the returned data and can specify a label and/or value field in the php, I don't understand why you are using the _renderItem for any of the autocompletes.

BTW, you should add mysql_real_escape_string to your php code for some sql injection protection: http://www.php.net/manual/en/function.mysql-real-escape-string.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文