AJAX form problem
I have javascript as follows :
<script type="text/javascript">
function lookup(inputString) {
if(inputString.length == 0) {
$('#suggestions').hide();
} else {
$.post("./index.php?menu=getmyFavList&ajax=ajax"
, {queryString: ""+inputString+""}
, function(data){
if(data.length >0) {
$('#suggestions').show();
$('#autoSuggestionsList').html(data);
}
});
}
}
function fill(thisValue) {
$('#inputString').val(thisValue);
setTimeout("$('#suggestions').hide();", 200);
}
</script>
And my corresponding php code is
$sql = "SELECT
u.UM_index
, u.UM_first_name
, u.UM_last_name
, p.bz_pro_city
, p.bz_pro_country
FROM
tbl_user_master As u
, tbl_profile AS p
WHERE
u.UM_index = p.bz_pro_id
AND
UM_first_name LIKE '%" . $q . "%'
AND
开发者_如何学JAVA UM_is_active = 'yes'
";
$res = mysql_query($sql) or die("unable to execute query");
while($row = mysql_fetch_array($res))
{
echo '<li onClick="fill(\''.$row['UM_first_name']
. ' ' . $row['UM_last_name'] . '\');">'
. $row['UM_first_name']
. ' ' . $row['UM_last_name']
. ',' . $row['bz_pro_city']
. '</li>';
}
And the HTML for is like :
<input type="text" name="reciever_name" size="37" id="inputString" onkeyup="lookup(this.value)" onblur="fill()" />
Now my problem is I need to pass the index
of the selected user which is in tbl_user_master
. How can I do this (of course hiding the index from the sender)?
Why not put the index
in an <input type="hidden">
and add it to your querystring
?
Edit What you need to do is change what happens when a user selects an item.
In your while
you'll need to change the echo
to
echo "
<li onclick=\"fill('{$row['UM_first_name']} {$row['UM_last_name']}'
, '{$row['UM_index']}')\">
{$row['UM_first_name']} {$row['UM_last_name']}, {$row['bz_pro_city']}
</li>"
And change your JavaScript
fill
function to
function fill(thisvalue, thisid){
$('#inputString').val(thisValue);
$('#selectedID').val(thisid);
setTimeout("$('#suggestions').hide();", 200);
}
You'll also need to add
<input type="hidden" name="thisid" id="thisid" value=""/>
When the form is submitted, you would access the ID of the selected item using $_POST['thisid']
精彩评论