jQuery/Ajax request is being sent twice

10,666

Solution 1

Try

$("#add-user-btn").unbind('click').bind('click', function () { });  

This will ensure the click event only gets called once.

Solution 2

When I worked with an specific website that I only had access to my javascript, I got this kind of error too. The problem was that there was lots of $(function() and $(document).ready in other codes.

what you can do to prevent this, if you can't correct the core problem is:

var preventRunDefault;
$(function () {
    if(!preventRunDefault) {
    preventRunDefault = true;
    $("#add-user-btn").click(function (e) {
        e.preventDefault();
        var email = $("#email").val();
        var name = $("#name").val();
        var action = "adduser";

        $.ajax({
            url: '../actions.php',
            type: 'POST',
            data: {
                action: action,
                email: email,
                name: name,
            },
            dataType: 'json',
            success: function (data) {
                $(".close-reveal-modal").click();
            }
        });
    });
   }
});

But the right way is to search the cause and don't do workarounds.

Solution 3

This problem also occurs when you include multiple times the same JavaScript file in your HTML file.

<html>
    <head>
        <script src="utility.js"></script>
        <script src="utility.js"></script>
    </head>
Share:
10,666
Jenszor
Author by

Jenszor

Updated on June 14, 2022

Comments

  • Jenszor
    Jenszor almost 2 years

    I've been scanning my code over and over again but I can't seem to find the problem. When I click the link #add-user-btn the file actions.php is called twice (and hence the PHP script is executed twice).

    Here's the script: I suppose it has something to do with the javascript in front of the ajax request?

    $(function () {
        $("#add-user-btn").click(function (e) {
            e.preventDefault();
            var email = $("#email").val();
            var name = $("#name").val();
            var action = "adduser";
    
            $.ajax({
                url: '../actions.php',
                type: 'POST',
                data: {
                    action: action,
                    email: email,
                    name: name,
                },
                dataType: 'json',
                success: function (data) {
                    $(".close-reveal-modal").click();
                }
            });
        });
    });
    

    The HTML:

    <div id="adduser" class="reveal-modal">
     <h1>Add new user</h1>
     <p><label for="email">E-mail:</label> <input id="email" name="email" type="text" placeholder="[email protected]" /></p>
     <p><label for="name">Name:</label> <input id="name" name="name" type="text" placeholder="Adam Smith"/></p>
     <p><label for="password">Password:</label> <input id="password" name="password" type="password" placeholder="123456" /></p>
     <p>
     <label for="authorization">Authorization:</label> 
     <select id="authorization" name="authorization">
        <option value="0">Administrator</option>
        <option value="1">Superuser</option>
        <option value="2">User</option>
     </select>     
     </p>
     <button id="add-user-btn">Submit</button>
     <a class="close-reveal-modal">&#215;</a>
     </div>
    
  • adeneo
    adeneo almost 11 years
    We all guess a little from time to time, but there's absolutely nothing in the question to suggest this is the right answer, even if this approach often solves issues that should have been solved elsewhere.
  • JeffRegan
    JeffRegan almost 11 years
    This could solve an issue that is caused by some other error (but doesn't solve the error). Both tymeJV and adeneo are correct. I was a bit hasty in answering.
  • Jenszor
    Jenszor almost 11 years
    This solved it! Many thanks! I still have no idea what went wrong though.
  • JeffRegan
    JeffRegan almost 11 years
    Like all the others have been implying, you should try to figure out the underlying issue. Does it submit twice every time? Are you loading the elements using ajax? The fact that the above works means that you don't have two elements with the same id and a form issue isn't causing your problem.
  • Michael Rovinsky
    Michael Rovinsky almost 3 years
    It depends on the browser implementation, but can happen indeed: stackoverflow.com/questions/1533723/…