0

I am making a function which searches the database for flight information. I want user to be redirected on "ticket.php" if the response is true. I want to update the content of the page "ticket.php" using jquery. But jquery doesn't work. Am I doing something wrong here ? Here's part of javascript. It's an external javascript. It's included in both "search.php" and "ticket.php" page. This function gets invoked by clicking on search button in "search.php" page.

if(data){
    setTimeout("location.href = 'ticket.php';",3000); // Redirect to new page

//Change content of "ticket.php" using jquery.
    $("#source").text(x);
    $("#destination").text(y);
    $("#date").text(z);
    $("#no_person").text(person);
    $("#trip_type").text(type);
}

else
    $("#alert").text("Oops! No Flight matches your criteria ! ");
4

2 回答 2

4

当您将 location.href 设置为 ticket.php 时,用户将被重定向到该新页面 (ticket.php)。然后浏览器将加载该新页面,并且将不再使用您当前页面上的 javascript。

您必须使数据出现在 ticket.php 上,例如使用从他们搜索的内容中获取的 url 参数,如下所示:

window.location.href = 'ticket.php?from=norway&to=sweden';
于 2013-05-02T20:31:52.730 回答
0

这对您不起作用的原因是因为当您重定向到ticket.php页面时重新加载并且页面上的所有javascript都被刷新,失去了它们之前的任何值。要保留数据,您必须将信息发送到ticket.php

有几种方法可以实现这一点,按首选方法排序。
假设您正在请求这样的页面:ticket.php?from=norway&to=sweden

页面ticket.php上的php

<?php
  echo $_GET['from'];
  echo $_GET['to'];
 ?>

页面上的javascript ticket.php

从这篇文章中获取参数的解决方案:https ://stackoverflow.com/a/1404100/2033671

alert(getURLParameter('from'));
alert(getURLParameter('to')); 

来自同一域上不同页面的 jQuery

    $.get('ticket.php',function(response){
    $html = $(response);
    $html.find("#destination").text(y);
    $html.find("#source").text(x);
    $("html").html($html);
});
于 2013-05-02T20:51:57.290 回答