PHP session variable not being stored

172

My session variables are not being stored.

I tried lot of things likevar_dump,print_r and my variables are well printed but not stored.

Here is my code:

<?php
session_start();
require "header.php";
?>

  <form action="../Views/index.php?page=createSecteur" method="post">
    <div >
      <label for="libelle">Libelle : </label>
      <input type="text" name="libelle" id="libelle" required value="<?php echo isset($_SESSION['libelle']) ? $_SESSION['libelle'] : "" ?>">
    </div>
    <button type="submit" name="insert_secteur" id="insert_secteur">Insert secteur</button>
  </form>

<?php
if (isset($_POST['insert_secteur'])) {
  $_SESSION['libelle'] = $_POST['libelle'];
}
require "footer.php";
?>

UPDATE : My $_POST['libelle'] gave me an error "undefined index"

964

Answer

Solution:

Try to check if$_POST['libelle'] exists too.
Also try to post to the same page by empty theaction of your form.
This will automatically be the same page when empty.

<?php
session_start();
require "header.php";
?>

  <form action="" method="post">
    <div >
      <label for="libelle">Libelle : </label>
      <input type="text" name="libelle" id="libelle" required value="<?php echo isset($_SESSION['libelle']) ? $_SESSION['libelle'] : "" ?>">
    </div>
    <button type="submit" name="insert_secteur" id="insert_secteur">Insert secteur</button>
  </form>

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' AND isset($_POST['insert_secteur']) AND isset($_POST['libelle'])) {
  $_SESSION['libelle'] = $_POST['libelle'];
}
require "footer.php";
?>
404

Answer

Solution:

It does work if you remove action url (If you post to same page):

<?php
  session_start();
  require "header.php";
  ?>

    <form action="" method="post">
      <div >
        <label for="libelle">Libelle : </label>
        <input type="text" name="libelle" id="libelle" required value="<?php echo isset($_SESSION['libelle']) ? $_SESSION['libelle'] : "" ?>">
      </div>
      <button type="submit" name="insert_secteur" id="insert_secteur">Insert secteur</button>
    </form>

  <?php
  if (isset($_POST['libelle'])) {
    $_SESSION['libelle'] = $_POST['libelle'];
  }


  echo $_SESSION['libelle'];
  require "footer.php";
?>

If you post to other page then post execution must be done on other page.

But if you need to know specific form was sent you can do this: By saving hidden input with key name

<?php
  session_start();
  require "header.php";
  ?>

    <form action="" method="post">
      <div >
        <label for="libelle">Libelle : </label>
        <input type="text" name="libelle" id="libelle" required value="<?php echo isset($_SESSION['libelle']) ? $_SESSION['libelle'] : "" ?>">
        <input type="hidden" name="source" value="createSecteur"/>
      </div>
      <button type="submit" name="insert_secteur" id="insert_secteur">Insert secteur</button>
    </form>

  <?php
  if($_SERVER["REQUEST_METHOD"] == "POST"){
    if (empty($_POST["source"])){$source = null;}else{$source = htmlentities($_POST['source'], ENT_QUOTES, "UTF-8");}

    if($source=='createSecteur'){
      if (isset($_POST['libelle'])) {

        $_SESSION['libelle'] = $_POST['libelle'];
      }
    }
  }


  echo $_SESSION['libelle'];
  require "footer.php";
?>

People are also looking for solutions to the problem: wordpress - Upload multiple images to woo commerce in php

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.