Jump to content

Can I stop PHP page loading in browser?


cherryaa

Recommended Posts

Hi, I'm new. I have been searching for this, but couldn't find my answer ...

 

I've got a form on page A, with its 'action' attribute set to page B.php.

Page B does nothing but process the form data & put it in a database.

At the bottom of B.php there's a header redirect to page C. 

 

My problem is that page B shows up briefly in the browser, looking all blank and weird. Is there a sensible way around this? I just want it to do its stuff in the background.

 

I realise I should be Ajaxing it all but, apart from the fact that I struggle with Ajax operations longer than a couple of lines, it's important that the site should work without javascript.

 

I HOPE this is a really easy one! Thanks :)

 

 

Link to comment
Share on other sites

Blimey, that was quick! Thanks.

 

I have found a better way to cheat - I put page B in a tiny iframe on page A  :D

 

I do want to know what's wrong, though, so am posting short versions below.

 

pageA.html

<form name="buythings" id="buythings" action="/pageB.php" method="post" target="">
<input stuff />
<submit>
</form>

I have added target="tinyframe" to it now.

 

pageB.php

<?php

if( isset( $_POST['stuff'] )
{ do calculations and update MySQL tables; }

header( "Location:http://cherryaa.com/pageC.html" );

?>

 

I also tried it without the redirect, with return and with exit. No difference.

 

Page C is just a page. It gets the new data out of the db, but doesn't directly interact with pageB.

 

Any clues?

Link to comment
Share on other sites

Page B should never be seen unless it's doing output.

 

What he said. And it would be impossible for page B to have any ouput and still have a header redirect at the bottom. Most likely the code on page B has some inefficient code and you are looking at a "white-page" while the code is processing.

Link to comment
Share on other sites

OK, mjdamato, it's only doing this because my PHP's slow to execute?

You mean I haven't done it wrong, I've done it badly - that's fixable. Thanks!

 

(The bracket was only missing in my example, litebearer, though I do have bad habits as seen!)

Link to comment
Share on other sites

OK, mjdamato, it's only doing this because my PHP's slow to execute?

You mean I haven't done it wrong, I've done it badly - that's fixable. Thanks!

 

I can't say it is right or wrong because I haven't seen all the code. But, as I stated it is IMPOSSIBLE for a PHP script to send output tot he browser THEN to do a header redirect. It's right there in the manual for the header() function. Not to mention we see many posts on these forums by users who don't pay attention to that requirement askign why they are getting a header error about content already being sent.

 

If your processing page has any loops - especially ones that run queries - that is a place to start looking.

Link to comment
Share on other sites

You're right. The script does a lot of processing, creating new variables from the retrieved information then working with those.

I've decided to buckle down and put it all into classes (another of my most-hated activities!), which will force me to think efficiency.

 

So ... while I don't feel like thanking you for making me rework my code  ;) I have to!

Link to comment
Share on other sites

It's unlikely that the time your form processing page is taking is causing this. You also didn't give us any indication of how long your form processing page takes to run/how long this blank page is displayed?

 

I'm going to guess your symptom is being caused by something you are doing on pageA in the browser (some validation or other), because I just simulated what you state you are doing (form on pageA and a 10 second delay on pageB before redirecting to pageC) and the result in two different browsers was - after submitting the form, pageA was still being displayed and the pageA status bar indicated waiting for the server..., then after the 10 second delay the redirect caused pageC to be requested and displayed.

 

If you want help with your actual problem, you will need to post enough of your code that REPRODUCES the problem. If the code you are going to post doesn't reproduce the problem for you on your system, don't bother posting it.

 

The 'efficiency' you have heard about concerning classes/OOP has nothing to do with the execution time of your script. In fact, the same exact application written using classes/OOP would take slightly longer to run. OOP is just a different coding method and if your exiting programming is doing something to cause an unusual result, using OOP won't change that until you find and fix what you are doing in your code that is causing the problem.

Link to comment
Share on other sites

That's interesting, PFMaBiSmAd, if somewhat depressing. The behaviour you've described - pageA staying in view until pageB's done its stuff - is what I expected.

 

I shan't post my scripts because (a) pageB is 220 lines, and (b) it's a checkout so posting could compromise security. I will take another look at pageA to see if it's doing anything weird.

 

Am relieved to hear object-ifying it won't make too much difference! I'll continue reviewing my script efficiency, though <cancels life for yet another week>.

 

I'm appreciating all your thoughts, cheers.

 

the result in two different browsers was - after submitting the form, pageA was still being displayed and the pageA status bar indicated waiting for the server..., then after the 10 second delay the redirect caused pageC to be requested and displayed.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.