开发者

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开发者_开发知识库


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.


window.location.reload();

Please refer this link

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜