1

So I am using an ajax request to post data into a php script. At some point, the php script redirects.

Well at least it should be. But it does all kind of weird things

header('Location: index.php');

It get's all the content in index.php and puts it in the callback area.

So I decided to reload the page using javascript. And this is what I got

$.post('booking.php',
{
    firstname : $('#firstname').val().trim(),
    lastname : $('#lastname').val().trim()
},function(data){   
        window.location = 'index.php';
        $('.notification-booking').html(data);
});

Problem is. There is some feedback being given. What I have up there displays the feedback for a moment then reloads the page. Which woukd be cumbersome if the user hasn't finished reading the text. So I would like to do the following.

  1. reload the page,
  2. display the feedback
  3. Eat a donut and be happy.

In exactly that order I have no. 3 under control. Help out on number 1

John Kariuki
  • 4,966
  • 5
  • 21
  • 30
  • u should do the .html() first then do a wait for a few seconds(settimeout) before running window.location – Jianhong Jul 27 '14 at 19:48
  • 1
    U could store the data u want to display in a session and check in your index.php file if the session is set and display it there – DarkBee Jul 27 '14 at 19:50

1 Answers1

0

You could pass variables to the second page via JavaScript through the URL, then retrieve them via PHP. That way you can display information for as long as the user needs to read it.

Javascript on first page

window.location = 'second.php?firstVar=' + firstname + '&secondVar=' + lastname + ';

Then inside second.php, you can retrieve the value of those variables, and display them as you please.

<?php
$first = $_GET['firstVar'];
$last  = $_GET['secondVar'];
?>

I recommend you redirect the user via Javascript instead of PHP headers because according to this thread it's really easy to screw up PHP headers. I'm assuming that's why you're getting weird behavior.

Community
  • 1
  • 1
M -
  • 26,908
  • 11
  • 49
  • 81