简体   繁体   中英

Store form values in sessions array

In page1.php a user can enter a day name and it stores in session array called days.

<?
// starting the session
session_start(); 

if (isset($_POST['submit'])) { 
    $_SESSION['days'] = $_POST['days'];
} 
?> 

<strong>Add a day</strong>
<form action="" method"post">
    <input type="text" name="days[]"/>
    <input type="submit" name="submit" value="Submit!" />
</form>
<p><a href="/test/page2.php">Page 2</a></p>

The in page2.php, a user can check which days it has entered:

<?php
session_start();
// loop through the session array with foreach
foreach($_SESSION['days'] as $key=>$value)
    {
    // and print out the values
    echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
    }
?>

But I cant make the session connection correct, it is just blank. Is this the correct way of handling sessions with arrays?

你忘记了page2.php中的session_start()和$ _SESSION!= $ _session

Aren't you forgetting the session_start() in the second page?

Also, why don't you post the values to the second page and use the $_POST instead of putting them in a session first?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM