lisovaccaro
lisovaccaro

Reputation: 33956

Can't make "Reload" script inside PHP work

This is what runs after submitting a form:

else
{
    echo '<br/> Tu chisme se agregó con éxito.';
    echo '<script type="text/javascript">window.location.reload(true);</script>';
}

Basically after the users submits the form it does a couple of things and finally displays a text saying that the post has been submitted successfully and then a second echo tries to reload the page. However I wasn't able to make it work. It displays the text but it doesn't reload the page. What's the problem with the code? Please ask for any info. Thanks

Upvotes: 0

Views: 265

Answers (3)

regilero
regilero

Reputation: 30506

The right thing to do after a POST is a 'Redirect After Post', that means sending an HTTP header with a 303 code (not a simple 302 code, most browser will prevent you from reposting with the back button if you use the 303 code, it's done for that purpose).

You should'nt have send anything before sending this HTTP header code (try using ob_start/ob_flush functions to control that).

So, well, in fact the HTTP protocol contains everything you need to control behavior after POST requests, it's really better to avoid use of JavaScript for that.

Upvotes: 2

Erik
Erik

Reputation: 20722

When your script prints out that line with "text/javascript", it's already a new page. You're reloading that new page .. which reloads itself. If you want to direct the visitor to the original page, you can use PHP's header:

header("Location: http://www.example.com/");

Replacing example.com with the target URL. It only works if nothing else has been output on the page first, so you'd have to get rid of that one line of text.

Upvotes: 0

Pradeep Singh
Pradeep Singh

Reputation: 3634

window.location.reload();

Please refer this link

http://programming.top54u.com/Samples/Javascript/Location-Object/Location-Reload/Default.aspx

Upvotes: 0

Related Questions