dongshi3818 2013-03-12 08:58
浏览 31

如何在一个页面中使用两个autosuggest(ajax,PHP,Mysql)

I used an autosuggest script (using jquery, ajax, php and mysql) in one of my pages and that worked perfectly.

but when i tried to add the autosuggest for another Form (Text Input) on the same page i got a lot of errors, it displays the correct information but when i click them that fills the first input, i tried modifying some variable but now its not working at all for the second input. and im really new to JS, Jquery, Ajax ( never used that before ).

Here's is the code of the page containing the inputs (Test.html).

<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js">
</script>
<script>
function suggest(inputString){
    if(inputString.length == 0) {
        $('#suggestions').fadeOut();
    } else {
    $('#country').addClass('load');
        $.post("autosuggest.php", {queryString: ""+inputString+""}, function(data){
            if(data.length >0) {
                $('#suggestions').fadeIn();
                $('#suggestionsList').html(data);
                $('#country').removeClass('load');
            }
        });
    }
}

function fill(thisValue) {
    $('#country').val(thisValue);
    setTimeout("$('#suggestions').fadeOut();", 600);
}
#result {
height:20px;
font-size:16px;
font-family:Arial, Helvetica, sans-serif;
color:#333;
padding:5px;
margin-bottom:10px;
background-color:#FFFF99;
}
#country{
padding:3px;
border:1px #CCC solid;
font-size:17px;
}
.suggestionsBox {
position: absolute;
left: 0px;
top:40px;
margin: 26px 0px 0px 0px;
width: 200px;
padding:0px;
background-color: #000;
border-top: 3px solid #000;
color: #fff;
}
.suggestionList {
margin: 0px;
padding: 0px;
}
.suggestionList ul li {
list-style:none;
margin: 0px;
padding: 6px;
border-bottom:1px dotted #666;
cursor: pointer;
}
.suggestionList ul li:hover {
background-color: #FC3;
color:#000;
}
ul {
font-family:Arial, Helvetica, sans-serif;
font-size:11px;
color:#FFF;
padding:0;
margin:0;
}

.load{
background-image:url(loader.gif);
background-position:right;
 background-repeat:no-repeat;
}

#suggest {
position:relative;
}

</style>
</head>
<body>

<form id="form" action="#">
<div id="suggest">type a country: <br />
  <input type="text" size="25" value="" id="country" onkeyup="suggest(this.value);"    onblur="fill();" class="" />

  <div class="suggestionsBox" id="suggestions" style="display: none;"> <img   src="arrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
    <div class="suggestionList" id="suggestionsList"> &nbsp; </div>
  </div>
</div>
<div id="suggestcity">type a city: <br />
  <input type="text" size="25" value="" id="city" class="" />
</div>
</form>
</body>
</html>

And the code of my the autosuggest.php is

<?php
$db_host = '';
$db_username = '';
$db_password = '';
$db_name = '';

$db = new mysqli($db_host, $db_username ,$db_password, $db_name);

if(!$db) {
       $db->set_charset("utf8");
    echo 'Could not connect to the database.';
} else {

    if(isset($_POST['queryString'])) {
        $queryString = $db->real_escape_string($_POST['queryString']);

        if(strlen($queryString) >0) {
            $db->set_charset("utf8");
            $query = $db->query("SELECT Ville FROM Villes WHERE Ville LIKE '$queryString%' LIMIT 10");
            if($query) {
            echo '<ul>';
                while ($result = $query ->fetch_object()) {
                    echo '<li onClick="fill(\''.addslashes($result->Ville).'\');">'.$result->Ville.'</li>';
                }
            echo '</ul>';

            } else {
                echo 'OOPS we had a problem :(';
            }
        } else {
            // do nothing
        }
    } else {
        echo 'There should be no direct access to this script!';
    }
}
?>

So My question is, what should i change ( variables and functions etc... ) in the two pages so i can use the autosuggest on the second input with the id="city" from another table on the same database.

if you can help that would be wonderful, i have been trying to modify whatever on the code for the last couple days and nothing worked. Thank you verry much

  • 写回答

1条回答 默认 最新

  • doudou32012 2013-03-12 09:00
    关注

    This will make your life much easier with auto-suggest. http://jqueryui.com/autocomplete/

    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像