我開始探索ajax,不知何故,我對它很好,不太難,也不容易。無論如何,我對這個項目有困難。它涉及ajax,php,js,mysql。我其實是試圖複製Facebook的標籤風格。當您在帖子中標記某人時,全名會轉向指向標籤用戶個人資料的超鏈接。這只是我感覺的一個小問題,它只是我對JS的新手。我已經做了其他的休息,但我有問題將文本轉換爲與js的超鏈接。我想在putin_livesearch()代碼中使用它。Javascript將文本從數據庫轉換爲超鏈接
這是我的代碼。
的index.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>FB TAG</title>
<style>
.namesearch {
cursor:pointer;
}
</style>
<script type="text/javascript">
function putin_livesearch(fname, lname)
{
//document.getElementById('searchbar').value = (fname + " " +lname);
//document.location.href='#'+ window.document.getElementById('searchbar').value = (fname);
window.document.location.href="#"+window.document.getElementById('searchbar').value;
}
function showResult(str)
{
if (str.length==0)
{
document.getElementById("livesearch").innerHTML="";
document.getElementById("livesearch").style.border="0px";
return;
}
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("livesearch").innerHTML=xmlhttp.responseText;
document.getElementById("livesearch").style.border="1px solid #A5ACB2";
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<input id ="searchbar" name="wallpost" type="text" onkeyup="showResult(this.value)" />
<div id="livesearch"></div>
</form>
</body>
</html>
getuser.php
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("place_main", $con);
$sql=" SELECT * FROM profiles WHERE firstname like '".$q."%' ";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$fname = $row['firstname'];
$lname = $row['lastname'];
echo '<span class ="namesearch" onclick="putin_livesearch(\''.$fname.'\',\''.$lname.'\' )"> '. $fname ." ". $lname .' </span><br />';
//echo "<span class ='namesearch' onclick='putin_livesearch(" .$fname. ", " .$lname. ")'> " . $fname . " " . $lname . "</span><br />";
}
mysql_close($con);
?>
我已經試過,其實。這個問題是當我搜索一個人的名字。它會顯示一個超鏈接的結果,問題是當我點擊它,它會重定向到一個新的頁面。這不是我想要的。我真的希望它在帶有超鏈接的文本框中顯示。 – electricfeel1979