JamesKoash Posted August 29, 2013 Share Posted August 29, 2013 Hi there, For those who have been following my recent posts, I'm building a web survey and I've hit quite a few road blocks. I've almost completed the project now, so hopefully this is my last query! I'm having trouble creating session data based on checkbox selections. The code I've just knocked together isn't working at all. In fact, I created the $custresult variable to test if it was working, but it keeps on coming up as an undefined variable. Bear in mind that "custexp" is a checkbox and, as a result, the user may be selecting multiple options, hence why I've created a different if statement for each option. if (isset($_POST['custexp'])) { if($_POST['custexp'] == "1") { $custexp1 = "Yes"; $_SESSION['custexp1'] = $custexp1; } if($_POST['custexp'] == "2") { $custexp2 = "Yes"; $_SESSION['custexp2'] = $custexp2; } if($_POST['custexp'] == "3") { $custexp3 = "Yes"; $_SESSION['custexp3'] = $custexp3; } if($_POST['custexp'] == "4") { $custexp4 = "Yes"; $_SESSION['custexp4'] = $custexp4; } $custresult = "" . $custexp1 . "" . $custexp2 . "" . $custexp3 . "" . $custexp4 . ""; } Thanks Quote Link to comment https://forums.phpfreaks.com/topic/281667-creating-session-data-based-on-checkbox-selections/ Share on other sites More sharing options...
PaulRyan Posted August 29, 2013 Share Posted August 29, 2013 You need to post the form code too. That alone is not enough for anyone to even consider helping. If this is related to what I posted in your other thread, instead of creating a new variable for each option, how about using the actual posted array that is created from the form? I even used print_r on the post to show you the out put, just save that array to the session then use it when needed. Quote Link to comment https://forums.phpfreaks.com/topic/281667-creating-session-data-based-on-checkbox-selections/#findComment-1447328 Share on other sites More sharing options...
JamesKoash Posted August 29, 2013 Author Share Posted August 29, 2013 Hi Paul, Yes, it's related. I adapted the code you wrote for a second set of checkboxes on the same form page $custexp2Array = array('1' => 'We are constantly looking to find new ways to improve the overall customer experience.', '2' => 'There is a team in the business specialising in managing the customer experience.', '3' => 'There is a dedicated position within the business for customer experience management.', '4' => 'Customer experience management is something that receives great consideration at a board/strategic level.'); $custexp2Output = ''; foreach($custexp2Array AS $key => $value) { $checked = (isset($_POST['custexp']) && is_array($_POST['custexp']) && in_array($key, $_POST['custexp'])) ? 'checked' : '' ; $custexp2Output .= '<input type="checkbox" class="checkbox" name="custexp[]" value="'. $key .'" '. $checked .'>'. $value .'<br />'; } I don't know much about arrays or print_r() so I'm quite confused about how I would use the code you've provided and turn that into session data. I think I've spent too many days working on this survey as my head is literally spinning. Haha. Quote Link to comment https://forums.phpfreaks.com/topic/281667-creating-session-data-based-on-checkbox-selections/#findComment-1447332 Share on other sites More sharing options...
PaulRyan Posted August 29, 2013 Share Posted August 29, 2013 If you have 2 different sets of check boxes in a form, you will need to change the name of the check boxes. The set I did was named "custexp[]" for next check boxes, it will need a different name I would imagine. Quote Link to comment https://forums.phpfreaks.com/topic/281667-creating-session-data-based-on-checkbox-selections/#findComment-1447333 Share on other sites More sharing options...
JamesKoash Posted August 29, 2013 Author Share Posted August 29, 2013 I don't think that's the problem. Everything in the page, including the arrays works, even with my use of "custexp[]". I just haven't managed to put together a code which saves the array to the session and my head is spinning from having tried multiple results, to no avail. You said earlier to "save that array to the session then use it when needed" but I don't know how to do that. Bear in mind that if I put the arrays inside if($_SERVER['REQUEST_METHOD'] == 'POST') then the form elements don't display properly, so I had to move the arrays outside of that so I imagine my code needs to look something like this. $custexp2Array = array('1' => 'We are constantly looking to find new ways to improve the overall customer experience.', '2' => 'There is a team in the business specialising in managing the customer experience.', '3' => 'There is a dedicated position within the business for customer experience management.', '4' => 'Customer experience management is something that receives great consideration at a board/strategic level.'); $custexp2Output = ''; foreach($custexp2Array AS $key => $value) { $checked = (isset($_POST['custexp']) && is_array($_POST['custexp']) && in_array($key, $_POST['custexp'])) ? 'checked' : '' ; $custexp2Output .= '<input type="checkbox" class="checkbox" name="custexp[]" value="'. $key .'" '. $checked .'>'. $value .'<br />'; if($_SERVER['REQUEST_METHOD'] == 'POST') { //PUT CODE HERE TO SAVE TO SESSION } } I've commented the section where I think I need to add code. Quote Link to comment https://forums.phpfreaks.com/topic/281667-creating-session-data-based-on-checkbox-selections/#findComment-1447337 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.