嘗試使用ajax/php進行實時搜索(ajax)。我有(2)個文件,index.php和searching.php。我已經編寫了初始查詢來搜索數據庫,以查找輸入時給定的字段。爲了執行ajax(即搜索),我需要使用php和ajax來實現這一點。我已經實現了$ query變量來搜索框中的任何3個值(例如「輸入一個名稱」等)。任何人?我就這一個:)使用Php Ajax的Livesearch - 需要幫助!
的index.php有點絕望
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="global.css" media="screen" />
<link rel="stylesheet" href="design.css" media="screen" />
<style type="text/css">
.hidden {
display: none;
}
</style>
<script type="text/javascript" src="inc/jquery.js"> </script>
<script type="text/javascript">$(function() {$('input[type=text]').focus(function() {$(this).val('')});});</script>
<script type="text/javascript">
$(document).ready(function(){
$("input[type=radio]").click(function(){
$(".box").val(this.value);
});
});
</script>
</head>
<body>
<div id="top_wrapper_bg">
<div id="wrapper_top">
<div id="header">
<div id="logo">
<a href="index.php"><img src="img/logo-02-01-11.png" alt="logo-02-01-11" width="300" height="100" border="0" /></a>
</div><!-- /logo -->
<div id="header_image">
right
</div><! -- /header_image -->
<div id="clear"> </div><! -- /clear -->
<div id="nav">
nav
</div><! -- /nav -->
</div><!-- /header-->
</div><!-- /wrapper_top -->
</div><!-- /top_wrapper_bg -->
<div id="header_search_break"> </div>
<div id="bot_wrapper_bg">
<div id="wrapper_bottom">
<div id="search">
<span class="medfont">Search by:</span>
<form id="searchSelect" action="searching.php">
<input type="radio" name="search_value" value="Enter Professor Name">Professor
<input type="radio" name="search_value" value="Enter Course (e.g. COM 310)">Course
<input type="radio" name="search_value" value="Enter Major (e.g. Business)">Major
</form>
<br>
<input class="box" type="text" size="25" name="searchQuery" value="Select an option..." class="textField clearMeFocus" />
</div><!-- /search -->
<div id="latest_stats">
<p>Stats</p>
</div><!-- /latest_stats -->
<div id="clear"> </div>
<div id="contain_stats">
<div id="latest_prof">
latest prof
</div><!-- /latest_prof -->
<div id="top_prof">
top prof
</div><!-- /top_prof -->
<div id="clear"> </div><! -- /clear -->
</div><!-- /contain_stats -->
<br><br><br><br><br><br><br><br>
</div><!-- /wrapper_bottom -->
</div><!-- /bot_wrapper_bg -->
</body>
</html>
searching.php
<?php
require_once('dbconnec.php');
$query = $_POST['search_value'];
SELECT profID, fName, lName FROM professor WHERE fName OR lName LIKE $query; // search for professor ajax livesearch code
SELECT courseID, name, primaryCode, secondaryCode FROM course WHERE courseID OR name OR primaryCode or secondaryCode LIKE $query; // search for course ajax livesearch code
SELECT name FROM department WHERE name LIKE $query; // search by dept ajax livesearch code
?>
看來你的'searching.php'容易受到[SQL注入](http://en.wikipedia.org/wiki/SQL_injection)攻擊。請在SQL查詢中使用[prepared statements](http://php.net/manual/en/pdo.prepared-statements.php),以確保您不會將整個數據庫暴露給攻擊者。 – sarnold 2011-03-14 01:54:49