Carlos F Carlos F - 1 month ago 19
HTML Question

Bootstrap modal firing up incrementing times

Bootstrap Modal fires action one time and if I click again it will fire up twice and so on.

<div id="loginModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title">Log In</h4>
</div>
<div class="modal-body">
<h3>Username</h3>
<input type="text" id="userLogin" placeholder="Username">
<h3>Password</h3>
<input type="password" id="pwdLogin" placeholder="Password">
<br>
<br>
<input type="checkbox" id="rememberMe">Remember Me
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" id="btnLogin">Log In</button>
</div>
</div>
</div>
</div>


Here's the span that fires the modal.

<span id="comment"><a id="testComment" data-toggle="modal" href="#loginModal" class="btn btn-default">Add comment</a></span>


After I click the span the modal shows. I click the #btnLogin and then the action fires up one time, two times and so on.

javascript that interacts with the modal(the action is within the span click):

$("#testComment").click(function (event) {
if ($("#msg").val() == "" || $("#nombre").val() == "") {
alert("To send a comment fill your mail and message!");
event.stopPropagation();
}
else {
$("#btnLogin").click(function () {
alert("this also");
$(this).off('shown.bs.modal');
if ($("#userLogin").val() != "" && $("#pwdLogin").val() != "") {
var dataToSend = {
"action": "LOGIN"
, "username": $("#userLogin").val()
, "password": $("#pwdLogin").val()
, "remember": $("#rememberMe").is(":checked")
}
$.ajax({
url: "data/applicationLayer.php"
, type: "POST"
, data: dataToSend
, dataTpe: "json"
, success: function (jsonData) {
var data = {
"comment": $("#msg").val()
, "username": $("#username").val()
}
$.ajax({
url: "data/addComment.php"
, type: "POST"
, data: data
, dataType: "text"
, success: function (dataResponse) {
var newHTMLContent = "";
newHTMLContent += "<tr><td>" + data.username + "</td>" + "<td>" + data.comment + "</td></tr>";
$("#commentTable").append(newHTMLContent);
alert("Comment was added!");
}
, error: function (errorMsg) {
alert("Error adding comment in ajax");
}
});
}
, error: function (errorMsg) {
alert("Login Error");
}
});
}
else {
alert('Missing username or password.');
}
});
}
});

Answer

Just put $("#btnLogin").click event outside. Here is the my code or go with below link may be it can help you.

JSFiddle

JAVSCRIPT

$("#testComment").click(function(event) {
    if ($("#msg").val() == "" || $("#nombre").val() == "") {
        alert("To send a comment fill your mail and message!");
        event.stopPropagation();
    }
});
$("#btnLogin").click(function() {
    alert("this also");
    $(this).off('shown.bs.modal');
    if ($("#userLogin").val() != "" && $("#pwdLogin").val() != "") {
        var dataToSend = {
            "action": "LOGIN",
            "username": $("#userLogin").val(),
            "password": $("#pwdLogin").val(),
            "remember": $("#rememberMe").is(":checked")
        }
        $.ajax({
            url: "data/applicationLayer.php",
            type: "POST",
            data: dataToSend,
            dataTpe: "json",
            success: function(jsonData) {
                var data = {
                    "comment": $("#msg").val(),
                    "username": $("#username").val()
                }
                $.ajax({
                    url: "data/addComment.php",
                    type: "POST",
                    data: data,
                    dataType: "text",
                    success: function(dataResponse) {
                        var newHTMLContent = "";
                        newHTMLContent += "<tr><td>" + data.username + "</td>" + "<td>" + data.comment + "</td></tr>";
                        $("#commentTable").append(newHTMLContent);
                        alert("Comment was added!");
                    },
                    error: function(errorMsg) {
                        alert("Error adding comment in ajax");
                    }
                });
            },
            error: function(errorMsg) {
                alert("Login Error");
            }
        });
    } else {
        alert('Missing username or password.');
    }
});