Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Stop re-sending form data while refreshing the page.

Posted on 2001-07-30
16
936 Views
Last Modified: 2008-03-06
Hi all,

When the user refreshes the page that is alreday submitted, it resends the form data and my table has duplicated rows inserted.

I also want to display the same page after submitting.

How do I prevent the form data being re-submitted to the server while refreshing?

I am using php+postgresql.

best regards
ascetic.

0
Comment
Question by:ascetic
  • 6
  • 4
  • 4
  • +2
16 Comments
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 6333768
Don't bother with testing the submission.

Use your database to either update or reject the duplicated entry.

If I press "Submit" ten times, the data shouldn't have changed.

If your database simply updates the existing data, you will only ever have 1 row!

What sort of data do you have? Is there a unique entry in the table?

If not, then send one to the user when the form is created. When you get the info back, log the unique info in the database. If they re-submit, you will have the unique key to check in the database. If it is present, update the table, if not insert it into the table.

If you can post your current insert code, then I can help further.
0
 

Author Comment

by:ascetic
ID: 6333853

There are 5 tables being inserted from this form.
The first table has the primary key generated from the
sequence and all ther tables  have unique keys.

I do not want to check for duplicate at the back end to save some workload on the server. The table being looked up is very large and there are a number of concurrent access to this form.
regards.

0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 6334067
If you have Primary and Unique set correctly, then how are you getting duplicated rows?

Are all the tables getting the duplicated row? Or just the primary key table? Or not the primary, but the 4 others?

Can you show the code relating to the insert/update of the first table?

I would suggest something like this (not real PHP, but should be understandable).

$result = db_update;
if (number_of_rows_affected($result) > 0)
 {
// We can add the rest of the entries.
 }
else
 {
// Duplicate keys possible?
 }

Sort of thing.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 3

Expert Comment

by:hagur
ID: 6334783
When you have submitted the data to the database, you should redirect back to the page with the form.  If the user will press refresh, all the form data variables will be empty and nothing will be submitted to the database again.

This is how I always do it.

Example:

form.php (or whatever) :

<form action="register.php" method="post">
(your form)
</form>


register.php :

insert into the database ...

then

Header("Location: form.php");


The user is now back at form.php and nothing will happen if he refreshes the page.

This will also work if you only have one file to do this, i.e display the form and add to the database.
Simply use Header("location: filename.php") as soon as you have inserted data into the database, the browser will no longer remember what was in the query string and if you press refresh, it will not re-send the form.  It will just refresh the page.
0
 
LVL 1

Expert Comment

by:fajr_n
ID: 6336862
place this script after insert data on your database

header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");header ("Location: yourform.php");
exit;

if user to try refresh the page, current page only will refresh.

I wish resolve your problem
0
 

Author Comment

by:ascetic
ID: 6337008
hagur /  fajr_n
It doesnot work for me. It seems calls to  header()  
should be only at the begining of the page.
regard
0
 
LVL 1

Expert Comment

by:fajr_n
ID: 6337156
type the script below for example:

filename: test.php
<?
error_reporting(1);
if(isset($Submit))
{
     header ("Cache-Control: no-cache, must-revalidate");
     header ("Pragma: no-cache");
     header ("Location: testx.php?item=$yuhuu");
     exit;
}
else
{
     echo "<form method=\"post\" action=\"test.php\">\n";
     echo "<font face=\"Arial\" size=\"2\">Type some words here :</font>\n";
     echo "<input type=\"text\" name=\"yuhuu\"><br>\n";
     echo "<input type=\"submit\" name=\"Submit\" value=\"Send Me\">\n";
     echo "</form>\n";
}
?>

filename: testx.php
<?
error_reporting(1);
if(!(empty($item)))
{
     echo $item;
}
else
{
     header ("Cache-Control: no-cache, must-revalidate");
     header ("Pragma: no-cache");
     header ("Location: test.php");
     exit;
}
?>

I'm already running it and work.

0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 6339707
All of this doesn't really fix the problems of duplicate rows though.

If the database is allowing them, then you are putting a LOT of work into the system to stop them.

If you have a valid constraint/index/uniqueness on the database, they simply will NOT happen! This negates any futher coding. You can handle it if you want, but the database will simply reject the update.

IMHO, this is a much more resiliant method.

Regards,

Richard.
0
 
LVL 2

Expert Comment

by:higijj
ID: 6340856
Well.. you could set a cookie to the user the first time he submits the data..

and in your script, just evaluate the cookie.. if it exists, simply don't update the data, if it doesn't, well.. it's the user's first time.. so store the data to your data base...


0
 

Author Comment

by:ascetic
ID: 6374995
fajr_n,

I am working out your suggestion. Thanks.

I am using PostGreSql Database as backend. It does not have any exception handling. Therefore, though, any duplicates will be checked by referential integrity check mechanisms, it dumps all weird messages on the user screen.

Moreover why should I do some additional querying on the backend and choke the server if something can be prevented at the client-side itself?

reagards.

0
 

Author Comment

by:ascetic
ID: 6374997
fajr_n,

I am working out your suggestion. Thanks.

I am using PostGreSql Database as backend. It does not have any exception handling. Therefore, though, any duplicates will be checked by referential integrity check mechanisms, it dumps all weird messages on the user screen.

Moreover why should I do some additional querying on the backend and choke the server if something can be prevented at the client-side itself?

reagards.

0
 

Author Comment

by:ascetic
ID: 6375047
fajr_n,

I am working out your suggestion. Thanks.

I am using PostGreSql Database as backend. It does not have any exception handling. Therefore, though, any duplicates will be checked by referential integrity check mechanisms, it dumps all weird messages on the user screen.

Moreover why should I do some additional querying on the backend and choke the server if something can be prevented at the client-side itself?

reagards.

0
 
LVL 1

Expert Comment

by:fajr_n
ID: 6375156
:)
0
 
LVL 1

Accepted Solution

by:
fajr_n earned 300 total points
ID: 6375160
glad to bring my hands with others :)
0
 

Author Comment

by:ascetic
ID: 6379027
fajr_n,

Sorry. What I am looking for is to display the same page after submitting and yet to stop refreshing the page.
Is there any browser api calls available?
regards.
jose.


0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 6379337
Ah.

So.

They fill in the form. Press submit. You then return a text only version of the form.

The page to return the results of the update should be the same layout, just without and of the form controls.

If they need to re-edit the data, you need to include a re-edit link which shows the original form with the field editable.

Regards,

Richard Quadling.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question