Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP session variable not working across 2 pages

Creating form and trying to carry variables accross two pages to a results page. I have tried $GET and $POST and it works fine from page1.php to results.php, but when I change to $SESSION the variable isn't passed or echo'd on results.php. Here's the php code for page1 which won't even work directly to results! Not sure if there is a problem with my code or possibly the server? Page 1.php:

<?php session_start();?>
<?php
$name = $_SESSION['name']; 
?>
<FORM action="results.php" method="post" enctype="multipart/form-data" id="questionnaire">
<input type="text" name="name" id="name" />

Results.php:

<?php session_start();?>

<html>           
    <body>                   
        <?php 
        $name = $_SESSION['name'];
        echo $name; ?>                               
    </body>           
</html> 
like image 712
jmill23 Avatar asked Jul 20 '26 15:07

jmill23


1 Answers

Please try executing following code snippet

<?php session_start();?>
<?php
   if($_SERVER['REQUEST_METHOD']=='POST')
   {
    $name = $_POST['name'];
    $_SESSION['name']=$name;
  }
 ?>
  <FORM action="results.php" method="post" enctype="multipart/form-data" id="questionnaire">
  <input type="text" name="name" id="name" />

Actually in your code snippet value for $_SESSION['name'] was not set .so I have defined value for session variable with posted value from HTML form

like image 143
Rubin Porwal Avatar answered Jul 22 '26 09:07

Rubin Porwal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!