dimanche 25 novembre 2018

Check-mark icon on submitted form using PHP

This code below will print out a <i class="far fa-star"></i> if the rowCount == 0 and if !=0 will print out <i class="fas fa-check"></i>, after the user submits the form they will not be able to see the <i class="fas fa-check"></i> unless they refresh the page, after the user submits the form the page refresh itself, and the user needs to refresh one more time to see the <i class="fas fa-check"></i>:

if ($rowCountFav == 0) {
  $favIcon = '<i class="far fa-star"></i>';
}else{$favIcon = '<i class="fas fa-check"></i>';}

if($_SERVER['REQUEST_METHOD'] == 'POST'){
  if(isset($_POST["fav"])){ 
    if ($rowCountFav == 0) {
      $favorito = $conn->prepare("INSERT INTO `favorito` (user_id, nameItem) VALUES (:user_id, :nameItem)");
      $favorito->bindParam(':user_id', $user_id, PDO::PARAM_INT);
      $favorito->bindParam(':nameItem', $nameItem, PDO::PARAM_STR);
      $favorito->execute();
    }
  } 
}
?>
<form action="" method="post" autocomplete="off">
  <button class="btnSub btnA" type="submit" name="fav" />
    Favorito <?= $favIcon;?>
  </button> <span class="ml-1 mr-2">-</span> 
</form> 

What i want: i want to print out the <i class="fas fa-check"></i> after the user submits the form.

So i tryed this, but nothing changed:

if($_SERVER['REQUEST_METHOD'] == 'POST'){
  if(isset($_POST["fav"])){ 
    if ($rowCountFav == 0) {
      $favorito = $conn->prepare("INSERT INTO `favorito` (user_id, nameItem) VALUES (:user_id, :nameItem)");
      $favorito->bindParam(':user_id', $user_id, PDO::PARAM_INT);
      $favorito->bindParam(':nameItem', $nameItem, PDO::PARAM_STR);
      $favorito->execute();
    }else{$favIcon = '<i class="fas fa-check"></i>';} ## I just added this line ##
  } 
}

Could someone help?

Aucun commentaire:

Enregistrer un commentaire