Chcel som si spravit vyhladavanie u seba na svojom blogu a napadlo ma to spravit za pomoci ajaxu, vid
http://jan.fecik.sk/search.html a zaujimalo by ma ci je to dobre riesenie? Riesim to takto:
Kód:
<html>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<head>
<script type="text/javascript">
function showHint(str) {
if (str.length==0) {
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest(); // code for IE7+, Firefox, Chrome, Opera, Safari
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); // code for IE6, IE5
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","search.php?query="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<input type="text" onkeyup="showHint(this.value)" size="20" />
</form>
<p><span id="txtHint"></span></p>
</body>
</html>
search.php
Kód:
<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("databaza", $con);
mysql_query("SET CHARACTER SET utf8");
$query=mysql_real_escape_string($_GET["query"]);
if(strlen($query) >= 3) {
$result = mysql_query("SELECT * FROM tabulka WHERE asdf LIKE '%$query%' ORDER BY id DESC");
if(mysql_num_rows($result)==null) { echo 'nic'; } else {
while($row = mysql_fetch_array($result)) {
echo '<p>'.mysql_real_escape_string($row['asdf']).'<hr></p>';
}
}
} else { echo 'zadaj minimalne 3 znaky'; }
mysql_close($con);
?>
a zaujimalo by ma, ci je to dobre riesenie, ci tym znacne nevytazujem db server, ako by sa to chovalo ak tam bude stovka clankov a ci je to aj bezpecne riesnie...?