2017-06-12 68 views
0

我正在检查电子邮件ID是否可用在数据库中使用ajax这是工作。我有一个提交按钮,并在页面加载禁用。我必须启用该按钮当用户输入数据库上可用的正确电子邮件地址时。如果电子邮件在数据库中可用,则该按钮将启用,否则按钮将被禁用。如果条件存在,则存在一些问题。我试过按钮仍然是同样的问题。你能帮我吗?接收成功响应,但仍然使用ajax禁用按钮

$("input[type='submit']").removeAttr("disabled"); 
    $("input[type='submit']").prop('disabled', false); 

如果我使用CSS的按钮,然后禁用不起作用。

的Html

<input type="email" id="email" name="email" class="text_field" /> 
<span id="email-validation-error" class="error"></span> 
<input id="id" type="submit" name="next" value="submit" > 

阿贾克斯

$(document).ready(function() 
    { 
    $("input[name='email']").on('keyup',function() 
     { 
     var email = $('#email').val(); 
     $.ajax(
     { 
      url:'process.php', 
      type:'POST', 
      data:'email='+email, 
      success:function(data) 
      { 
       if (data == 1) { 
      $('input[type="submit"]').attr('disabled' , false); 
       } 
       else{ 
       $("#email-validation-error").html(data); 
       $('input[type="submit"]').attr('disabled', true); 
       } 
      }, 
     }); 
    }); 
}); 

//Disable the button on page load 
    $(document).ready(function() { 
    $('input[type="submit"]').attr('disabled', true); 
    }); 

Process.php

include('db/connection.php'); 

if(isset($_POST['email'])){ 
$email=$_POST['email']; 
$query="SELECT Email FROM `request` WHERE Email='".$email."'"; 
$result = $conn->query($query); 

$search_record=$result->num_rows; 

if ($search_record == 0) { 
    echo "Email does not exist, please sign up to use our services"; 
    } 
} 
+0

你会得到什么问题?你的代码看起来不错? https://jsfiddle.net/maddak7q/ – Mark

+0

感谢您回复Mr.Mark。输入电子邮件ID提交按钮将激活,但我不知道为什么我的代码不工作 –

回答

0

最后,我发现我与Mr.Ahmed Ginani

HTML的帮助

<!DOCTYPE html> 
<html> 
<head> 
<title></title> 
</head> 
<body> 
<form > 
<input type="email" id="email" name="email" class="text_field" /> 
<span id="email-validation-error" class="error"></span> 


<input id="id" type="submit" name="next" value="submit" disabled> 
</form> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<script type="text/javascript"> 


$(document).ready(function() 
{ 
var elem = $("#id"); //assign target element with id 
$(elem).attr('disabled', true); 
$("input[name='email']").bind('change',function() // Changes from key press to change and bind 
{ 
var email = $('#email').val(); 
$.ajax( 
{ 
url:'process.php', 
type:'POST', 
data:'email='+email, 
success:function(data) 
{ 
if (data == 'success') { // getting success name from process.php page 
$("#id").attr('disabled' , false); 
$("#email-validation-error").html(''); //Change here for hiding the error message 
} 
else{ 
$("#email-validation-error").html(data); 
$('#id').attr('disabled', true); 
} 
}, 
}); 
}); 
}); 

</script> 
</body> 
</html> 

Process.php

答案
if(isset($_POST['email'])){ 
$email=$_POST['email']; 
$_SESSION['username']=$email; 

$query="SELECT Email FROM `request` WHERE Email='".$email."'"; 
$result = $conn->query($query); 

$search_record=$result->num_rows; 

if ($search_record > 0) { 
    echo "success"; 
     } 
    else{ 
    echo "Email does not exist, please sign up to use our services"; 
    } 

} 
0

尝试这个 -

$(document).ready(function() 
    { 
    var elem = $("#id"); //assign target element with id 
    $("input[name='email']").on('keyup',function() 
     { 
     var email = $('#email').val(); 
     $.ajax(
     { 
      url:'process.php', 
      type:'POST', 
      data:'email='+email, 
      success:function(data) 
      { 
       if (data == "ok") { 
      $(elem).attr('disabled' , false); //here pass elem 
       } 
       else{ 
       $("#email-validation-error").html('Email not available'); 
       $(elem).attr('disabled', true); //here pass elem 
       } 
      }, 
     }); 
    }); 
}); 

Process.php

include('db/connection.php'); 

    if(isset($_POST['email'])){ 
    $email=$_POST['email']; 
    $query="SELECT Email FROM `request` WHERE Email='".$email."'"; 
    $result = $conn->query($query); 

    $search_record=$result->num_rows; 

    if ($search_record == 0) { 
     echo "ok"; 
     } 
    } 
+0

感谢您回复MR.Shubham715,我试过你的代码,但仍然无法正常工作。我正在输入数据库中可用的正确电子邮件ID,但我的按钮未启用。 –

+0

如果(数据== 1){并检查其工作或不是例如if(data == 1){alert(“hello”); }如果警报没有来,那么你应该检查控制台什么反应即将到来。 – shubham715

+0

是的,我没有得到任何警报和控制台显示为空 –

0

您应该检查并确认您的回应:

Process.php

if ($search_record == 0) { 
     echo "Email does not exist, please sign up to use our services"; 
     } 
    else{ 
     echo "success"; 
    } 

阿贾克斯

if (data == "success") { 
$("#submitYesNo").prop('disabled', false); 

} 
else{ 
$("#email-validation-error").html(data); 
$("#submitYesNo").prop('disabled', true);  
} 

HTML

<input id="submitYesNo" type="submit" name="next" value="submit" > 
+0

我只获取Sucess消息,但该按钮仍处于禁用状态。 –

+0

我禁用页面加载按钮。代码是完美的吗? –

+0

是的,这是罚款,这不是问题,你可以尝试禁用启用该按钮,在简单的点击,你会得到正确的想法,哪里是实际的错误也试图提醒成功的消息 –

0

试试这个代码。

希望它会正常工作

success:function(data) 
{ 
    if (data == 1) 
    { 
     $('input[type="submit"]').removeAttr("disabled", "disabled"); 
    } 
    else 
    { 
     $("#email-validation-error").html(data); 
     $('input[type="submit"]').attr("disabled", "disabled"); 

    } 
+0

我试过了但不起作用 –

+0

提交按钮再添加一个属性disabled =“disabled”。 –

+0

并再次显示我的代码,我编辑它。 –