Problem with ajax and button event

Problem with ajax and button event

Hi guys,

sorry for this stupid question, but I can't solve this.

When I click on the button , the ajax request sent as often as I have "each " elements . Plus once the regular . How can that be? The ajax request is started but only once at the end . And it should also be started only once.

I just do not come on , and prophesied no longer what I could google .


That is my jQuery code:
  1. $('#iptableschange-save').click(function() {
        id = $(this).attr('value');
        chainid = $('#changeiptablerule #add-iptable-rule-chain input[type=radio]:checked').val();
        if( typeof chainid == 'undefined' ) {
          $('#add-iptable-rule-chain').removeClass('panel-default');
          $('#add-iptable-rule-chain').addClass('panel-danger');
        } else {
          $('#add-iptable-rule-chain').removeClass('panel-danger');
          $('#add-iptable-rule-chain').addClass('panel-success');
        }
        inputinterface = $('#add-iptable-rule-input input[type=radio]:checked').val();
        if( typeof inputinterface == 'undefined' ) {
          $('#add-iptable-rule-input').removeClass('panel-default');
          $('#add-iptable-rule-input').addClass('panel-danger');
        } else {
          $('#add-iptable-rule-input').removeClass('panel-danger');
          $('#add-iptable-rule-input').addClass('panel-success');
        }
        outputinterface = $('#add-iptable-rule-output input[type=radio]:checked').val();
        if( typeof outputinterface == 'undefined' ) {
          $('#add-iptable-rule-output').removeClass('panel-default');
          $('#add-iptable-rule-output').addClass('panel-danger');
        } else {
          $('#add-iptable-rule-output').removeClass('panel-danger');
          $('#add-iptable-rule-output').addClass('panel-success');
        }
        target = $('#add-iptable-rule-target input[type=radio]:checked').val();
        if( typeof outputinterface == 'undefined' ) {
          $('#add-iptable-rule-target').removeClass('panel-default');
          $('#add-iptable-rule-target').addClass('panel-danger');
        } else {
          $('#add-iptable-rule-target').removeClass('panel-danger');
          $('#add-iptable-rule-target').addClass('panel-success');
        }
        comment = $('#add-iptable-rule-comment textarea').val();
        active = false;
        if( $('#add-iptable-rule-last-overview input[type=checkbox]').is(":checked"))
        {
          active = true;
        }
        priority = $('#add-iptable-rule-last-overview input[type=text]').val();
        if( priority == "" ) {
          $.ajax({url: "handler/getMaxPriority.php?chainid=" + chainid, success: function(result){
            gottenResult = result.split("Priority: ");
            priority = gottenResult[1];
            $('#add-iptable-rule-last-overview input[type=text]').val(priority)
          }})
        }
        if( $('#changeiptablerule').find('.panel-danger').length == 0 ) {
          var data = {
                        chainid:  chainid,
                        inputinterfaceid:  inputinterface,
                        outputinterfaceid: outputinterface,
                        sourceid: {
                                hosts: { },
                                groups: { },
                              },
                        destinationid: {
                                hosts: { },
                                groups: { },
                              },
                        serviceid: {
                                hosts: { },
                                groups: { },
                              },
                        target:  target,
                        comment:  comment,
                        active:  active,
                        priority:  priority
                  };
          sourceHost = 0;
          sourceGroup = 0;
          $('#add-iptable-rule-source .selectedItems ul .selectedElement').each(function(){
            if( $(this).text().indexOf("Group:") > -1) {
              data['sourceid']['groups'][sourceGroup] = $(this).val();
              sourceGroup++;
            } else {
              data['sourceid']['hosts'][sourceHost] = $(this).val();
              sourceHost++;
            }
          })
          destinationHost = 0;
          destinationGroup = 0;
          $('#add-iptable-rule-destination .selectedItems ul .selectedElement').each(function(){
            if( $(this).text().indexOf("Group:") > -1) {
              data['destinationid']['groups'][destinationGroup] = $(this).val();
              destinationGroup++;
            } else {
              data['destinationid']['hosts'][destinationHost] = $(this).val();
              destinationHost++;
            }
          })
          serviceHost = 0;
          serviceGroup = 0;
          $('#add-iptable-rule-service .selectedItems ul .selectedElement').each(function(){
            if( $(this).text().indexOf("Group:") > -1) {
              data['serviceid']['groups'][serviceGroup] = $(this).val();
              serviceGroup++;
            } else {
              data['serviceid']['hosts'][serviceHost] = $(this).val();
              serviceHost++;
            }
          })
          $.ajax({
              type:    'post',
              cache:   false,
              url:     "handler/writeIptable.php?id=" + id,
              data:    data,
              success: function(result){
                alert(result);
          }}
          )
        } else {
          // What to do if there is an error
        }
    });

And this is my html button:

  1. <button id="iptableschange-save" type="button" class="btn btn-default">Save</button>
Thank you for your help in advance!

Chears, Johannes