Forum Moderators: coopster

Message Too Old, No Replies

Data isn't being saved when redirecting web page

         

Kathy_OC

6:39 pm on Mar 17, 2008 (gmt 0)

10+ Year Member



I am new to PHP/MySQL and feel like I'm getting a basic understanding, but I ran into something that baffles me. I have a multi-page form and once the data has been entered, checked for errors, and saved on one of the screens, I want to redirect the page to the next screen. I have tried to redirect using both the PHP header function and Javascript. If I redirect, then the next screen is displayed, but my data isn't saved into MySQL. If I comment out the redirect (leaving the exit command) then the data is saved, but I (of course) don't move to the next screen. This behavior is the same whether I redirect using Javascript or PHP.

What am I doing wrong? Is there a different way to do this?

Thanks,
Kathy

cameraman

9:14 pm on Mar 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm a bit confused by
behavior is the same whether I redirect using Javascript or PHP
.
When you redirect via javascript, are you doing it in the onload event? Otherwise I can't think of any possible way that they could produce the same result, since php would redirect and send the target to the browser while javascript would redirect the browser without submitting the form back to the server to process.

Are you using the same script to display the form as you are to process it?
Would you say this is your general flow:

  1. Display form
  2. on submit event, use javascript to validate data
  3. When submitted, use php to validate data again
  4. If valid data, save
  5. Redirect to next page

Something you could try for debugging purposes is to set up the redirect url in a variable at the top of the script:
$url = '/page2.php';

Right after you save the data to the database,
$url .= '?save=1';

Then down where you do the redirect, use the variable:
header("Location: $url");
exit();

When the next page comes up, look in your address bar - if save=1 isn't there, you likely have a logic problem - for example, you might have the saving embedded in an if clause that isn't getting executed.

Kathy_OC

6:06 pm on Mar 18, 2008 (gmt 0)

10+ Year Member



Yes, I'm using the same script to display the form as to process it and you described my flow correctly. I tried location.href to do the redirect in javascript.

I tried your debugging idea with both the header function and location.href and the page redirects with the save=1 in the address bar. - but my data still isn't saved to the table. As I said earlier, if I comment out the redirect, my data saves to the table like it should.

Thanks for your debugging suggestion. I'll keep trying to figure this out and if you have other ideas, I would be happy to hear them.

Kathy