我试图创建我的注册页面,使用Ajax将数据发布到PHP文件然后PHP文件检查表单的错误并返回一些错误
我的表格
<head>
<script type="text/javascript" src="assets/extra/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#submit").click(function(){
var fname=$('#firstname').val();
var lname=$('#lastname').val();
var displayname=$('#displayname').val();
var password=$('#password').val();
var cpassword=$('#cpassword').val();
var email=$('#email').val();
var terms=$('#terms').val();
$.ajax({
type: "POST",
url: "register.php",
data: "firstname="+fname+"&lastname="+lname+"&displayname="+displayname+"&password="+password+"&cpassword="+cpassword+"&email="+email+"&term="+terms ,
success: function(html){
$("#load").css('display','block');
$("#signupform").css('display','none');
$("#box").css('display','none');
$("#loading").fadeOut('500', function(){
$("#loading").css('display','none');
$("#box").html(html).show('slow');
});
}
});
return false;
});
});
</script>
</head>
<style type="text/css">
#loading
{
display:none;
width:500px;
height:500px;
background:url(img/loading.gif) no-repeat;
}
</style>
</head>
<div id="loading" style="">
</div>
<div id="box">
</div>
<div class="form-group">
<label for="firstname" class="col-md-3 control-label">Fistname</label>
<div class="col-md-9">
<input type="text" name="firstname" id="firstname" class="form-control inp" placeholder="Fistname" minlength="3" maxlength="50" value="" required>
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-md-3 control-label">Lastname</label>
<div class="col-md-9">
<input type="text" name="lastname" id="lastname" class="form-control inp" placeholder="Lastname" minlength="3" maxlength="50" value="" required>
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-md-3 control-label">Displayname</label>
<div class="col-md-9">
<input type="text" name="displayname" id="displayname" class="form-control inp" placeholder="Displayname" minlength="3" maxlength="50" value="" required>
</div>
</div>
<div class="form-group">
<label for="email" class="col-md-3 control-label">Email</label>
<div class="col-md-9">
<input type="email" name="email" id="email" class="form-control inp" value="" placeholder="Email" required>
</div>
</div>
<div class="form-group">
<label for="password" class="col-md-3 control-label">Password</label>
<div class="col-md-9">
<input type="password" name="password" id="password" class="form-control inp" value="" placeholder="Password" required>
</div>
</div>
<div class="form-group">
<label for="password" class="col-md-3 control-label">Confirm Password</label>
<div class="col-md-9">
<input type="password" name="cpassword" id="cpassword" class="form-control inp" value="" placeholder="Confirm Password" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<label><input type="checkbox" name="terms" id="terms" value="yes" checked> Accept<a href="#" target="_blank"> Term</a></label><div id="err_terms" class="err"></div>
</div>
</div>
<!-- Button -->
<div class="form-group">
<div class="col-md-offset-3 col-md-9">
<button id="btn-signup" type="submit" class="btn btn-info signup-bt" id="submit" name="submit">   <i class="fa fa-user-plus"></i> Register</button>
</div>
</div>
</form>
这是我的PHP
if(isset($_POST['fistname']) && isset($_POST['lastname']) && isset($_POST['email']) && isset($_POST['displayname'])) {
$fname = mysql_real_escape_string($_POST['firstname']);
$lname = mysql_real_escape_string($_POST['lastname']);
$email = mysql_real_escape_string($_POST['email']);
$password = mysql_real_escape_string($_POST['password']);
$cpassword = mysql_real_escape_string($_POST['cpassword']);
$displayname = mysql_real_escape_string($_POST['displayname']);
$checkemail = mysql_query("SELECT `email` FROM `users` WHERE email='$email'");
$checkname = mysql_query("SELECT `displayname` FROM `users` WHERE displayname='$displayname'");
if(mysql_num_rows($checkemail) != 0){
echo '<span class="message error">Something</span>';
}
if(mysql_num_rows($checkname) != 0){
echo '<span class="message error">Something</span>';
}
if($terms!="yes"){
echo '<span class="message error">Something</span>';
}
if($password!=$cpassword){
echo '<span class="message error">Something</span>';
}
if($displayname==""){
echo '<span class="message error">Something</span>';
}
if($password==""){
echo '<span class="message error">Something</span>';
}
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
echo '<span class="message error">Something</span>';
}
if($terms != "yes"){
echo '<span class="message error">Something</span>';
}
else{
// QUERY
}
}
当我点击提交时,我尝试检查HTTP标头,它没有向register.php发送数据 那么..我该如何解决? 谢谢
答案 0 :(得分:0)
由于缺乏更好的描述,浏览器“混淆”会将您的按钮类型从button
更改为<button id="btn-signup" type="button" class="btn btn-info signup-bt" id="submit" name="submit">   <i class="fa fa-user-plus"></i> Register</button>
,因此它不会尝试提交:
$.ajax({
type: "POST",
url: "register.php",
data: {
"firstname": fname,
"lastname": lname,
"displayname": displayname,
"password": password,
"cpassword": cpassword,
"email": email,
"term": terms
},
success: function(html){ /* you success function here*/
});
你应该传递一个对象而不是一个字符串来发布ajax:
require.config({
waitSeconds : 20, //make sure it is enough to load all scripts
paths: {
cordova: '../lib/js/ng-cordova',
angular: '../lib/js/angular/angular', // <---- here is angular
angularAnimate: '../lib/js/angular/angular-animate',
angularTouch: '../lib/js/angular/angular-touch',
angularSanitize: '../lib/js/angular/angular-sanitize',
uiRouter: '../lib/js/angular-ui/angular-ui-router',
ionic: '../lib/js/ionic.bundle',
ionicServiceCore: '../lib/js/ionic-core',
angularIonic: '../lib/js/ionic-angular'
}
}
答案 1 :(得分:0)
你这里有一个错字:
if(isset($_POST['fistname']) && ...
应该是'firstname'。