【问题标题】:php reload page from ajax divphp从ajax div重新加载页面
【发布时间】:2013-03-30 20:00:52
【问题描述】:

我使用带有 Jquery 选项卡的页面,如果我在选项卡中提交其中一个表单,则只有该选项卡被提交并使用此 jquery 代码刷新:

$(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);
      }     
    });
});

但在必须付款的情况下,我想用付款页面重新加载页面。我想在 div 重新加载数据后这样做,因为我需要在数据库中使用来自 GET 的数据放置一个支付行。位置可以选择吗?如果我现在使用它,只有 div (tab2) 会加载付款页面....

所以: 1.推送提交 2.通过Ajax提交表单并在div中加载页面/脚本 3.check in php script (in the div) if payment is required 4.如果是,在数据库中添加带有支付数据的行并用支付页面重新加载整个页面(在url中有一些Get数据(最后插入的id)

【问题讨论】:

  • 所以你想让整个页面刷新到支付页面?
  • 您能否重新提出您的问题,您到底想做什么??
  • pendo,是的,这就是我想要的!

标签: php ajax jquery


【解决方案1】:
  success:function(data){
    $("#tab2").html(data);
    location.href = "/yourpage.php";
  }

【讨论】:

  • 我想在 ajax 重新加载后在 php 脚本中执行此操作,所以这行不通...
  • 你试过那个代码了吗?如果还没有,请先尝试。在这种情况下,您不能使用 php 重新加载/重定向,因为 plaatsen_advertentie_stap3.php 是异步执行的。此外,您的初始页面的 php 脚本在用户看到页面之前就已经完全执行,因此之后没有机会调用任何函数。
  • 嗯。我不明白。这是在ajax函数之后完成的吗?您可以使用 JSON 将数据发送到 yourpage.php。
  • 查看我在问卷中添加的额外规则
【解决方案2】:

既然您想在 HTML 生成后执行此操作,请给一些时间,例如 5 秒左右?

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

这适用于您的用例。这不能从服务器端完成。

【讨论】:

  • 这似乎是个坏主意。要依靠 nr 秒来实现这一目标?
  • 是的,我同意...但是想不出比这更好的了。
【解决方案3】:

我认为 load() 是您正在寻找的。 http://api.jquery.com/load/

下面的代码仅供参考,我什至不确定它是否有效(我还没有测试过)。但我希望它会有所帮助。

我会这样做:

//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
       }

       } );


      }     
    });
});

【讨论】:

    猜你喜欢
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    • 2013-05-28
    • 1970-01-01
    相关资源
    最近更新 更多