How to change variable value after user submit a form?

Asked

Viewed 121 times

1

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

EDITED, SUGGESTED BY Riscadoooooo and Scribbled

I NEED THAT <i class="fas fa-check"></i> IS DISPLAYED IN THE FORM AFTER THE SUBMIT.

FINAL ISSUE.

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

Someone can help me?

2 answers

1


The solution was to declare the $rowCount value after the INSERT, now the icon is changed right after the user sends the form, and no 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>

0

Natalie, try it like 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 ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.