PHP와 XML을 사용하는 웹 페이지를 만드는 데는 비교적 새로운 편이지만 W3S Schools에서 보았던 somthine에 관심이 있습니다. 예제 페이지에서 보여주는 AJAX 라이브 검색을 만들고 싶지만 먼저 실행 방법을 배우는 데 도움이 필요합니다. (http://www.w3schools.com/php/php_ajax_livesearch.asp) 웹 사이트에있는 세 개의 코드 파일을 복사하고 html 파일은 모두 빈 폼 상자입니다. 어떻게 든 MySql과 연결해야합니까? 그렇다면 어떻게해야할까요?PHP 예제 AJAX 라이브 검색
index.html을 :
<html>
<head>
<script>
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","livesearch.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<input type="text" size="30" onkeyup="showResult(this.value)">
<div id="livesearch"></div>
</form>
</body>
</html>
livesearch.php :
는<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("links.xml");
$x=$xmlDoc->getElementsByTagName('link');
//get the q parameter from URL
$q=$_GET["q"];
//lookup all links from the xml file if length of q>0
if (strlen($q)>0)
{
$hint="";
for($i=0; $i<($x->length); $i++)
{
$y=$x->item($i)->getElementsByTagName('title');
$z=$x->item($i)->getElementsByTagName('url');
if ($y->item(0)->nodeType==1)
{
//find a link matching the search text
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q))
{
if ($hint=="")
{
$hint="<a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
}
else
{
$hint=$hint . "<br /><a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
}
}
}
}
}
// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint=="")
{
$response="no suggestion";
}
else
{
$response=$hint;
}
//output the response
echo $response;
?>
links.xml : 어떤 도움
<!-- Edited by XMLSpy® --><pages><link><title>HTML a tag</title><url>http://www.w3schools.com/tags/tag_a.asp</url></link><link><title>HTML br tag</title><url>http://www.w3schools.com/tags/tag_br.asp</url></link><link><title>CSS background Property</title><url>http://www.w3schools.com/cssref/css3_pr_background.asp</url></link><link><title>CSS border Property</title><url>http://www.w3schools.com/cssref/pr_border.asp</url></link><link><title>JavaScript Date Object</title><url>http://www.w3schools.com/jsref/jsref_obj_date.asp</url></link><link><title>JavaScript Array Object</title><url>http://www.w3schools.com/jsref/jsref_obj_array.asp</url></link></pages>
덕분에
선택하신 마스터 검색을보실 수 있습니다. 정말 대단합니다! – Si8
이 파일을 웹 서버에 복사 했습니까? 이 예제에서는 데이터베이스 액세스가 필요하지 않습니다. 모든 관련 데이터는 .xml 파일에 있습니다. – sofl
@ sofl 나는 이것을 텍스트 랭글러에 복사하고 문서 폴더의 폴더에 저장했습니다. 이것의 아무도는 웹 서버에서 i didnt는 내가 그것을 필요로 할 것이다라고 생각한다. 그러나 그것이 didnt 할 때 suprised했다. 그것들은 같은 폴더에있는 3 개의 개별 파일 들로서 붙여 넣기 방식과 동일합니다. @ SiKni8 당신은 링크를 배치 할 수 구글 그냥 전설 물건의 리그를 제공합니다 –