简体   繁体   中英

php reload page from ajax div

I use a page with Jquery tabs and if i submit one of the forms in the tabs only that tab is submitted and refreshed with this jquery code:

$(document).on("submit", "#plaatsen_stap3", function(event) {
/* stop form from submitting normally */    

event.preventDefault();      
    $.ajax({
      type:"GET",
      url:"../plaatsen_advertentie/plaatsen_advertentie_stap3.php",
      cache: false,                 
      data: $("#plaatsen_stap3").serialize(),
      success:function(data){
        $("#tab2").html(data);
      }     
    });
});

But in the case that there has to be payed i want to reload the page with the payment page. I want to do that AFTER the div is reloaded with the data, because i need to put a payment row in the DB with the data from the GET. Is location an option? If i use that now only the div (tab2) is loaded with the payment page....

So: 1.push submit 2.submit the form and load page/script in div by Ajax 3.check in php script (within the div) if payment is needed 4.if yes,add row with payment data in database and reload entire page with payment page (with some Get data in the url (last inserted id)

  success:function(data){
    $("#tab2").html(data);
    location.href = "/yourpage.php";
  }

Since you wanna do once the HTML is generated, give some time like about 5 seconds?

success:function(data){
    $("#tab2").html(data);
    setTimeout(function(){location.href = "/yourpage.php";}, 5000);
}

This would work for your use case. This cannot be done from server side.

I think load() is what you are looking for. http://api.jquery.com/load/

The code below is intended as a guideline, and I'm not even sure it's working (I have not tested it). But I hope it will be of some help.

I would do something like this:

//Step 1
$(document).on("submit", "#plaatsen_stap3", function(event) {
/* stop form from submitting normally */    

event.preventDefault();      
    $.ajax({
      type:"GET",
      url:"../plaatsen_advertentie/plaatsen_advertentie_stap3.php",
      cache: false,                 
      data: $("#plaatsen_stap3").serialize(),
      success:function(data){


   //Step 2 - submit the form and load page/script in div by Ajax 
   //Make sure array[] is an actual array that is sent to the test.php-script.
   $("#tab2").load("test.php", { 'array[]' , function() {
            //Step 3 - check in php script (within the div) if payment is needed (Do this from test.php - don't check the actual div but check values from array[])
            //Step 4 -  if yes,add row with payment data in database and reload entire page with payment page (with some Get data in the url (last inserted id)                     
            //Do this from test.php and use header-redirect to reload entire page   
           $("tab2").html(data); //Do this when test.php is loaded
       }

       } );


      }     
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM