我想插入一些東西到數據庫中。我調用了PHP到$ .ajax。 textstatus顯示'成功',但表格如何仍然返回零結果?需要幫助非常糟糕。謝謝。
HTML:
<div id="reg">
<form id="regform" name="regform" method="post">
Name: <input type="text" name="name"/>
<label id="name_error" for="name" class="error">Enter Your Complete Name.</label><br/>
<input type='submit' name="Submit Name" class="submit"/>
</form>
</div>
JQUERY:
$(function() {
$('.error').hide();
$(".submit").click(function() {
$('.error').hide();
var name= $("input#name").val();
if(name==""){
$("label#name_error").show();
$("input#name").focus();
return false;
}
$.ajax({
type: "POST",
url: "http://localhost/folder1/folder2/reg.php";
sync: false,
data: {name:name},
success: function(data, textStatus){
//alert(data);
document.getElementById('reg').innerHTML = data + textStatus;
},
error: function(jqXHR){
alert(jqXHR.responseText);
}
});
return false;
});
});
PHP/reg.php
<?php
include "dbcon.php";
$name=$_POST['name'];
$query=mysql_query("INSERT INTO regtable ('name'), VALUES('$name')") or die();
if($query){
echo "1";
}else{
echo "2";
}
?>
將這段代碼
以逗號出來的查詢。 – Archer
不應該先使用'$ _POST'值,而不要先清理它。我建議更新你的當前代碼來使用PHP的PDO - 這是一個很好的教程:http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-訪問/ – Joe