我使用下面的脚本来检索回调结果,但是提交表单时什么都没有返回,下面的代码有什么问题?以及如何使每个错误输出到 div 标签id='msg'
?
这是我的注册表的完整脚本:
<script type="text/javascript">
$(document).ready(function() {
$('#wait').hide();
$('#wait').ajaxStart(function() {
$(this).show();
}).ajaxComplete(function() {
$(this).hide();
});
$('#btn_register').click(function(){
var parameters = $('#register_form').serialize();
//alert(parameters);
$.ajax({
url: 'request.php',
type: 'POST',
data: parameters,
dataType: 'json',
success: function(output_str){
if(output_str == "error"){
alert('Error: '+ output_str);
}else{
alert(output_str);
//$('#result_msg').html(output_str);
}
},
error: function(){
console.log(arguments);
}
});
});
});
</script>
<div class="content">
<div>
<div class="title-bar" align="center"><h2 class="title">Registration Form</h2></div>
<div class="inner-wrap pad">
<div id="msg"></div>
<div align="center">
<form action="" method="post" id="register_form" enctype="multipart/form-data">
<table width="60%" border="0" cellspacing="1" cellpadding="5" class="tb">
<tr>
<td width="30%" class="r"><label for="email">Email</label></td>
<td width="70%"><input type="text" id="email" name="email" maxlength="50" value="" autofocus autocomplete="off" /></td>
</tr>
<tr>
<td class="r"><label for="password">Password</label></td>
<td><input type="password" id="password" name="password" style="width:150px;" value="<?php echo isset($_POST['password']) ? $_POST['password'] : '' ?>" /></td>
</tr>
<tr>
<td class="r"><label for="rpassword">Retype Password</label></td>
<td><input type="password" id="rpassword" name="rpassword" style="width:150px;" value="<?php echo isset($_POST['rpassword']) ? $_POST['rpassword'] : '' ?>" /></td>
</tr>
<tr>
<td class="r"><label for="fname">Full Name</label></td>
<td><input type="text" id="fname" name="fname" maxlength="50" value="<?php echo isset($_POST['fname']) ? $_POST['fname'] : '' ?>" /></td>
</tr>
<tr>
<td class="r"><label for="contact">Contact Number</label></td>
<td><input type="text" id="contact" name="contact" maxlength="15" value="<?php echo isset($_POST['contact']) ? $_POST['contact'] : '' ?>" /></td>
</tr>
<tr>
<td class="r"><label for="dob">Date of Birth</label></td>
<td>
<input type="text" id="dd" name="dd" style="width:24px;" maxlength="2" value="<?php echo isset($_POST['dd']) ? $_POST['dd'] : '' ?>" placeholder="dd" /> /
<input type="text" id="mm" name="mm" style="width:24px;" maxlength="2" value="<?php echo isset($_POST['mm']) ? $_POST['mm'] : '' ?>" placeholder="mm" /> /
<input type="text" id="yy" name="yy" style="width:40px;" maxlength="4" value="<?php echo isset($_POST['yy']) ? $_POST['yy'] : '' ?>" placeholder="yyyy" />
</td>
</tr>
<tr>
<td class="r"><label for="capcha"></label></td>
<td>
<div style="padding-bottom:3px;">
<img id="captcha" src="inc/securimage/securimage_show.php" alt="CAPTCHA Image" />
<a href="#" onclick="document.getElementById('captcha').src='inc/securimage/securimage_show.php?' + Math.random(); return false" class="capcha">Refresh</a>
</div>
<div><input type="text" name="ct_captcha" size="12" maxlength="8" id="capcha" style="width:100px;" /></div>
</td>
</tr>
<tr>
<td></td>
<td><a href="javascript:void(0);" class="btn" id="btn_register">Proceed</a> <span id="wait"><img src="images/ajax-loader.gif" /></span></td>
</tr>
</table>
</form>
</div>
</div>
</div>
请求.php
<?php
include('connectdb.php');
include('functions.php');
$email = mysql_real_escape_string(strtolower(trim($_POST['email'])));
$password = mysql_real_escape_string(trim($_POST['password']));
$error = NULL;
if(!check_email_address($email)){
$error .= "<li>Invalid Email Address";
}
if(empty($password) || strlen($password) < 0){
$error .= "<li>Invalid Password";
}
// if found invalid input
if(!empty($error)){
$output_str = "error";
}else{
$output_str = "No Error";
}
echo json_encode($output_str);
?>
谢谢。
更新:
控制台错误:
[Object { readyState=
4
, responseText=
"<br />\n<b>Notice</b>: ...<b>14</b><br />\n"error""
, status=
200
, more...},
"parsererror"
,
SyntaxError: JSON.parse: unexpected character
[Break On This Error]
return window.JSON.parse( data );
jquery-1.7.2.js (line 564)
]
0
Object { readyState=
4
, responseText=
"<br />\n<b>Notice</b>: ...<b>14</b><br />\n"error""
, status=
200
, more...}
1
"parsererror"
2
SyntaxError: JSON.parse: unexpected character
[Break On This Error]
return window.JSON.parse( data );
jquery-1.7.2.js (line 564)