You are here: Re: refresh causes double post « PHP Programming Language « IT news, forums, messages
Re: refresh causes double post

Posted by Erwin Moller on 02/24/06 12:36

Mark wrote:

> i'm using an html form to submit comments to a database. the form is on
> the same page where the comments are being displayed. if you hit
> submit, your comment appears as it should, but then if you hit refresh
> there is a duplicate post. how might i fix this?

Hi Mark,

That has more to do with the browser than php.

Look at your action-tag in your form.
(It is very stange in your case by the way)

If a form is submitted the browser expects a page as anser.
In most cases this is the page the receiving script of the form generates.

Now if you hit 'refresh' the browser assumes you want the same page
refreshed, the one that was produced as a result of your posting.

One simple way to 'fix' this (because nothing is wrong) is:
page1.php contains form
set the action to page1_process.php

page1_process.php
receives the form, does its stuff like databaseinserts.
Do not create ANY output.
End you script with something like this:

$result = urlencode("Thanks for posting!");
header ("Location: page1.php?result=".$result);


And in page1.php you could display the result if it exists in the
$_GET["result"].

Now the browser gets the news that page1.php is the one to be displayed.
Refreshing it will only refresh the page and not do a reposting.

If you set up all your scripts like this:
1) You never get that annoying problem again
2) You neatly seperate processing pages from contentrich pages, thus
avoiding long and bugprone scripts that do it 'all functionality at once'.


Regards,
Erwin Moller

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация