2013-01-07 78 views
0

我正在創建一個社交網站。我在search.php的文本框中搜索存儲在數據庫中的場所。當用戶開始鍵入場地名稱時,數據庫中存儲的場地名稱列表應該是加載到div venuesearch。這工作正常。我的問題是,我需要在div venuesearch可點擊內的行,以便當用戶單擊一個特定的行時,該值應該來到文本框。連續點擊

的search.php

<script language="javascript"> 
function showData(str) 
{ 
    if (str.length==0) 
    { 
     document.getElementById("venuesearch").innerHTML=""; 
     document.getElementById("venuesearch").style.border="0px"; 
     return; 
    } 
    var venue_data = document.getElementById("venue").value; 
    if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp=new XMLHttpRequest(); 
    } 
    else 
    {// code for IE6, IE5 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.onreadystatechange=function() 
    { 
     if (xmlhttp.readyState==4 && xmlhttp.status==200) 
     { 
      document.getElementById("venuesearch").innerHTML=xmlhttp.responseText; 
      document.getElementById("venuesearch").style.border="1px solid #A5ACB2";  
      document.getElementById("venuesearch").style.zIndex = "100"; 
     } 
    } 
    xmlhttp.open("GET","aj_search.php?venue="+str,true); 
    xmlhttp.send(); 
} 
</script> 
</head> 

<body> 

<input id="venue" name="venue" type="text" onkeyup="showData(this.value)" value="Tag venue name"/> 
<div id="venuesearch"> 
</div> 
</body> 

aj_search.php

<?php 
$dbhandle=mysql_connect("localhost","root","")or die("Unable to connect"); 
$select=mysql_select_db("scenekey",$dbhandle) or die("Unable to connect"); 

    if(isset($_GET['venue'])) 
    { 
    $venue_name = $_GET['venue']; 
    } 
    $hint=''; 
    $query_get_venue = "SELECT * from sk_accounts WHERE acnt_member_class='venue' and acnt_fname LIKE '".$venue_name."%'"; 

    $result_query_get_venue = mysql_query($query_get_venue); 
    $row_count = mysql_num_rows($result_query_get_venue); 
if($row_count > 0) 
{ 
    $hint = "<table>"; 
    while($row = mysql_fetch_array($result_query_get_venue)) 
    { 

     $act_fname = $row['acnt_fname']; 
     $act_lname = $row['acnt_lname']; 
     $act_name = $act_fname . ' ' . $act_lname; 
     $hint.= "<tr><td>".$act_name."</td></tr>"; 
    } 
    $hint .= "</table>"; 
} 
    if ($hint == "") 
    { 
    $response="no suggestion"; 
    } 
    else 
    { 
$response=$hint; 
     } 

    //output the response 
    echo $response; 
     ?> 
+0

你解決你的問題? – Vimalnath

回答

1

添加idclass,然後設置單擊事件相同或添加javascript功能

$hint.= "<tr onClick ='hintVal(".$act_name.");'><td>".$act_name."</td></tr>"; 

在javascript中:

function hintVal(trVal) { 
alert('tr clicked'); 
alert('tr value:'+trVal); 
} 
+0

'$(this.val()'是Jquery,他沒有使用它。) – Sahal

+0

@sahal在這種情況下,OP可以在函數本身傳遞tr值: 'hintVal(「。$ act_name。」);' – Vimalnath

+0

@ sajal感謝指針 – Vimalnath