I have something like: <table> <tr> <td><input type="checkbox" name="day[]" value="Saturday" />Satur</td> <td><input type="checkbox" name="day[]" value="Sunday" />Sun</td> <td><input type="checkbox" name="day[]" value="Monday" />Mon</td> <td><input type="checkbox" name="day[]" value="Tuesday" />Tues</td> <td><input type="checkbox" name="day[]" value="Wednesday" />Wednes</td> <td><input type="checkbox" name="day[]" value="Thursday" />Thurs</td> <td><input type="checkbox" name="day[]" value="Friday" />Fri</td> </tr> </table> PHP: and I want to save day[] in a session and reuse it again how can I do that? Thank you in advance
I did somthing like $day =$_POST['day']; PHP: then I saved $day in a session $_SESSION['day'] = $day ; // store session data PHP: then I want to display days in SESSION so I wrote: foreach($_SESSION['day'] as $day){ echo "Session day: ".$day."<br>"; } PHP: and it gives me the following Invalid argument supplied for foreach()
I'm not sure the reason you can't directly assign your $_POST array to a $_SESSION array and there might be a function to make it more effecient, but this worked for me: $_SESSION['day']; for ($i=0; $i<count($_POST['day']); ++$i) $_SESSION['day'][$i]=$_POST['day'][$i]; PHP:
Yes I am calling session_start(); PHP: in the start of the file and day is an array as shown below <table> <tr> <td><input type="checkbox" name="day[]" value="Saturday" />Satur</td> <td><input type="checkbox" name="day[]" value="Sunday" />Sun</td> <td><input type="checkbox" name="day[]" value="Monday" />Mon</td> <td><input type="checkbox" name="day[]" value="Tuesday" />Tues</td> <td><input type="checkbox" name="day[]" value="Wednesday" />Wednes</td> <td><input type="checkbox" name="day[]" value="Thursday" />Thurs</td> <td><input type="checkbox" name="day[]" value="Friday" />Fri</td> </tr> </table> PHP: and I get it from the POST method and save it $day =$_POST['day']; PHP: and the save it to a SESSION $_SESSION['day'] = $day ; // store session data PHP: then I want to display what's saved in SESSION and I don't know how? Can anyone tell me how?
This code will get into the session: $_SESSION['day']; for ($i=0; $i<count($_POST['day']); ++$i) $_SESSION['day'][$i]=$_POST['day'][$i]; PHP: And this code will get it out: for ($i=0; $i<count($_SESSION['day']; ++$i) echo "Session day: ".$_SESSION['day'][$i]."<br>"; PHP:
I have created 3 pages: first page called combobox.php <?php echo '<form name="combo" method="post" action="post_days.php"> <table> <tr> <td><input type="checkbox" name="day[]" value="Saturday" />Satur</td> <td><input type="checkbox" name="day[]" value="Sunday" />Sun</td> <td><input type="checkbox" name="day[]" value="Monday" />Mon</td> <td><input type="checkbox" name="day[]" value="Tuesday" />Tues</td> <td><input type="checkbox" name="day[]" value="Wednesday" />Wednes</td> <td><input type="checkbox" name="day[]" value="Thursday" />Thurs</td> <td><input type="checkbox" name="day[]" value="Friday" />Fri</td> </tr> <tr> <input type="submit" /> </tr> </table> </form> '; ?> PHP: second page: post_days.php <?php session_start(); //get into session for ($i=0; $i<count($_POST['day']); $i++) { echo "POST Day... ".$_POST['day'][$i]."<br>"; $_SESSION['day'][$i]=$_POST['day'][$i]; echo "For 1... Session Day: ".$_SESSION['day'][$i]."<br>"; } //$_SESSION['day'] = $_POST['day']; //get it out for ($i=0; $i<count($_POST['day']); $i++){ echo "For 2... Session day: ".$_SESSION['day'][$i]."<br>"; } echo '<form method="post" action="insert_days.php"> <input type="submit" /> </form>'; ?> PHP: and the output is POST Day... Saturday For 1... Session Day: S POST Day... Wednesday For 1... Session Day: W For 2... Session day: S For 2... Session day: W I don't know why it's saved as single letter I want to know how to solve this problem
Look at what you're inserting and echoing: $_POST['day'][$i] That will show the $i character in $_POST['day'] which is absolutely wrong. You can't run through that correctly in a for-loop. Use foreach instead. foreach ($_POST['day'] as &$value) { echo $value."<br />"; } Code (markup):
check what's going in the session: <?php echo '<pre>'; print_r($_SESSION); ?> Your first foreach error was because the data $_SESSION['day'] is not an array type. Use this code: <?php session_start(); if(isset($_POST[submit])){ $_SESSION['d']= $_POST['d']; } //echo '<pre>'; print_r($_SESSION); //show session data foreach($_SESSION['d'] as $v){ echo $v.'<br>'; } print ' <form action="" method="post"> <input type="checkbox" name="d[]" value="sunday"> Sunday <br> <input type="checkbox" name="d[]" value="monday"> monday <br> <input type="checkbox" name="d[]" value="tuesday"> Tuesday<br> <input type="submit" name="submit"> </form> '; ?> Code (markup):