How to Prevent Duplicate on page refresh? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to Prevent Duplicate on page refresh?

i created form using php, mysql. when i refresh the page, it automatically submit previous data. how to resolve it? This is my git link. https://github.com/eforever/RegistrationFormExample

25th Aug 2018, 2:57 PM
Shanmugarajeshwaran R
Shanmugarajeshwaran R - avatar
11 Answers
+ 4
Well, I have an idea, but mind that this is a totally untested. Suppose your form.php submits data to savedata.php; where you process the data (save to database etc.) and once you're done, issue redirection by: header("location:http://localhost/form.php?status=1"); and in your form.php you check the $_GET for this argument; if(isset($_GET['status'])) { if($_GET['status'] == '1') // server said data had been saved } I don't even know if it will work, but maybe you can try : )
27th Aug 2018, 12:19 PM
Ipang
+ 5
Without the code, it's pretty hard to debug, if not impossible.
25th Aug 2018, 3:04 PM
Hatsy Rei
Hatsy Rei - avatar
26th Aug 2018, 6:21 PM
Ipang
+ 3
Comment the header to see the echo output, the echo output isn't there anymore because of redirection by header location call.
27th Aug 2018, 10:25 AM
Ipang
+ 3
The article didn't say anything about getting message from server, I don't know how it's done, sorry.
27th Aug 2018, 11:38 AM
Ipang
+ 2
I don't know that, as I said, it was just a thought, of course the URL in address bar showed that because it was GET method : ) An alternative for this, I guess is to show message in savedata.php to notify user that operation succeeded, and put a link (button) to redirect to form.php again. But I guess the best way to do this would be to use AJAX, as it works asynchronously and transparent, data is transferred back and forth in background, and no redirect or page reload was needed. There are plenty tutorials out there for learning, perhaps start looking at w3schools, it's lessons are beginner friendly : )
27th Aug 2018, 1:01 PM
Ipang
+ 1
<html> <!-- disallow browser cache --> <meta HTTP-EQUIV="Pragma" content="no-cache"> <meta HTTP-EQUIV="Expires" content="-1" > <?php if(isset($_POST['sdf'])){ echo 'POSTED'; header('Location:http://localhost/post.php'); }else{ echo 'no values' ; } ?> <form action='' method='post'> <input type='text' name='sdf' value='sdf'> <input type='submit' value='submit'> </form> </html> you are right. but, from above code echo not working. header(); only working.
27th Aug 2018, 9:38 AM
Shanmugarajeshwaran R
Shanmugarajeshwaran R - avatar
+ 1
thank you
27th Aug 2018, 11:52 AM
Shanmugarajeshwaran R
Shanmugarajeshwaran R - avatar
+ 1
its working. but when i refresh this page, still it showing same message because of url. how to reset the url?
27th Aug 2018, 12:48 PM
Shanmugarajeshwaran R
Shanmugarajeshwaran R - avatar
0
https://github.com/eforever/RegistrationFormExample This is my github link of code. Please check it.
26th Aug 2018, 7:20 AM
Shanmugarajeshwaran R
Shanmugarajeshwaran R - avatar
0
then how to get message from server?
27th Aug 2018, 10:38 AM
Shanmugarajeshwaran R
Shanmugarajeshwaran R - avatar