caner taşdemir
caner taşdemir

Reputation: 203

jQuery - How to send ajax post on ajax post page

I need to send ajax post on ajax posted page.

index.php has

$.ajax({ type: "POST",datatype:"json", url: "/notification.php",      
data: "kime=karaakce&tur=konuyayorum&postid=2020",
success: function(html){
}
});

notification.php has same function but posts to track.php

$.ajax({ type: "POST",datatype:"json", url: "/track.php",      
data: "kime=karaakce&tur=konuyayorum&postid=2020",
success: function(html){
}
});

However, notification.php doesnt send the ajax post. How can I make it run ?

Upvotes: 0

Views: 137

Answers (1)

Rishabh
Rishabh

Reputation: 1213

First thing you cannot run the jquery code even if you include it in your notification.php file. That is because jquery runs only in browser, not at backend. So unless you "physically" open the notification.php page in browser the jquery won't run.

So to address your issue you'll have to chain the success response from one php file to next.

eg: Data from index.php ---> notification.php ---> index.php ---> track.php (Although a very crude approach)

Here is the code that can achieve this.

index.php file

$.ajax({ 
    type: "POST",
    datatype:"json", 
    url: "/notification.php",      
    data: {
            kime=karaakce,
            tur=konuyayorum,
            postid=2020
          }
    success: function(responseData){
           $.ajax({ 
                     type: "POST",
                     datatype:"json", 
                     url: "/track.php",      
                     data: {
                              kime=karaakce,
                              tur=konuyayorum,
                              postid=2020
                     }
                     success: function(html){
                        // This is your final success 
                  }
               });
   }
});

Your notification.php file should return a JSON data which you can use to send it to the next request. It will come in 'responseData' object.

Upvotes: 1

Related Questions