473,387 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to prevent form data being re-submit when page is refreshed?

ganesanji
Hi,


I have written a simple php coding for getting the name , phone number and insert the values into data base. My problem is that when ever I just click the refresh button in the browser window, it will automatically previous inserted values are once again inserted into databse as a new entry.

Please anybody give the perfect coding which will avoid automatic insertion of previous values into database.....

I attched my coding below........
Expand|Select|Wrap|Line Numbers
  1. <form id="form1" name="form1" method="post" action="index.php">
  2. Name:<input name="name" type="text" /><br />
  3. Phone Number:<input name="pnumber" type="text" /><br />
  4. <input type="submit" name="submit" value="submit" />
  5.  
  6. </form>
  7. <?
  8. $submit=addslashes($_POST['submit']);
  9. if(isset($submit))
  10. {
  11. $name=addslashes($_POST['name']);
  12. $pnumber=addslashes($_POST['pnumber']);
  13. $sql="insert into details(phone,name) values ('$pnumber','$name')"; 
  14. mysql_query($sql) or die('Could not Connect the database'.mysql_error());
  15. echo"<script language=javascript>alert('Your details are stored successfully');</script>";
  16.  
  17. }
  18. ?>
  19.  
Any body help me plz very urgent.........

thanks....
Sep 27 '07 #1
4 10491
Yes it is the problem while refreshing the page.
Why don't you try to empty all the fields when the query is successful.
Or you can redirect the page to another page when the query is successful.
Or you can add a hidden field whose value u can set when clicking the submit button. Then you can check that hidden value while inserting the values.
Sep 27 '07 #2
Atli
5,058 Expert 4TB
Hi.

In this case, I would recommend submitting the form to a different page, which would then process the data.

If, however, you need to keep it all in the same form, consider something like this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.   # Check if the $_GET['sent'] element is set
  3.   if(!isset($_GET['sent'])) {
  4.     # Process the data
  5.     #  Note, for the header function to work, nothing can be
  6.     #  printed before it is called! 
  7.     header("Location: ?sent=true");
  8.   }
  9.   else {
  10.     echo "Your data has been added!";
  11.   }
  12. ?>
  13. <form action="?" method="POST">
  14.   <!-- your fields here -->
  15. </form>
  16.  
Sep 27 '07 #3
Yes it is the problem while refreshing the page.
Why don't you try to empty all the fields when the query is successful.
Or you can redirect the page to another page when the query is successful.
Or you can add a hidden field whose value u can set when clicking the submit button. Then you can check that hidden value while inserting the values.
thanks..........
now its working fine.............
Sep 28 '07 #4
Hi.

In this case, I would recommend submitting the form to a different page, which would then process the data.

If, however, you need to keep it all in the same form, consider something like this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.   # Check if the $_GET['sent'] element is set
  3.   if(!isset($_GET['sent'])) {
  4.     # Process the data
  5.     #  Note, for the header function to work, nothing can be
  6.     #  printed before it is called! 
  7.     header("Location: ?sent=true");
  8.   }
  9.   else {
  10.     echo "Your data has been added!";
  11.   }
  12. ?>
  13. <form action="?" method="POST">
  14.   <!-- your fields here -->
  15. </form>
  16.  

thanks for ur suggestions.....
it is working now fine
Sep 28 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: John | last post by:
How do I prevent blank data from being entered into a table? For instance, the user fills out a form, but if a field is left blank, then the entire entry won't be filled in. This isn't...
1
by: Job Lot | last post by:
How can I prevent multiple instances of MdiChild form? I have a MdiParent form with a DataGrid showing all the clients in the database. User clicks on a row to open MdiChild form which display...
5
by: Morris | last post by:
This may not be possible on the server side, so apologies if this is the wrong group for this post. My form consists of an unknown number of pairs of text boxes. They are named textbox_a and...
6
by: Miguelito Bain | last post by:
hi everybody- i have a form with 2 fields on it that i want the user to fill out before he or she can save the record, close the record, or move to the next record, etc... here's the code i...
3
by: Randy | last post by:
I want to set up a table where I can enter dates that will prevent data entry of Dates in the Main table. I have done this in Approach by linking two tables and setting up a validation formula...
10
by: hendedav | last post by:
Gang, I am not quite sure if this has been posted before. I have searched google groups and couldn't find anything. I am trying to setup a container div that contains columns of divs (to...
11
by: ariel81 | last post by:
i have created a function to input data from four textboxes into a table. how do i prevent duplicate records of the same mth/Yr into the table? editing of the data in the table with reference to the...
3
by: MyWaterloo | last post by:
Heya... Thanks in advance if you can help... or at least understand my gibberish. I have 2 tables, let's call them tblBacT and tblBacTData, they are related through the auto number primary key...
1
by: MyWaterloo | last post by:
Hey, I'm trying to figure out how to prevent entry into a new record on a data sheet or columnar sub form. I want the user to finish the record they are working on before moving on to the next. ...
1
by: sennopati | last post by:
Dear, I have looping statement like this: For Each frmChild As Form In Me.MdiChildren 'Ada validasi untuk periksa status data(sudah disimpan atau belum) frmChild.Close() frmChild = Nothing...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.