How to save variable and use after page refresh

Asked

Viewed 227 times

0

//variavel recebe de input html
$ssid = @$_POST['user']; 
 if(isset($_POST['enviar'])
{
  //deveria printar valor na tela
  echo $ssid;
}

The variable $ssid receives an input value, but when I click on the send button the value of the variable goes along with the page refresh as save the value of the variable to use after page refresh?

  • tried to save it in $_SESSION?

2 answers

1


php is stateless, so each request is independent, not related to the previous one, because the attribute status is not maintained. Therefore Voce can store in the session as follows:

<?php 
// session_start inicia a sessão
session_start();
//variavel recebe de input html
$ssid = @$_POST['user']; 
$_SESSION['ssid'] = $ssid;
  • It didn’t even work with Session when you enter if(isset), the value goes missing

  • It worked, but I had to change the method from $_POST to $_GET

  • Show, if you had to change from POST to GET is because your form is sending as GET, I only suggested POST because as you put this in your example gave meet that your form was POST. I just didn’t understand why my answer wasn’t marked as the correct one and I answered it well before.

0

To be able to store the variable even after the page update, use $_SESSION.

To log into the file you are working = session_start();

$_SESSION['ssid'] = @$_POST['user']; // Aqui você está armazenando a sua varivel recebida na $_SESSION; 

 if(isset($_POST['enviar'])
{
  echo $_SESSION['ssid']; 

}

If you want to remove Session, unset($_SESSION['ssid is used']);

To learn more about Sessions, the PHP website has her manual https://www.php.net/manual/en/reserved.variables.session.php

  • it still didn’t work

  • It worked, but I had to change the method from $_POST to $_GET

Browser other questions tagged

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