/    Sign up×
Community /Pin to ProfileBookmark

Multiple autosuggest textboxes

I’m trying to get a series of dropdown boxes which will autosuggest based on info stored in a DB. I have it working correctly for the initial box, but when I put a second it’ll refill the first box. I’m pretty sure I need an array or something but im not a programmer and am not really sure how to adjust the script for multiple boxes. I was hoping someone here could help me out.

Thanks! php code and html page are included. As I said, first box works correctly, using the second pulls up the autosuggest options, but when I select one it fills the first box.

[CODE]<?php
$db = new mysqli(‘xxx’, ‘xxx’ ,’xxx’, ‘xxx’);

if(!$db) {

echo ‘Could not connect to the database.’;
} else {

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

if(strlen($queryString) >0) {

$query = $db->query(“SELECT item FROM items WHERE item LIKE ‘$queryString&#37;’ LIMIT 10”);
if($query) {
echo ‘<ul>’;
while ($result = $query ->fetch_object()) {
echo ‘<li onClick=”fill(”.addslashes($result->item).”);”>’.$result->item.'</li>’;
}
echo ‘</ul>’;

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

[code=html]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<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);
$(‘#item’).removeClass(‘load’);
}
});
}
}

function fill(thisValue) {
$(‘#item’).val(thisValue);
setTimeout(“$(‘#suggestions’).fadeOut();”, 600);
}

</script>

<style>
#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”><br />
<input type=”text” size=”25″ value=”” id=”country” onkeyup=”suggest(this.value);” onblur=”fill();” class=”” />

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

</body>
</html>[/code]

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@OctoberWindOct 07.2011 — 
<input type="text" size="25" value="" [COLOR="Red"]id="country"[/COLOR] onkeyup="suggest(this.value);" onblur="fill();" class="" />

<input type="text" size="25" value="" [COLOR="Red"]id="country" [/COLOR]onkeyup="suggest(this.value);" onblur="fill();" class="" />
[/QUOTE]


You can only have one unique ID per page. You also might need to adjust your javascript to not target $('#country'). but rather a more global $('.some_field_class_to_modify') that you can then apply to any field you want to auto-suggest for.
Copy linkTweet thisAlerts:
@jschnyderiteauthorOct 07.2011 — You can only have one unique ID per page. You also might need to adjust your javascript to not target $('#country'). but rather a more global $('.some_field_class_to_modify') that you can then apply to any field you want to auto-suggest for.[/QUOTE]

i changed the classes to item1 and item2, first works fine, but the latter pulls up the dropdown menu but doesn't prefill when you select an item...is there a way to make it like an array so that any id beginning with 'item' will be recognized and work.
Copy linkTweet thisAlerts:
@jschnyderiteauthorOct 10.2011 — You can only have one unique ID per page. You also might need to adjust your javascript to not target $('#country'). but rather a more global $('.some_field_class_to_modify') that you can then apply to any field you want to auto-suggest for.[/QUOTE]

octoberwind, i didn't understand this part of you reply:

but rather a more global $('.some_field_class_to_modify') that you can then apply to any field you want to auto-suggest for

Can you show an example of how it would be used or explain what areas it needs to be changed so I can see if I can get this script working? I'm at a standstill with things to try.
Copy linkTweet thisAlerts:
@OctoberWindOct 10.2011 — The idea is not to use the ID's to target the fields that you want to use the auto-suggest (since you can only have 1 id per page), but rather a class that you can put on any element that you want. Otherwise, you'd have to write functionality for every input that needs auto-suggest.

[code=html]$('#country').addClass('load');[/code]

this will only target the first occurrence of [i]element#country[/i] on a given page.
Copy linkTweet thisAlerts:
@jschnyderiteauthorOct 12.2011 — The idea is not to use the ID's to target the fields that you want to use the auto-suggest (since you can only have 1 id per page), but rather a class that you can put on any element that you want. Otherwise, you'd have to write functionality for every input that needs auto-suggest.

[code=html]$('#country').addClass('load');[/code]

this will only target the first occurrence of [i]element#country[/i] on a given page.[/QUOTE]



so i should use below?
[code=html]$('.country').addClass('load');[/code]

and change any other instances of the id..thats it? should my suugestion id be a class as well? do any other areas of the code need a change?
Copy linkTweet thisAlerts:
@jschnyderiteauthorOct 12.2011 — here is my updated HTML..now filling either field fills both with the same info. i need each to work independently. any ideas?

[code=html]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Papermashup.com | jQuery PHP Ajax Autosuggest</title>


<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 {
$('.item').addClass('load');
$.post("autosuggest.php", {queryString: ""+inputString+""}, function(data){
if(data.length >0) {
$('#suggestions').fadeIn();
$('#suggestionsList').html(data);
$('.item').removeClass('load');
}
});
}
}

function fill(thisValue) {
$('.item').val(thisValue);
setTimeout("$('#suggestions').fadeOut();", 600);
}

</script>

<style>
#result {
height:20px;
font-size:16px;
font-family:Arial, Helvetica, sans-serif;
color:#333;
padding:5px;
margin-bottom:10px;
background-color:#FFFF99;
}
.item{
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"><br />
<input type="text" size="25" value="" class="item" onkeyup="suggest(this.value);" onblur="fill();" class="" />

<input type="text" size="25" value="" class="item" 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>
</form>



</body>
</html>[/code]
×

Success!

Help @jschnyderite spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...