How to enable a button after validation?

Asked

Viewed 103 times

0

I need the button is enabled when the email does not exist in the database. Note that the validation if the email already exists works, I need the implementation to enable the button if the email does not exist.

Javascript

<script type="text/javascript">

$(document).ready(function () {

    $('#email').blur(function() {
        $th = $(this);
          $.ajax({
            url: 'plano.php',
            type: 'POST',
            data: {email: $th.val()},
            beforeSend: function(){
                $("#resultados").html("Carregando...");
            },
            success: function(data){
                $("#resultados").html(data);
            },
            error: function(){
                $("#resultados").html("Ouve um erro ao enviar sua URL");
            }
         });//ajax       
    });
});

</script>

HTML

<div id="resultados"></div>
<button id="registrar" disabled="disabled" type="submit">CONTRATAR AGORA!</button>

plano.php

$email = $_POST['email'];
    #Conecta banco de dados 
    $sql = mysqli_query($con, "SELECT email FROM conta WHERE email = '$email' ORDER BY EMAIL ASC LIMIT 1");

    while($sqlregistro=mysqli_fetch_array($sql))
{
    $emailbanco = $sqlregistro['email'];
}

    echo $emailbanco;
    echo "<br>";

    if(isset($email ) && $emailbanco === $email ){
        echo "Este email existe no banco";
    }else{
        echo "Email digitado " .$emailbanco. " Nao existe no banco "; 
    }

1 answer

0

...

success: function(data){
    $("#resultados").html(data);
    if(data.search("Nao existe") != -1)
        $("#registrar" ).prop( "disabled", false );
},

...
  • It worked in part, let me explain what happened. When I type an email that actually exists the code keeps the button disabled. And when I enter an email that doesn’t exist it enables the button. Perfect!!! However, when I type an email that does not exist and then I write another one that exists it does not disable, and keeps enabled. The correct one would be that each time I left the email field it enabled or disabled the button.

  • Do it like this... In the Blur, before the line $th = $(this);, place $("#registrar" ).prop( "disabled", true );. Or make a $('#email').keyup(()=>{$("#registrar" ).prop( "disabled", true );}) that there will always disable and will only enable when rolling the Success in Ajax. :)

  • Unfortunately it did not give

  • Put to disable in keyup?

  • yes, I did. but it didn’t work.

  • How to do to exist the email deletes what is in the email input?

  • The keyup is out of the Blur, right? Weird...

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.