如何爲ajax做到這一點?感謝您的幫助!Ajax問題
這裏的表單代碼
<form action="" method="post">
<p>Search for a name:</p>
<p><input type="text" name="name" id="name" /></p>
<input type="submit" name="submit" id="submit" value="Search Name" />
</form>
<div id="output"></div>
和它下面
$("input#submit").click(function(){
var name_val = $("input#name").val();
$.ajax({
type: "POST",
url: "select.php",
dataType: "json",
data: {
name: name_val
},
success: function(data){
if(data.error == true){
$("#output").append("<p>"+ name_value +" is on the list.</p>");
} else {
$("#output").append("<p>"+ name_value +" is not the list.</p>");
}
}
});
})
</script>
的JavaScript和這裏的select.php裏面的代碼
if(isset($_POST['submit'])){
$name = $_POST['name'];
$sql = "SELECT * FROM persons WHERE FirstName = '{$name}'";
$result = mysql_query($sql, $con);
if(!$result){
die("Database query failed: ". mysql_error());
}
while($row = mysql_fetch_array($result)){
if(in_array($name, $row)){
$return['success'] = true;
echo json_encode($return);
} else {
$return['success'] = false;
echo json_encode($return);
}
}
}
任何不工作? – Neal
如果'$ _POST ['name]'=='x'; DROP TABLE人員; - ?? – Mike
你的結果是什麼?或者沒有結果? – ryryan