Submit page after 3 attempts [closed]

-1

I have this simple form:

    <form class="form-horizontal"  >
<fieldset>

<!-- Text input-->
<div class="form-group" align="center">
  <label class="col-md-4 control-label" for="textinput"></label>  
  <div class="col-md-4">
  <input id="textinput" name="textinput" type="text" placeholder="Login " class="form-control input-md">
  <span class="help-block"></span>  
  </div>
</div>

<!-- Password input-->
<div class="form-group" align="center" >
  <label class="col-md-4 control-label" for="passwordinput"></label>
  <div class="col-md-4">
    <input id="passwordinput" name="passwordinput" type="password" placeholder="Senha " class="form-control input-md" >
    <span class="help-block"></span>
  </div>
</div>

<!-- Button -->
<div class="form-group" align="center" id=addCount>
  <label class="col-md-4 control-label" for="singlebutton"></label>
  <div class="col-md-4">
   <button id="contador" name="singlebutton"  class="btn btn-primary">Enviar</button>

  </div>
</div>

</fieldset>
</form>

I need three alerts to be issued after submitting this form, on the fourth attempt, to go to the destination page. It's possible? Tried via javascript, but with no success.

    
asked by anonymous 19.12.2017 / 21:17

2 answers

1

Pure Javascript without library

Create a javascript function that counts clicks on type="button" button and after third try change button to type="submit"

	var clicks=0;
	var incrementCount = function(){
	    clicks++;
	    
	    if (clicks==1){
	      alert("alerta 1");
	    }else if (clicks==2){
	      alert("alerta 2");
	    }else if (clicks==3){
	      alert("alerta 3");
	      document.getElementById("demo").innerHTML = '<button type="submit"  id="contador" name="singlebutton"  class="btn btn-primary" />Enviar</button>';
	    }   
	}
<form class="form-horizontal" action="https://pt.stackoverflow.com/">
<fieldset>

<!-- Text input-->
<div class="form-group" align="center">
  <label class="col-md-4 control-label" for="textinput"></label>  
  <div class="col-md-4">
  <input id="textinput" name="textinput" type="text" placeholder="Login " class="form-control input-md">
  <span class="help-block"></span>  
  </div>
</div>

<!-- Password input-->
<div class="form-group" align="center" >
  <label class="col-md-4 control-label" for="passwordinput"></label>
  <div class="col-md-4">
    <input id="passwordinput" name="passwordinput" type="password" placeholder="Senha " class="form-control input-md" >
    <span class="help-block"></span>
  </div>
</div>

<!-- Button -->
<div class="form-group" align="center" id=addCount>
  <label class="col-md-4 control-label" for="singlebutton"></label>
  <div class="col-md-4">
   <div id="demo"><button type="button" id="contador" name="singlebutton"  class="btn btn-primary"  onclick="incrementCount();">Enviar</button>
</div>

  </div>
</div>

</fieldset>
</form>

About the type attribute of buttons

  • submit: The button sends the form data to the server.
  • button: The button has no default behavior. It may have client-side scripts associated with element events, which are triggered when the event occurs.
  • <button> - Mozilla Developer Network

        
    20.12.2017 / 00:23
    1

    Although you can not understand the reason, you can manipulate the .submit() event by using .preventDefault() to prevent the submission of the form and then using .unbind("submit").submit() to ignore .preventDefault() and submit the form normally.

    $(document).ready(function(){
      var tentativa = 0;
      $("form").submit(function(event){
        event.preventDefault();
        tentativa++;
        if (tentativa > 3) {
          $("form").unbind("submit").submit();
        } else {
          alert("Tentativa " + tentativa);
        }
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><form><inputtype="submit" value="enviar">
    </form>

    If you are using jquery 3.X replace .unbind() with .off() since unbind has been deprecated since version 3.x

        
    19.12.2017 / 22:04