我想检查电子邮件的可用性,但这里出了点问题。在我的表格中:
<input id="inpMail" name="email" type="text" placeholder="E-mail">
JS
$(document).ready(function() {
$("#inpMail").change(function() {
var mail = $("#inpMail").val();
var msgbox = $("#status"); //`status` is a div
if (mail.length > 4) {
$("#status").html('<img src="img/loader.gif" align="absmiddle"> Checking availability...'); //this works
$.ajax({
type: "POST",
url: "ajax.php", // this file is in the same folder
data: "mail="+ mail,
success: function(msg) {
$("#status").ajaxComplete(function(event, request){
if (msg == 'OK') {
msgbox.html('<img src="img/available.png" align="absmiddle">'); //doesn't work
}
else {
msgbox.html(msg); // doesn't work
}
});
}
});
}
else {
$("#status").html('<font color="#cc0000">Please enter atleast 5 letters</font>'); //this works
}
return false;
});
});
ajax.php
$conn = mysql_connect("localhost","root","") or die("Database not connected"); // this message never appears
$db = mysql_select_db("vedic", $conn) or die("Database not connected");
if (isset($_POST['inpMail']))
{
$mail = $_POST['inpMail'];
$sql = mysql_query("select id from members where email='$mail'");
// my db is named `vedic`, table is `members` some fields are `id` and `email`
if (mysql_num_rows($sql))
{
echo '<STRONG>'.$mail.'</STRONG> is already in use.';
}
else
{
echo 'OK';
}
}