Page 1 of 1

problem on page refresh

Posted: Sat Aug 08, 2009 6:02 pm
by dimebag
hello
im very new to php coding
having some problem hope you can help me
i have form with text input field which take the username and and the php codes for this is in the same page as the form and then the php code will do its job
the php code do its job as it is supposed to do but the problem is for the first time that the page loads everything is ok but if i put anything in my form input box ( the username i want ) and submit the php code will the desired job but everytime i refresh the page or revist it the same username will be added to database though there is nothing in the input filed and this happens until i enter new username and then again on every page refresh then the new username start adding to databse
hope i have explained clearly
this my code i tried unset or ob_start and end_clean but nothing i tried if not empty too but still nothing

Code: Select all

<form action="" method="post" name="username">
<input type="text" name="username" />
<input type="submit" value="submit"  />
</form>
<?php 
 
ob_start() ;
$username= $_POST['username'];
if (!empty ($username)) {
         
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');
 
if (!$conn)
  {
  die('Could not connect: ' . mysql_error());
  }
//select from database
 
mysql_select_db("amir", $conn);
 
$add = mysql_query ("INSERT INTO users (username)
VALUES ('$username')");
if ($add == true )
{echo 'done'; } ;}
unset ($username);
ob_end_flush();
ob_end_clean ();
 

Re: problem on page refresh

Posted: Sat Aug 08, 2009 7:59 pm
by aceconcepts
If I was going to run validation code within the same page I would do it before the form is output.

Good practice to determine whether a form has been submitted is to use isset():

Code: Select all

if(isset($_POST['submit'])){ //execute remaining code }
The reason your entries are duplicated in your database is due to the fact that you have previously submitted an HTTP POST form data set. The browser will retain this as the last action as you're sending the form to itself (the same page as it resides). Refreshing the page after a form submit is just like clicking the submit button again - as you are aware.

To stop duplicate database entries you should always validate insertions before they are made anyway.

Hope this helps.

Re: problem on page refresh

Posted: Sun Aug 09, 2009 5:08 am
by dimebag
thanks alot for this
that was the exac problem i had
thanks for your nice tips
regards

Re: problem on page refresh

Posted: Sun Aug 09, 2009 8:55 am
by dimebag
hello
what is wrong with my code now ?
i have added the function if (isset ($_POST['submit']))
still have the same problem on page refresh again same data enters to DB
what am i doing wrong ?

Code: Select all

 
?php 
 
 
 
if (isset ($_POST['submit'])){
$username= $_POST['username'];
         
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');
 
if (!$conn)
  {
  die('Could not connect: ' . mysql_error());
  }
//select from database
 
mysql_select_db("amir", $conn);
 
$add = mysql_query ("INSERT INTO users (username)
VALUES ('$username')");
if ( $add == true ) {
    echo " done ";
} else {
    echo " error";
}
 
;}
?>
<form name="input" action="" method="post">
<input type="text" name="username" />
<input type="submit" value="Submit" name="submit" />
</form>

Re: problem on page refresh

Posted: Sun Aug 09, 2009 9:29 am
by aceconcepts
The thing is that when you refresh a page after submitting a form it reacts in the same way as clicking submit.

You need to perform a validation check on your database e.g.

Code: Select all

 
if(isset($_POST['submit'])){
  //query database to check whether records exist
  //if not then add records
}
 

Re: problem on page refresh

Posted: Sun Aug 09, 2009 9:48 am
by frao_0
Regarding your problem, when you refresh the page, does your browser issue an alert like "If you refresh this page, it will resend post data. Do you want to continue?".

Also, some advice on your code:

On line 14, you have redundancy. You already specified in the previous line to die an error message if MySQL connexion failed. Plus, in line 16, you call mysql_error(), but this function works only to issue errors once connexion has been made. Also, in line 24, you can phrase if($add) instead of if($add==true).

Hope it helps

Re: problem on page refresh

Posted: Sun Aug 09, 2009 3:21 pm
by dimebag
yes yes this is exactly what happens
if i refresh the page it asks for retry or cancel for resendig info
so how should i disable it ?
thanks

Re: problem on page refresh

Posted: Sun Aug 09, 2009 4:59 pm
by aceconcepts
You can't disable it - it's how your browser works. It's the result of a form submission. GET on the other hand does not cause this.

However, there are pros and cons. Using POST as your method, a page refresh is one of the predictable encounters you will face so in order to gurad against it regarding your database you should have query validation in place - this should be done at all times anyway.

Re: problem on page refresh

Posted: Mon Aug 10, 2009 3:22 am
by frao_0
agreed with aceconcepts. One of the work-around might be to send the form to a different URL that will process the form and then redirect to the page where the form is. Or use an iframe and the target attribute in your form element. Or use AJAX to send the form in the background