0

I am trying to add data from a form on one page and send it to another. I have done this elsewhere on the first page and it adds the data. I try it again and when I echo it out on the second page I get a null value.

Session was already started at the top of the page.

Code on the first page:

<?php
if(!isset($_POST['submit'])){}
else{
    $var_fname = $_SESSION['fname'] = $_POST['fname']; 
    $var_lname = $_SESSION['lname'] = $_POST['lname'];
    $var_address = $_SESSION['address'] = $_POST['address'];
    $var_city = $_SESSION['city'] = $_POST['city'];
    $var_state = $_SESSION['state'] = $_POST['state'];
    $var_zip = $_SESSION['zip'] = $_POST['zip'];
    $var_email = $_SESSION['email'] = $_POST['email'];
};
?>

Code on the second page:

<?php
/**
 * @author 
 * @copyright 2022
 */
session_start();
echo $var_session = json_encode($_SESSION["shopping_cart"]);
?>
<html>
<body>
<br /><br /><br />
</body>
</html>
<?php
//echo $var_email = json_encode($_SESSION["email"]);
echo $var_fname = json_encode($_SESSION["fname"]);
?>**

The second echo outputs NULL. Form I used to gather the data:

<form method="post" action='email.php'>
First name:  <input type="text" name="fname"/><br />
Last name:   <input type="text" name="lname"/><br />
Address:     <input type="text" name="address"/><br />
City:        <input type="text" name="city"/><br />
State:       <input type="text" name="state" /><br />
Zip code:    <input type="text" name="zip"/><br />
Email:       <input type="text" name="email"/><br />
<button type="submit">Continue to checkout</button>
</form> 

--output on second page:

{"Bag01":{"name":"Laptop Bag","code":"Bag01","price":"50.00","quantity":1,"image":"product-images\/laptop-bag.jpg"}}

shopping_cart|a:1:{s:5:"Bag01";a:5:{s:4:"name";s:10:"Laptop Bag";s:4:"code";s:5:"Bag01";s:5:"price";s:5:"50.00";s:8:"quantity";i:1;s:5:"image";s:29:"product-images/laptop-bag.jpg";}}
  • 1
    Session was started on both pages? `shopping_cart` also is not set, unclear which value is NULL, also you are outputting after closing document – user3783243 Jan 26 '22 at 19:26
  • Yes session was started on both pages. The shopping_cart was set earlier in the first page and outputs correctly. The null values are from the text input fields from the form I just created right above where I attempt to put fname, lname, ect in the session. – Andy Dempsey Jan 26 '22 at 19:35
  • Just to rule out invalid HTML problems (because you're generating illegal HTML atm): echo your text inside `...`, don't add it before the doctype or after the closing html tag. And to keep things simpler to test: don't json_encode, first find out what's in `$_SESSION` at all. – Mike 'Pomax' Kamermans Jan 26 '22 at 19:55
  • So you are certain `echo $var_fname = json_encode($_SESSION["fname"]);` gives you `NULL` on the browser, and `$var_fname = $_SESSION['fname'] = $_POST['fname']; ` has a value in it? – user3783243 Jan 26 '22 at 19:57
  • I closed the browser to reset the session and now it does not even show that the form data I tried to add at all (not even the null values I was getting. I moved the code into the body of the html and it still has the same problem. I will post above the output on the second page. $var_fname = $_SESSION['fname'] = $_POSR['fname']; is supposed to be getting the value from the textbox in the form created above. – Andy Dempsey Jan 26 '22 at 20:03
  • Read your logs. – symcbean Jan 26 '22 at 20:19
  • That output would at minimum have ` ` in it. View the full page source. – user3783243 Jan 27 '22 at 11:09

1 Answers1

-1

Change -> if(!isset($_POST['submit'])){} to isset , because you want to post it if the submit button is set right?

i would change this:

 $var_fname = $_SESSION['fname'] = $_POST['fname']; 
    $var_lname = $_SESSION['lname'] = $_POST['lname'];
.....

to something like this

$var_firstname = $_POST['fname'];
$_SESSION['fname'] = $var_firstname;

and so on

  • I moved it to the if(isset) now it sends fname, lname, ect to the second page but the values are still null ------ shopping_cart|a:1:{s:5:"Bag01";a:5:{s:4:"name";s:10:"Laptop Bag";s:4:"code";s:5:"Bag01";s:5:"price";s:5:"50.00";s:8:"quantity";i:1;s:5:"image";s:29:"product-images/laptop-bag.jpg";}}fname|N;lname|N;address|N;city|N;state|N;zip|N;email|N; – Andy Dempsey Jan 26 '22 at 20:34