接收成功响应但按钮仍然使用ajax禁用

时间:2017-06-12 06:34:02

标签: php jquery ajax

我正在使用正在运行的ajax检查电子邮件ID是否可用。我有一个提交按钮,在页面加载时被禁用。当用户输入正确的电子邮件地址时,我必须启用该按钮可在数据库中找到。如果数据库中有电子邮件,则按钮将启用,否则按钮将被禁用。如果条件存在问题。我试过按钮仍然是同样的问题。你能帮帮我吗?

$("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"  > 

的Ajax

$(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";
  }
}

4 个答案:

答案 0 :(得分: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";
      }
    }

答案 1 :(得分:0)

您应该检查并验证您的回复:

Process.php

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

<强>的Ajax

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"  > 

答案 2 :(得分: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");

    }

答案 3 :(得分:0)

最后,我在艾哈迈德·吉纳尼先生的帮助下找到了答案

<强> 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";
    }

 }