当我想将用户添加到数据库,但字段未填充时,调用工作正常,页面不更新,只有表单。当我填写字段并单击添加页面更新。有什么问题?
它显示没有更新print_r($_POST);
谢谢。
<form action="" method="POST" id="reg">
<input type="text" name="First" id="fn" value="">
<input type="text" name="Last" id="ln" value="">
<input type="submit" name="post_user" onclick="add_ajax_rquest()" value="Add" >
</form>
function add_ajax_rquest() {
var fn = document.getElementById('fn').value,
ln = document.getElementById('ln').value;
var request = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
request.open("POST", 'regist.php', true);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.send("&First="+ fn + "&Last="+ ln);
request.onreadystatechange = function() {
if (request.readyState == 4 && this.status == 200) {
document.getElementById('txtHint').innerHTML = request.responseText;
}
}
}
$name = $_POST['First'];
$last_name = $_POST['Last'];
echo $name;
echo $last_name;
if ( $name == '' && $last_name == '' ) {
echo "Fields name is not filing!";
} else {
$queryInsertName =
"INSERT INTO
users ( `user_first_name`,
`user_last_name` )
VALUES ( '$name', '$last_name' )";
$resultName = mysqli_query($con, $queryInsertName);
}
print_r($_POST);}
答案 0 :(得分:0)
所以你在一个也发布的表单里面做一个ajax请求?你自己很困惑。如果你做ajax,那么不要做表单,如果你正在做表单,不要做ajax。例如,如果您正在执行ajax,请删除表单包装器,因为您不需要它。此外,你正在以最糟糕的方式做ajax。从这里使用JQUERY:Post Values via AJAX with PHP and JQUERY