asotshia
asotshia

Reputation: 121

How to merge two identical jQuery scripts in one?

Below there is a jQuery I use to add some entries in the DB. It is working correctly. I use the same one in the same file to remove some data. The form of the remove-from-db has id RemoveMe and it posts to $.post("add-to-db.php?id=<?php echo (int)$_GET['id'] ?>"

Below you can see the add-to-db script with form id AddMe.

My question is if there is a way to make these two in one. Thank you.

$(function(){
    $("#AddMe").submit(function(e){
       e.preventDefault();   
        $.post("add-to-db.php?id=<?php echo (int)$_GET['id'] ?>", $("#AddMe").serialize(), function (data){  
            $("#submit").attr('src','http://download.oracle.com/tech/blaf/specs/buttons/actnavbuttons/actnav_disabled_example.gif');
            $("#submit").attr('disabled',true);
            $("#submit").unbind('click');
            $("#message_post").html("Thank you");
setTimeout(function () { $("#message_post").hide(); window.location.href = "product.php?id=<?php echo (int)$_GET['id'] ?>"; }, 2000);

        });
    });
});

<form id="AddMe">
<input type="image" name="submit" id="submit" src="http://blog.mirthlab.com/wp-content/uploads/2008/04/rollover-up.png">
</form>
<div id="message_post"></div>

$(function(){
    $("#RemoveMe").submit(function(e){
       e.preventDefault();   
        $.post("remove-from-db.php?id=<?php echo (int)$_GET['id'] ?>", $("#RemoveMe").serialize(), function (data){          
            $("#submit").attr('src','http://download.oracle.com/tech/blaf/specs/buttons/actnavbuttons/actnav_disabled_example.gif');
            $("#submit").attr('disabled',true);
            $("#submit").unbind('click');
            $("#message_post").html("Thank you");
setTimeout(function () { $("#message_post").hide(); window.location.href = "product.php?id=<?php echo (int)$_GET['id'] ?>"; }, 2000);
        });
    });
});

<form id="RemoveMe">
<input type="image" name="submit" id="submit" src="http://blog.mirthlab.com/wp-content/uploads/2008/04/rollover-up.png">
</form>
<div id="message_post"></div>

Upvotes: 0

Views: 173

Answers (3)

Russ Cam
Russ Cam

Reputation: 125488

You could use an array of object literals to store the id and url pairs

$(function () {   
    var buttons = [
      { id : '#AddMe', url : 'add-to-db.php' }, 
      { id : '#RemoveMe', url : 'remove-from-db.php' }
    ];

    $.each(buttons, function(i,v) {
        $(v.id).submit(function(e){
           e.preventDefault();   
            $.post(v.url + "?id=<?php echo (int)$_GET['id'] ?>", $(v.id).serialize(), function (data) {  
                $("#submit")
                    .attr('src','http://download.oracle.com/tech/blaf/specs/buttons/actnavbuttons/actnav_disabled_example.gif')
                    .attr('disabled',true)
                    .unbind('click');

                $("#message_post").html("Thank you");

                setTimeout(function () { 
                    $("#message_post").hide(); 
                    window.location.href = "product.php?id=<?php echo (int)$_GET['id'] ?>"; 
                }, 2000);

            });
        });
    });
});

Upvotes: 3

Šime Vidas
Šime Vidas

Reputation: 185883

Yes, you can transform this:

$( function () {
    // code block A
});

$( function () {
    // code block B
});

into this:

$( function () {
    // code block A
    // code block B
});

Upvotes: 0

Justin Thomas
Justin Thomas

Reputation: 5848

Use it as you have it above and remove this:

});



$(function(){

Upvotes: 0

Related Questions