2
votes

I'm doing a $.post to a page using jquery, the problem is this post executed twice when ever its fired, I've tried to run the page in IE and firefox, the result is the same, and using firebug shows that the request is bieng submitted twice.

I need your help please, thanks in advance. Here is the code:

jQuery().ready(function() {
    //Reply buttons binding live event
    $(".ticketButton").live('click', function(e) {
        e.preventDefault(); 

        //build dialog with id & title
        $( "#msgs-form:ui-dialog" ).dialog( "destroy" );
        var ticket_ID= $(this).attr("title");
        var ticket_title= $(this).attr("msgTitle");
        $("#msgs-dialog").attr("title", ticket_title);

        //fetch data from the server to fill dialog content
        $.post("server_processing_tickets_response.php", {'getTickets' : ticket_ID},
                function(data){
                var div_data;
                //build div's of classes .ticket-1 and ticket-0 from the returned json object
                $.each(data.tickets_data, function(i, tickets_data)
                    {
                    div_data += "<div class='ticket-0'><span>" + tickets_data.content + "</span></div>";
                    });
                $(div_data).appendTo("#ticketsWrap");
                }, "json");

        $("#msgs-form #ticketID").attr("value", ticket_ID);
                $( "#msgs-dialog" ).dialog({
                    autoOpen: false,
                    title: ticket_title,
                    height: 'auto',
                    width: 460,
                    show: "fade",
                    hide: "fade",
                    modal: true,
                    resizable: false,
                    buttons: false,
                    close: resetDialog()
                });

        $( "#msgs-dialog" ).dialog( "open" );
    });

    function resetDialog(){
    $("#ticketsWrap").empty();
    }

}

thanks for your fast reply! Here is the full javascript code I'm using:

$(document).ajaxStart(function(){
$('#real-content').showLoading();
    }).ajaxStop(function(){
$('#real-content').hideLoading();
    });

//ajax forms submission
$(function() {      

        var msgs_table = $('#msgs_table').dataTable( {
                "bJQueryUI": true,
                "bProcessing": false,
                "bServerSide": true,
                "sAjaxSource": "server_processing_msgs.php",
                "aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
                "aoColumns": [ 
                                    { "sWidth": "30px" },
                                    { "sWidth": "100px" },
                                    { "sWidth": "200px" },
                                    { "sWidth": "250px" },
                                    { "sWidth": "50px" }
                            ],
                "oLanguage": {
                                "sProcessing": "...تحميل",
                                "sZeroRecords": "لايوجد لديك تذاكر حالياً",
                                "sInfoEmpty": "",
                                "sInfoFiltered": "",
                                "sInfo": "يتم إظهار _START_ إلى _END_ من _TOTAL_ تذكرة",
                                "oPaginate": {
                                                "sFirst":    "البداية",
                                                "sPrevious": "السابق",
                                                "sNext":     "التالي",
                                                "sLast":     "النهاية"
                                              },
                                "sSearch": "بحث ضمن التذاكر:",
                                "sLengthMenu": "عرض _MENU_ تذكرة"

                             },
                "aoColumnDefs": [{
                        "fnRender": function ( oObj ) {
                                        return "<input type='button' style='padding:3px 6px;'  name='ticket-ID-button' value='عرض' msgTitle='" + oObj.aData[3] + "\' title='" + oObj.aData[0] + "\' class=\"ticketButton ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only\" />";  
                                    },
                            "aTargets": [ 0 ]}]
            } );



    jQuery().ready(function() {

                $( "#ٍSucsessMessage" ).dialog({
                    autoOpen: false,
                    height: 'auto',
                    width: 300,
                    show: "fade",
                    hide: "fade",
                    modal: true,
                    resizable: false,
                    buttons: false,
                });
                $( "#ٍFailMessage" ).dialog({
                    autoOpen: false,
                    height: 'auto',
                    width: 300,
                    show: "fade",
                    hide: "fade",
                    modal: true,
                    resizable: false,
                    buttons: false,
                });


    //Reply buttons binding live event
    $(".ticketButton").live('click', function(e) {
        e.preventDefault(); 

        //build dialog with id & title
        $( "#msgs-form:ui-dialog" ).dialog( "destroy" );
        var ticket_ID= $(this).attr("title");
        var ticket_title= $(this).attr("msgTitle");
        $("#msgs-dialog").attr("title", ticket_title);

        //fetch data from the server to fill dialog content
        $.post("server_processing_tickets_response.php", {'getTickets' : ticket_ID},
                function(data){
                var div_data;
                //build div's of classes .ticket-1 and ticket-0 from the returned json object
                $.each(data.tickets_data, function(i, tickets_data)
                    {
                    div_data += "<div class='ticket-0'><span>" + tickets_data.content + "</span></div>";
                    });
                $(div_data).appendTo("#ticketsWrap");
                }, "json");

        $("#msgs-form #ticketID").attr("value", ticket_ID);
                $( "#msgs-dialog" ).dialog({
                    autoOpen: false,
                    title: ticket_title,
                    height: 'auto',
                    width: 460,
                    show: "fade",
                    hide: "fade",
                    modal: true,
                    resizable: false,
                    buttons: false,
                    close: resetDialog()
                });

        $( "#msgs-dialog" ).dialog( "open" );
    });

    function resetDialog(){
    $("#ticketsWrap").empty();
    }

    $( "#msgs-reply-button" ).click(function() {
        $("#msgs-form").submit();
        $("#msgs-dialog").dialog( "close" );
    }); 

    //reply form ajax submit
    $("#msgs-form").submit(function(e){
        e.preventDefault(); 
        $.post("server_processing_tickets_response.php", $("#msgs-form").serialize(),
        function(data){
            if(data.valid == 'valid'){ 
                $( "#ٍSucsessMessage" ).dialog( "open" );
                setTimeout(function(){$( "#ٍSucsessMessage" ).dialog("close")},1500);
            }
            else if(data.valid == 'invalid')
            {
                $( "#FailMessagee" ).dialog( "open" );
                setTimeout(function(){$( "#FailMessagee" ).dialog("close")},3000);
            }
        }, "json");

    });
    //Reply buttons in table//

Is it something related to request header? here is the headers i get form firebug after I submit the form : Response Headers Date Sat, 09 Jul 2011 13:29:51 GMT Server Apache/2.2.17 (Win32) PHP/5.3.5 X-Powered-By PHP/5.3.5 Expires Thu, 19 Nov 1981 08:52:00 GMT Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma no-cache Content-Length 24 Keep-Alive timeout=5, max=84 Connection Keep-Alive Content-Type text/html Request Headers Host 127.0.0.1:8888 User-Agent Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1 Accept application/json, text/javascript, /; q=0.01 Accept-Language en-us,en;q=0.5 Accept-Encoding gzip, deflate Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive 115 Connection keep-alive Content-Type application/x-www-form-urlencoded; charset=UTF-8 X-Requested-With XMLHttpRequest Referer http://127.0.0.1:8888/Lightc-tables/msgs.php Content-Length 41 Cookie PHPSESSID=ku8e6o3h2bl27ltofkir58a626

and the same headers with different Expires also repeated after this headers..

1
Try debugging your code in firebug or chromeJayantha Lal Sirisena
Bakk is this issue resolved? If yes how?rovsen

1 Answers

1
votes

You need to use some little trigger-hoak ;)

$(".ticketbutton").unbind("click");
$(".ticketbutton").unbind("live");
$(".ticketbutton").die();