-1

I'm trying to store a form of checkcboxes into a session variable so I can echo it out on a different page. EXAMPLE: User picks first two checkboxes on page 1. Page redirects to page 2 where "apple" and "banana" is printed.

Form:

<form action="" method="post">
    <input name="crate[]" value="apple" type="checkbox">
    <input name="crate[]" value="banana" type="checkbox">
    <input name="crate[]" value="potato" type="checkbox">
    <input type="submit" name="Submit" value="submit">
</form>
  • yes, you are right ... `print_r($_POST['crate']);` then `$_SESSION['checkboxes'] = $_POST['crate'];` – devpro Feb 05 '19 at 13:21
  • 2
    Are you trying to store which checkboxes where checked by the user, or are you trying to store the HTML code of this form? Rather unclear what exactly you mean; and also, what the actual problem is. Please go read [ask], and then edit your question accordingly. – 04FS Feb 05 '19 at 13:23
  • if you just wanna save data from page to page you have many options (with pros and cons): cookies, localStorage, DB (directly or thru sessions). Please be more explicit. Of course I guess you wanna save selected checkbox data. – Unkle Benz Feb 05 '19 at 13:47
  • I've updated my question. I'm guessing it's a very simple solution. – Laurits Brok Feb 06 '19 at 10:08
  • Where's the code to write something to the session? What exactly is not working? – Nico Haase Feb 06 '19 at 10:11
  • I don't have any code for the session. That's what i'm asking for, i'm asuming it isn't more than 4 lines of code. – Laurits Brok Feb 06 '19 at 11:02

1 Answers1

0

After submission of the form, You need to check whether post not empty then assign it in the session variable

$_SESSION['VARIABLE_NAME'] = empty($_POST['crate']) ? [] : $_POST['crate'];

In the second page, also check session variable exist and not empty

if(!empty($_SESSION['VARIABLE_NAME'])){
   print_r($_SESSION['VARIABLE_NAME']);
}

Note:- Don't forget to write session_start(); at top of the page and check this link.

Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
  • It still doesn't print. When i take the print_r line out of the if statement, it simply prints an empty array. I'm not sure why it doesn't pass the values into the array in page 1 – Laurits Brok Feb 06 '19 at 10:11
  • Have you print `$_POST['crate']`?? If it contains value then `$_SESSION['VARIABLE_NAME']` must be set. check this link also. https://stackoverflow.com/questions/3181149/php-print-r-session-doesnt-show-its-content – Ravi Hirani Feb 06 '19 at 12:05
  • If i print $_POST['crate'] on page 2 it gives me an error. If i print_r($_SESSION) it prints an empty array inside of VARIABLE_NAME. It don't think it get's passed into the array correctly on page 1 – Laurits Brok Feb 06 '19 at 12:28
  • I hope you have set the correct form action. check this link1: https://www.guru99.com/php-forms-handling.html and link2 : https://stackoverflow.com/questions/20184670/html-php-form-input-as-array – Ravi Hirani Feb 06 '19 at 12:31