我正在使用jQuery和PHP使用AJAX开发一个简单的注册页面。
以下是进行ajax调用的脚本:
<script>
function submitForm(){
var data1=$('#regform').serialize();
$.ajax({
type:'POST',
url:'signup.php',
data:data1,
success: function(response){
console.log(response);
if(response==1)
alert('taken');
else if(response==2)
alert('registered');
else
alert(response);
}
});
}
</script>
以及响应该调用的PHP脚本:
signup.php:
<?php
require_once 'dbconnect.php';
if($_POST) {
$user_name = $_POST['user_name'];
$user_email = $_POST['user_email'];
$user_password = $_POST['password'];
try {
$sth = $dbh->prepare("SELECT * FROM logindata WHERE email=:email");
$sth->execute(array(":email"=>$user_email));
$count = $sth->rowCount();
if($count==0){
$sth = $dbh->prepare("INSERT INTO logindata(username,email,pass) VALUES(:uname, :email, :pass)");
$sth->bindParam(":uname",$user_name);
$sth->bindParam(":email",$user_email);
$sth->bindParam(":pass",$user_password);
if(!$sth->execute()) {
echo "3";
} else {
echo "2";
}
} else{
echo "1";
}
}
catch(PDOException $e){
echo $e->getMessage();
}
}
?>
使用PDO。
dbconnect.php:
<?php
$dbhost='localhost';
$dbuser='root';
$dbpass='';
$dbname='ambitio';
try{
$dbh=new PDO("mysql:host={$dbhost};dbname={$dbname}",$dbuser,$dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e){
echo $e->getMessage();
}
?>
问题:
这两个响应都没有被返回到jQuery ajax调用。我查看了console.log()
,但浏览器中没有显示任何内容。数据存储在MySQL数据库中(检查它),这意味着execute()
在PHP中工作正常,但浏览器中仍然没有显示警报。如果1
评估为count
,则会返回0
。
此外,当我在没有实际提交表单的情况下刷新表单时,我在浏览器控制台上收到错误POST 412 (Precondition Failed)
。
编辑:发布完整的php。
EDIT2:Apache访问日志
::1 - - [19/Jul/2016:23:51:55 +0530] "GET /ambitio/css/bootstrap.css HTTP/1.1" 304 - "http://localhost/ambitio/signup.html" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
::1 - - [19/Jul/2016:23:51:55 +0530] "GET /ambitio/css/overboot.css HTTP/1.1" 304 - "http://localhost/ambitio/signup.html" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
::1 - - [19/Jul/2016:23:51:55 +0530] "GET /ambitio/css/font-awesome.css HTTP/1.1" 304 - "http://localhost/ambitio/signup.html" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
::1 - - [19/Jul/2016:23:51:55 +0530] "GET /ambitio/js/jquery.js HTTP/1.1" 304 - "http://localhost/ambitio/signup.html" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
::1 - - [19/Jul/2016:23:51:55 +0530] "GET /ambitio/js/bootstrap.js HTTP/1.1" 304 - "http://localhost/ambitio/signup.html" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
::1 - - [20/Jul/2016:00:43:36 +0530] "GET /ambitio/signup.html HTTP/1.1" 200 1454 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
::1 - - [20/Jul/2016:00:43:46 +0530] "POST /ambitio/signup.html HTTP/1.1" 200 1454 "http://localhost/ambitio/signup.html" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
::1 - - [20/Jul/2016:00:43:46 +0530] "POST /ambitio/signup.php HTTP/1.1" 200 1 "http://localhost/ambitio/signup.html" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
EDIT3:表单HTML标记:
<form method="post" id="regform" onSubmit="submitForm()">
<input type="text" placeholder="Username" id="user_name" name="user_name" />
<input type="email" placeholder="Email" id="user_email" name="user_email" />
<input type="password" placeholder="Password" id="password" name="password" />
<input type="password" placeholder="Retype password" id="rpassword" name="rpassword" />
<input type="submit" id="submit"/>
</form>
答案 0 :(得分:2)
由于您的表单不包含要发送的网址,而您的onSubmit
- 处理程序(submitForm
)没有返回false
,表单实际上会获得由浏览器发布到它当前所在的页面(很可能无法处理表单的POST数据),只会重新加载包含页面的登录表单。
POST可能会发送到您的ajax脚本以及,但您的浏览器不会等待响应,因为它已经继续。
有关提交流程的详细信息,请参阅https://html.spec.whatwg.org/multipage/forms.html#concept-form-submit。
解决方案是将return false
添加到submitForm
函数或onSubmit
处理程序(;return false
)