0

What I'm doing is basically using 3 files which I describe below

//File1.php
$('button.button1').click(function(e){
        $.ajax({
                 type: "POST",
                 url: "file2.php",

                 timeout: 180000
                })
                .done(function(data) {
                 //load data returned in modal
                })
                .fail(function(msg) {
                  //do something
                });
});

Then, my next file load modal code html (header,body and footer) with a button called 'OK'. (The body modal contains a form with the button 'OK')

//file2.php

    $('button.ok').click(function(e){
              $.ajax({
                        type: "POST",
                        url: "File3.php",
                        contentType:false,
                        processData:false,
                        cache:false,
                        data: formData,
                        timeout: 180000
                    })
                    .done(function(data) {
    //do something
                    } 
                    })
                    .fail(function(msg) {
                        //do something
                    });
                });

     In my file3.php do bd operations and save the values

So, when executing the program from the first script, the page is automatically returned to the index and the query is not executed in the bd, what is this? That I should return in each file to be executed correctly and why the values are sent through the get method (they are seen in the url) when it is specified that the method is post? HELP

to conclude, i have one button that do an ajax call , load a modal and inside the modal another ajax call is done

Mvram
  • 434
  • 1
  • 5
  • 20
  • 1
    Is `button.ok` loaded dynamically by the first AJAX call? – Barmar Feb 06 '17 at 20:00
  • It is loaded dinamically in the first AJAX call because it is inside the form (that is inside the modal) that is returned by the file2.php, but actually is loaded in file2.php not dinamically – Mvram Feb 06 '17 at 20:04

1 Answers1

1

jQuery is only aware of the elements in the page at the time it runs, so new elements added to the DOM dynamically are unrecognized by jQuery. To combat the problem use event delegation, bubbling events from newly added items up to a point in the DOM which was there when jQuery ran on page load. Many people use document as the place to catch the bubbled event, but it isn't necessary to go all the way up the DOM tree. Ideally you should delegate to the nearest parent existing at the time of page load.

$(document).on('click', 'button.ok', function(e){...

Using the .on() function should solve your issue.

Community
  • 1
  • 1
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119