rugzo Posted October 17, 2008 Share Posted October 17, 2008 Hi guys, i made a simple database where i want to keep my data. I did it but i cannot avoid resending data on refreshing the page. The code is below. Can anyone help, thanks... <html> <head> <title>Rapor Takip</title> </head> <body bgcolor="#80FFFF"> <br> <br> <form action="rapor.php" method="post" name="rp"> <table border="1"> <tr> <td width="30">Agent</td> <td width="10">Rapor(ssk/normal)</td> <td width="10">Gün Sayısı</td> <td width="10">Tarih</td> </tr> <tr> <td width="30"><input type="text" name="Agent" size="20"></td> <td width="20"><input type="text" name="Rapor" size="20"></td> <td width="20"><input type="text" name="Gun" size="20"></td> <td width="20"><input type="text" name="Tarih" size="20"></td> <td width="20"><input type="button" name="Gonderen" value="submit" size="20" onclick="if(rp.Tarih.value.length==0) alert('nme empty'); else rp.submit();"></td> <td width="20"><input type="reset" value="reset" size="20"></td> </tr> </table> </form> <?php @mysql_connect ("localhost", "root", "123*qwe123") or die ("nix connect sql") ; @mysql_select_db ("nsn") or die ("nix db") ; $Agent = $_POST["Agent"] ; $Rapor = $_POST["Rapor"] ; $Gun = $_POST["Gun"] ; $Tarih = $_POST["Tarih"] ; if ($Agent <>"") { $sql = "INSERT INTO rapor (Agent, Rapor, Gun, Tarih) values ('$Agent', '$Rapor', '$Gun', '$Tarih')" ; $kayit = mysql_query($sql) ; echo mysql_error() ; } ?> <table border="1"> <tr> <td width="5"><font face="Arial Black" size="2">Nr</font></td> <td width="20"><font face="Arial Black" size="2">Agent</font></td> <td width="10"><font face="Arial Black" size="2">Rapor</font></td> <td width="10"><font face="Arial Black" size="2">Gun</font></td> <td width="10"><font face="Arial Black" size="2">Tarih</font></td> > </tr> <?php @mysql_connect ("localhost","root","123*qwe123") or die ("nix sql") ; @mysql_select_db ("nsn") or die ("nix db") ; $sql = mysql_query ("SELECT * FROM rapor") ; while ($liste = mysql_fetch_array($sql)) { ?> <tr> <td width="16%"><font face="verdana" size="2"><? echo "$liste[0]"; ?></font></td> <td width="16%"><font face="verdana" size="2"><? echo "$liste[1]"; ?></font></td> <td width="17%"><font face="verdana" size="2"><? echo "$liste[2]"; ?></font></td> <td width="13%"><font face="verdana" size="2"><? echo "$liste[3]"; ?></font></td> </tr> <? } echo mysql_error() ; ?> </table> </body> </html> Quote Link to comment Share on other sites More sharing options...
CroNiX Posted October 17, 2008 Share Posted October 17, 2008 I suppose an easy way would be to use sessions. You can have a counter on your page that gets stored in a session variable. Check the variable at the top of the script and if its greater than 1 dont insert. Quote Link to comment Share on other sites More sharing options...
rugzo Posted October 17, 2008 Author Share Posted October 17, 2008 My Problem is this; After i enter the values and click on submit i get the page with the empty fields and with the entered string. Everything is fine till here. But if i click refresh i get a second string with the same information. And same on the third one. How can i prevent this. Can you provide me the code. Quote Link to comment Share on other sites More sharing options...
.josh Posted October 17, 2008 Share Posted October 17, 2008 Are you trying to prevent someone from resubmitting info in general, or just avoid posting the same thing twice if you accidentally refresh? Quote Link to comment Share on other sites More sharing options...
rugzo Posted October 17, 2008 Author Share Posted October 17, 2008 just avoid posting the thing twice by accidentally refresh... Quote Link to comment Share on other sites More sharing options...
.josh Posted October 17, 2008 Share Posted October 17, 2008 I don't really see how you can accidentally move your curser all the way up to the refresh button... but anyways... Couple of possible suggestions: - Add a timestamp of some kind to your table. Check when the last time a user inserted something into the database and don't insert if it's < x time. I suppose someone could wait until after x time is passed and then hit the refresh button, but it would prevent those initial "accidental" refreshes. - Run a query on all the posted vars and check if there is a row where all the posted vars match. If a row is returned, then don't insert a new row. This isn't really efficient...mostly depends on how many vars there are and what's in them... - Use a javascript event handler to prevent user from using refresh button. Downside is that user can easily disable it. If we're talking about "accidental" refreshes, I don't think you need to worry about that...though the user may not have javascript enabled in the first place... - As cronix suggested: make a session variable and check for it. Don't allow insertion if it exists. And no, we cannot provide you with the code. We aren't here to write the code for you. Just point you in the right direction and help you fix bugs in your own code. If you want code written for you, hire a freelancer. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 17, 2008 Share Posted October 17, 2008 There is a very simple solution to this. Just use a header to redirect after you process the post data. You can have the page redirect to a confirmation screen. Or, if you have the form post to itself so the user can enter another record, have the page redirect to itself after processing. All POST data is stripped when redirected in that manner. Quote Link to comment Share on other sites More sharing options...
loving_php2008 Posted October 18, 2008 Share Posted October 18, 2008 i need help with php search engine.. i want to know if we have a big text and we want to serach for a word in it and contain 4 words in different line or maybe in same line... and i want i get 4 results... thanks for all Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted October 18, 2008 Share Posted October 18, 2008 i need help with php search engine.. i want to know if we have a big text and we want to serach for a word in it and contain 4 words in different line or maybe in same line... and i want i get 4 results... thanks for all Don't hijack other peoples topics, that's just common courtesy. Go and post your own in the PHP Help section, but to me it sounds like you want some Freelance work done, so post in the Freelance section. Quote Link to comment Share on other sites More sharing options...
sprintlife Posted October 18, 2008 Share Posted October 18, 2008 The double Submit Fix by O'Reilly Just read the PDF. I hope this works for you. [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
CroNiX Posted October 18, 2008 Share Posted October 18, 2008 The double Submit Fix by O'Reilly Just read the PDF. I hope this works for you. Thats a lot of unneeded stuff...using a session variable you could do that in like 10 lines of code. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted October 18, 2008 Share Posted October 18, 2008 Some code to do this using a session - <?php session_start(); // session variable to check if the form processing code has already been executed once if(!isset($_SESSION['processed'])){ // if the session variable does not exist, create it and initialize it to false $_SESSION['processed'] = false; } if($_SESSION['processed']){ // form data has already been processed exit('Thank you, the form data has already been processed.'); } .... // put the following line in your actual code at the point that the data has been successfully processed - $_SESSION['processed'] = true; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.