How to change variable value after the user submits a form?

1

This code will print <i class="far fa-star"></i> if rowCount == 0 and if > 0 will print <i class="fas fa-check"></i> , after the user submits the form they will not be able to see <i class="fas fa-check"></i> unless it refreshes the page , after the user submits the form the page updates itself, but it needs to refresh the page one more time to be able to see <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 you to print the <i class="fas fa-check"></i> after the user submits the form.

  

EDITED, SUGGESTED ScratchOoOoO and RabisCadoO

I NEED THIS <i class="fas fa-check"></i> TO BE DISPLAYED WITHIN THE FORM AFTER SUBMIT.

  

END OF EDITING.

So I tried this, but nothing has 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>';} ## eu apenas add essa linha ##
  } 
}

Can anyone help me?

    
asked by anonymous 25.11.2018 / 19:30

2 answers

1

The solution was to declare the $ rowCount value after the INSERT query, now the icon is changed shortly after the user submits the form, and does not need to refresh the page.

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();
      $rowCountFav = 1;
    }
  } 
}

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

?>
<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>
    
26.11.2018 / 19:54
0

Natalie, try this:

form action="" method="post" autocomplete="off">
  <button class="btnSub btnA" type="submit" name="fav" />
    Favorito <?= if(isset($favIcon)){echo $favIcon;}?>
  </button> <span class="ml-1 mr-2">-</span> 
</form> 

If it works, let us know, please;)

    
26.11.2018 / 15:55