我想插入一些东西到数据库中。我调用了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