Luis Salamanca
Luis Salamanca

Reputation: 21

Alertify confirms and submits a form automatically without clicking OK

I'm creating a blog with bootstrap and I have a form to submit categories:

<form action="categories.php" id="category-form" class="form-horizontal" role="form" method="post">
  <div class="form-group">
    <label for="category" class="col-sm-2 control-label">Category</label>
    <div class="col-sm-10">
      <input type="text" name="category" class="form-control" id="category" placeholder="Category">
    </div>
  </div>
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <input type="submit" name="submit" id="btn-submit" class="btn btn-primary" value="Add Category">
    </div>
  </div>
</form>

When I press the form button "Add Category" the dialog appears but after a few seconds it submits itself immediately and the dialog disappears without clicking the buttons "yes" or "no", searching the web I found some solutions but doesn't work for me. The code I use for Alertify JS is the following:

$("#btn-submit").on("click", function(){
    alertify.confirm("This is an alert dialog?", function(e){
        if (e) {
            alertify.success("Category was saved.")
        } else {
            alertify.error("Category not saved.");
        }
    });
return false;
});

I also try event.preventDefault();:

$("#btn-submit").on("click", function(event){
    event.preventDefault();
    alertify.confirm("This is an alert dialog?", function(e){
        if (e) {
            $("#category-form").submit();
            alertify.success("Category was saved.")
            return true;
        } else {
            alertify.error("Category not saved.");
            return false;
        }
    });
});

But does not work as well. Any help please... Thank you.

Upvotes: 2

Views: 5246

Answers (4)

littleinstein
littleinstein

Reputation: 71

     Ok, This was posted and answered a long time ago. But as I was developing asp net 
    core in 2020, I came across AlertifyJS. I like the library. A solution for this 
    issue is:
    1) I used <button>
    2) I used e.preventdefault()
    3) I used ('#category-form').submit() 
    4) Make sure you add id='category-form' in your form and also and id for button. 
    5) Use button and not input type=submit. 
     There could be a difference in how they work. 
     The difference is that <button> can have content, 
     whereas <input> cannot (it is a null element).

        $('#btn-submit').on('click', function (e) {
            e.preventDefault();
            alertify.confirm('Save Category', 'Do you want to proceed?', function (e) 
            {
                if (e) {
                    $("#category-form").submit();
                    alertify.success('Category Saved Successfully.');
                    return true;
                }
                else {
                    alertify.error("Category was not saved.");
                    return false;
                }                   
            }, function () { alertify.error('Cancel') });
        });

Upvotes: 0

Saeed Ansari
Saeed Ansari

Reputation: 465

alertify basically take 4 arguments in confirm function

alertify.confirm(title, message, onconfirm, oncancel);

You don't have to do if(e)this code

$("#formID").on("click", function(){
    alertify.confirm("This is an alert dialog?", function(e){
        if (e) {
            alertify.success("Category was saved.")
        } else {
            alertify.error("Category not saved.");
        }
    });
return false;
});

will now become

$("#formID").submit(function(e){
  e.preventDefault();
 alertify.confirm('Confirm Title', 'Confirm Message', function(){
   // call the function that will handle your form submission may be ajax don't write $('#fomr').submit() becasue it will start an endless loop through this function   
  }, function(){ 
    // when user click cancel
  });

  });

Upvotes: 0

vijayP
vijayP

Reputation: 11502

Could you please try following code:

You need to remove type="submit" from your button. Otherwise it submits the form by default.

HTML:

<form action="categories.php" id="category-form" class="form-horizontal" role="form" method="post">
  <div class="form-group">
    <label for="category" class="col-sm-2 control-label">Category</label>
    <div class="col-sm-10">
      <input type="text" name="category" class="form-control" id="category" placeholder="Category">
    </div>
  </div>
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <input type="button" id="btn-submit" class="btn btn-primary" value="Add Category">
    </div>
  </div>
</form>

JQuery Code:

//your button click code
$("#btn-submit").on("click", function(event){
    event.preventDefault();
    alertify.confirm("This is an alert dialog?", function(e){
        if (e) {
            $("#category-form").submit();
            alertify.success("Category was saved.")
            return true;
        } else {
            alertify.error("Category not saved.");
            return false;
        }
    });
});

Upvotes: 0

vitally
vitally

Reputation: 377

try something like this. i think you want the confirmation before submitting the form so added the confirm message part. hope it will help.

$("#category-form").submit(function (e) {
            var result = confirm("Are you sure ?");
            if(result){
                // do something
            } else {
                alertify.error("Error mesage.");
                e.preventDefault(); // this will prevent from submitting the form.
                }
        });

Upvotes: 1

Related Questions