Jump to content

how to avoid sending data on refresh


rugzo

Recommended Posts

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
?>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.