PDA

View Full Version : getting annother value


gavimobile
01-29-2010, 09:32 PM
hi folks,
my javascript is poor, could someone give me a hand here?

i have a function which sends the value of a field to a php page and returns an autocomplete result list based on the keyes entered in the field. however i have alot of rows in my database and i would like to improve the response time by adding an additional field.

This is the javascript function i need to edit (just not sure how).

<script type="text/javascript">
function lookup(inputString) {
if(inputString.length == 0) {
// Hide the suggestion box.
$('#suggestions').hide();
} else {
$.post("rpc.php", {queryString: ""+inputString+""}, function(data){
if(data.length >0) {
$('#suggestions').show();
$('#autoSuggestionsList').html(data);
}
});
}
} // lookup
function fill(thisValue) {
$('#inputString').val(thisValue);
setTimeout("$('#suggestions').hide();", 200);
}
</script>


inputString is the id of the autocomplete field, this is how javascript is posting the entered keys to the rpc.php file which allows the databse query to list results. i created a new field called city and i edited the php query. i want javascript to make sure city will be selected before so there will be less street names in the results which will allow the whole process to work faster.

do i need to create a new function? can i apply this to the same function?

here is the php file (rpc.php)
<?php require_once("Connections/connect.php"); ?>
<?php
if(isset($_POST['queryString'])) {
$city = trim($_POST['city']);
$trim = trim($_POST['queryString']);
$queryString = mysql_real_escape_string($trim);
$cityname = mysql_real_escape_string($city);
if(strlen($queryString) >0) {
mysql_select_db($database_connect, $connect);
$sql = "SELECT * FROM table WHERE city = '$city' AND street LIKE '%$queryString%' LIMIT 5";
$result = mysql_query($sql, $connect) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo '<li onClick="fill(\''.$row['street'].'\');">'.$row['street'].'</li>';
}
}
}
?>

here is a more detailed post

<html>
<head>
<script src="js/jquery-1.2.6.js" type="text/javascript"></script>
<script src="js/ui.core.js" type="text/javascript"></script>
<script src="js/ui.tabs.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css"href="css/flora.tabs.css" />
<link rel="stylesheet" type="text/css" href="css/autocomplete.css" />
<script type="text/javascript">
function lookup(inputString) {
if(inputString.length == 0) {
// Hide the suggestion box.
$('#suggestions').hide();
} else {
$.post("rpc.php", {queryString: ""+inputString+""}, function(data){
if(data.length >0) {
$('#suggestions').show();
$('#autoSuggestionsList').html(data);
}
});
}
} // lookup
function fill(thisValue) {
$('#inputString').val(thisValue);
setTimeout("$('#suggestions').hide();", 200);
}
</script>
</head>
<body>
<form method="post" name="form1" action="">
<select name="city" id="city" >
<options value="">Select City</option>
<options value="city">cityname</option>
</select>
<input type="text" name="address" id="inputString" onblur="fill();" onkeyup="lookup(this.value);" value="" size="32">
<div class="suggestionsBox" id="suggestions" style="display: none;">
<div class="suggestionList" id="autoSuggestionsList">&nbsp;</div>
</div>
</form>
</body>
</html>


hope i was clear? please ask me if i wasnt.

tia
gavriel