-1

i have one page having the php codes and the form. after an insertion if i refresh the page insertion is made again i would like to prevent that.

 if (!empty ($_POST['submit']) AND $_POST['submit'] == 'Save')
 {

 $name = mysqli_real_escape_string($conn,$_POST['nom']);
 $username=mysqli_real_escape_string($conn, $_POST['username']);
$password=$_POST['password'];
 $sql = "INSERT INTO utilisateur VALUES ('', '$name', '$username', '$password')";
 $res = mysqli_query($conn, $sql);
  echo '<p style="text-align:center">Un utilisateur a ete ajoute avec succes !</p>';
}
else
 {

 }


  <form action="" method="post"  name="form"> 
  <label>Nom <em>*</em></label>
 <input type="text" name="nom" onkeyup="verif_name(this);" required />
  <label>Nom utilisateur <em>*</em></label>
 <input type="text" name="username" required /></br></br>
  <label >Password <em>*</em></label>
 <input type="password"   name= "password" id= "password">

  <input type="submit" id="submit" name="submit" value="Save">
 </form>
David
  • 1,147
  • 4
  • 17
  • 29
branche
  • 73
  • 8
  • 1
    https://en.wikipedia.org/wiki/Post/Redirect/Get – j08691 Feb 04 '16 at 15:03
  • ^ This. Redirect the user to the same page. If you want to show a "success" message you have to save stuff to the users session. An easy pre-built solution would be to [use session flashes](https://github.com/plasticbrain/PhpFlashMessages). – h2ooooooo Feb 04 '16 at 15:06
  • would it be a matter if you show me how to use session flashes in my exemple ? – branche Feb 04 '16 at 15:39

2 Answers2

0

Use redirection and different files and URIs

form.php:

<?php 
    if (!empty($_COOKIE['message']))
    {
        $message = $_COOKIE['message'];
        setcookie('message', '');
        echo '<p>' . $message . '</p>';
    }
?>
<form action="action.php" method="post" name="form">
    <label>Nom <em>*</em></label>
    <input type="text" name="nom" onkeyup="verif_name(this);" required />
    <label>Nom utilisateur <em>*</em></label>
    <input type="text" name="username" required />
    <br /><br />
    <label>Password <em>*</em></label>
    <input type="password" name="password" id="password">
    <input type="submit" id="submit" name="submit" value="Save">
</form>

action.php:

<?php
    if (!empty ($_POST['submit']) AND $_POST['submit'] == 'Save')
    {
        $name = mysqli_real_escape_string($conn,$_POST['nom']);
        $username=mysqli_real_escape_string($conn, $_POST['username']);
        $password=$_POST['password'];
        $sql = "INSERT INTO utilisateur VALUES ('', '$name', '$username', '$password')";
        $res = mysqli_query($conn, $sql);
        setcookie('message', 'Un utilisateur a ete ajoute avec succes !');
    } else {
        setcookie('message', 'Error!');
    }
        header('Location: /form.php');
?>
Mihai Răducanu
  • 12,225
  • 4
  • 22
  • 31
  • Even better, try and use AJAX – Mihai Răducanu Feb 04 '16 at 15:41
  • Thank you for codes the insertion problem is solved but the message in the cookie keeps displaying on the top of the form even after i leave the page or log out – branche Feb 04 '16 at 15:51
  • i did it as well behold the answer i got from php : Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\upage\administrateur.php:9) in C:\wamp\www\upage\administrateur.php on line 120. // Line 120 is setcookie('message', ''); – branche Feb 04 '16 at 15:59
  • Sorry, you cannot set a cookie after you echo something :) – Mihai Răducanu Feb 04 '16 at 16:03
  • i change it problem still there – branche Feb 04 '16 at 16:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102614/discussion-between-branche-and-mihai-rducanu). – branche Feb 04 '16 at 16:13
-1

There is actually a design pattern that adresses this issue and it is called "POST/REDIRECT/GET"

https://en.wikipedia.org/wiki/Post/Redirect/Get

The concept is very simple, after posting some info to the server, you return a redirect header to some page so that the user is does not resubmit the infos twice on F5

A crude example is presented in this question:

Simple Post-Redirect-Get code example

Community
  • 1
  • 1
sashok_bg
  • 2,436
  • 1
  • 22
  • 33
  • Why the down vote ? My answer provides examples and explanations and is based on a so called "good practice" and "design patterns" – sashok_bg Feb 04 '16 at 16:13
  • Probably because you just took the same link I posted 40 minutes before you and didn't add anything than a link to an existing question. Your answer should be complete, not a link to wikipedia and another SO question. – j08691 Feb 04 '16 at 16:21
  • i'm the one who posts the question it's not me who down vote you – branche Feb 04 '16 at 16:32