我試圖檢查剛剛輸入的電子郵件用戶是否在數據庫中,並顯示message.its工作正常。但是當我刪除輸入的值並聚焦時,則會顯示其他選項。但我不希望顯示neither.Here是我的代碼的Ajax文本類型輸入元素的默認值
<script>
$(document).ready(function(){
$("#email").focusout(function(){
var emailVal = $('#email').val();
$.ajax({
type: 'post',
url: 'check_email.php',
data: {
myparam:emailVal //set it with a parameter name
},
success: function(data) {
alert(data);
if(data==1){
document.getElementById('email_check').innerHTML="<p style='color:red;'>The email you have entered is already exist!</p>";
}
if(data==0){
document.getElementById('email_check').innerHTML="<p style='color:green;'>The email you have entered is available!</p>";}
}
});
});
});
</script>
這裏是我的check_email.php文件,
<?php
require_once("connnection.php");
$con=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($_POST['myparam'])){
$email=mysqli_real_escape_string($con,$_POST['myparam']);
$sql = "SELECT * FROM user WHERE email='$email'";
$select=mysqli_query($con,$sql) or die("fail");
$row = mysqli_num_rows($select);
if ($row >0) {
echo 1;
}else {
echo 0;
}
else
echo "post error";
後刪除輸入值和重點出(點擊另一個地方) ,發生以下情況,使用警報(數據)我從服務器得到0作爲數據。這怎麼可能發生。如果文本字段中沒有值,那麼它是如何進入到php文件中的sql查詢段的。是否有任何默認值或在場景後面傳遞的內容?請解釋?
感謝....你的幫助 – cmit 2014-11-02 12:54:23