0

I am trying to diable a button using jquery so user cant click the button multiple times but its not working the code lets me reclick the register button over and over and over ! I did my research and found solutions but can seem to get them to work for my scenario !

heres my javascript $(document).ready(function(){

 $('#button').click(function(e){

 $('#button').prop('disabled',true);


var setIt = 'set' ;
$.post("form.php",{pull_form: setIt},function(data){

        $('.msg').fadeOut('slow',function(){
          $(this).html(data);
          $('.red').hide();
          $('.all').hide();
          $('.red').fadeIn('slow');
          $('.all').fadeIn('slow');
        }).fadeIn('slow');




        $('.red').click(function(){
        $('.all').css('display','none') ;
          })

})


 e.preventDefault() ;
 })




}) ; 

Heres my html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Using GET</title>
    <link rel="stylesheet" href="css.css" media="screen" type="text/css">
  </head>
  <body>

<h1>Click the button to Register now</h1>
<button type="button" >

  <a id='button' href="register.php">Register</a>

</button>


<div class="msg">

</div>



<div class="msg2">

</div>

<div class="all">

<button type="button" class="red">X</button>

</div>
<script src='../jquery.js'></script>
<script type="text/javascript" src='custom.js'></script>

  </body>
</html>

heres the file my ajax is making a call to

    <?php

    if(isset($_POST['pull_form'])) {


      ?>
    <div class="all">
    <h2>Register</h2>




    <form class="" action="index.html" method="post">


      <input type="text" name="name" value="">
      <input type="text" name="password" value="">
      <input type="submit" name="submit" value="Submit">

    </form>

    </div>
    <?php
    }

     ?>
4
  • Take you <a id='button'... element out of the button element Commented Oct 20, 2016 at 3:32
  • i used id button , is that bad ? Commented Oct 20, 2016 at 3:33
  • I know. But the button wrapping around it still clicks visually. You can style you a element like a button or move link functionality to button element. One inside another is not good. Commented Oct 20, 2016 at 3:35
  • thanks your right fixed my problem lol Commented Oct 20, 2016 at 3:39

3 Answers 3

2

Firstly you can change your #button element. it is not a button. you can use only a tag.

and follow :

<form class="Myfrom" action="your_action_page.php" method="post">
      <input type="text" name="name" value="">
      <input type="text" name="password" value="">
      <input type="submit" id="submit-button" name="submit" value="Submit">
</form>

$(".Myfrom").on("submit", "#submit-button", function(e){
     e.preventDefault() ;
     $(this).prop('disabled',true);
});
Sign up to request clarification or add additional context in comments.

2 Comments

Yea my html is poor , but this most definitely fixed it thanks
I find it funny I spend all night coding the most difficult things , but cant get the simple things right lol
0

Your #button is not a button, it's an a element. And to disable the a tag from redirecting you, you should set its href attribute to javascript: void(0).

3 Comments

you could also do <a href="#"> instead of javascript:void(0)
I use preventDefault to stop it from redirecting me
@Bwolfing you can still click it if you use that, and to prove it you can try, the link color changes.
0

You need to take a look at this: https://api.jquery.com/Ajax_Events/ A small example: http://pastebin.com/jAKiZqE6 Don't worry about the event on the button, control the submit form event. Also you could try to put your php post code in a different file, without HTML.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.