我的表单底部有一个按钮,如果用户尝试使用的电子邮件已在使用中,我会尝试禁用它。用户提供的电子邮件是$ email
这是我的前端代码..
<script type="text/JavaScript">
var email = '<?php echo $email; ?>';//Get the value in the username textbox
$.ajax({ //Make the Ajax Request
type: "POST",
url: "ajax_check_email.php", //file name
data: "email="+ email, //data
success: function(server_response){
$("#availability_status").ajaxComplete(function(event, request){
if(server_response == '0')//if ajax_check_username.php return value "0"
{
alert("Server response recieved: 0");
$("#availability_status").html('<img src="/img/icon-success.png" align="absmiddle" style="height: 15px; width: auto; margin-top: -2px;"> <font color="#c9dc54" style="padding-top: 5px;"> Available </font> ');
$("#btn-submit-2").html('<button type="submit" class="btn btn-danger btn-lg btn-block" id="b" style="font-family: "klavikaRegular"; letter-spacing: 1px;" name="skip" value="0">CONTINUE</button>');
//add this image to the span with id "availability_status"
}
else if(server_response == '1')//if it returns "1"
{
alert("Server response recieved: 1");
$("#availability_status").html('<img src="/img/icon-error.png" align="absmiddle" style="height: 15px; width: auto; margin-top: -2px;"> <font color="#b11116" style="padding-top: 5px;">Not Available </font>');
$("#btn-submit-2").html('<a class="btn btn-default btn-lg btn-block" style="font-family: "klavikaRegular"; letter-spacing: 1px;">CONTINUE</a>');
}
});
}
});
}
else
{
alert("failed query");
$("#availability_status").html('<font color="#b11116">Username too short</font>');
$("#btn-submit").html('<a class="btn btn-default btn-lg btn-block" style="font-family: "klavikaRegular"; letter-spacing: 1px;">USERNAME TOO SHORT</a>');
//if in case the username is less than or equal 3 characters only
}
</script>
这是我的php文件源
<?php
include('database_connection.php');
//Include The Database Connection File
if(isset($_POST['email']))//If a username has been submitted
{
$email = mysql_real_escape_string($_POST['email']);//Some clean up :)
$check_for_email = mysql_query("SELECT user_login FROM wp_users WHERE user_email='$email'");
//Query to check if username is available or not
if(mysql_num_rows($check_for_email))
{
echo '1';//If there is a record match in the Database - Not Available
}
else
{
echo '0';//No Record Found - Username is available
}
}
?>
我的javascript语法有问题吗?
$ email是硬编码的,因为我知道它应该失败..
答案 0 :(得分:0)
尝试在php页面中成功写作:
显示页面
<div id="availability_status"></div>
<script type="text/JavaScript">
var email = '<?php echo $email; ?>';//Get the value in the username textbox
$(document).ready(function() {
$.ajax({ //Make the Ajax Request
url: "ajax_check_email.php?email="+email, //data
cache: false,
success: function(server_response){
$("#availability_status").html(server_response);
}
});
});
</script>
** ajax_check_email.php **
<?php
include('database_connection.php');
//Include The Database Connection File
if(isset($_GET['email'])) {
if(filter_var($_GET['email'], FILTER_VALIDATE_EMAIL)) {
$email = mysql_real_escape_string($_POST['email']);//Some clean up :)
$check_for_email = mysql_query("SELECT user_login FROM wp_users WHERE user_email='$email'");
//Query to check if username is available or not
if(mysql_num_rows($check_for_email)) { ?>
<img src="/img/icon-error.png" align="absmiddle" style="height: 15px; width: auto; margin-top: -2px;">
<font color="#b11116" style="padding-top: 5px;">Not Available </font>
<?php }
else { ?>
<img src="/img/icon-success.png" align="absmiddle" style="height: 15px; width: auto; margin-top: -2px;"> <font color="#c9dc54" style="padding-top: 5px;"> Available </font>
<button type="submit" class="btn btn-danger btn-lg btn-block" id="b" style="font-family: "klavikaRegular"; letter-spacing: 1px;" name="skip" value="0">CONTINUE</button>
<?php }
}
else { ?>
<h2>Invalid Email Address.</h2>
<?php }
} ?>
答案 1 :(得分:0)
是的,您的JavaScript上存在语法错误。
}
else
{
alert("failed query");
$("#availability_status").html('<font color="#b11116">Username too short</font>');
$("#btn-submit").html('<a class="btn btn-default btn-lg btn-block" style="font-family: "klavikaRegular"; letter-spacing: 1px;">USERNAME TOO SHORT</a>');
//if in case the username is less than or equal 3 characters only
}
那里的括号确实没有关闭任何东西,加上那个其他没有 if (唯一一个真的在$ .ajaxComplete()调用内)。
您不需要像这样使用$ .ajaxComplete。
$.ajax({success: function(data){
if(data == '1') {
//If server returns 1 do...
} else if(data == '0') {
//If server returns 0 do...
} else {
//Nothing returned
}
}
});
另外,将AJAX的dataType指定为文本。
$.ajax({dataType: 'text',
success: ... });