freddy
freddy

Reputation: 157

Refresh page after only one click on submit

So I have the code bellow. My problem, is that to make the «vous êtes amis» message appear, the user have to click twice on the submit button. But I want to make the user click only once on the submit button. Thanks!

       <?php
       $checkcontact = mysql_query("SELECT * FROM contacts WHERE IDcontact = '".$dnn['id']."'");
       if(mysql_num_rows($checkcontact) == 1)
           {
           echo "<h1>Vous êtes amis</h1>";
           }
           else
           {
       ?>

<form method="post" action="profile.php?id=<?php echo $dnn['id']; ?>">
    <input type="hidden" value="<?=$dnn['Username'];?>" name="userid" />
    <input type="submit" value="Add as Friend" />
</form>


<?php
if ($_POST)
{  
  $fid = htmlspecialchars(stripslashes($_POST['userid']));
  $fid = mysql_real_escape_string($fid);
      include("base.php");
      mysql_query("INSERT into contacts (ID, Nom, IDcontact, Nomcontact) VALUES ('".$_SESSION  ['id']."', '".$_SESSION['Username']."', '".$dnn['id']."', '$fid')");
      }
}
?>

Upvotes: 0

Views: 606

Answers (2)

DarkMaze
DarkMaze

Reputation: 263

you must put your controller code if ($_POST)... before selecting data from database

Upvotes: 0

Paul Dessert
Paul Dessert

Reputation: 6389

Move this block of code to the end:

  $checkcontact = mysql_query("SELECT * FROM contacts WHERE IDcontact = '".$dnn['id']."'");
   if(mysql_num_rows($checkcontact) == 1)
       {
       echo "<h1>Vous êtes amis</h1>";
       }
       else
       {

This checks the value in the database AFTER it's been inserted. Right now, You're checking before it's been submitted

Upvotes: 1

Related Questions