JamesKoash Posted August 29, 2013 Share Posted August 29, 2013 Hi there all, Two questions. Firstly, about keeping radio buttons selected after POST. Managed to get this working on one of my survey pages, but can't seem to get it working on the latest page I'm putting together. I've probably made a minor error but can't seem to work it out. Here's my code. 1 <input type=\"radio\" class=\"radio\" name=\"custexpvalue\" value=\"1\" " . (isset($_POST['custexpvalue']) && $_POST['custexpvalue'] == "1" ? "selected=\"selected\"" : "") ."> 2 <input type=\"radio\" class=\"radio\" name=\"custexpvalue\" value=\"2\" " . (isset($_POST['custexpvalue']) && $_POST['custexpvalue'] == "2" ? "selected=\"selected\"" : "") ."> 3 <input type=\"radio\" class=\"radio\" name=\"custexpvalue\" value=\"3\" " . (isset($_POST['custexpvalue']) && $_POST['custexpvalue'] == "3" ? "selected=\"selected\"" : "") ."> 4 <input type=\"radio\" class=\"radio\" name=\"custexpvalue\" value=\"4\" " . (isset($_POST['custexpvalue']) && $_POST['custexpvalue'] == "4" ? "selected=\"selected\"" : "") ."> 5 <input type=\"radio\" class=\"radio\" name=\"custexpvalue\" value=\"5\" " . (isset($_POST['custexpvalue']) && $_POST['custexpvalue'] == "5" ? "selected=\"selected\"" : "") .">" Secondly, I'm trying to get PHP to store the values of checkboxes when the submit button is hit but because multiple values can be selected I'm not sure how to do this with POST. Here's my code so far: <input type=\"checkbox\" class=\"checkbox\" name=\"outsource\" value=\"1\">Marketing agency<br /> <input type=\"checkbox\" class=\"checkbox\" name=\"outsource\" value=\"2\">PR agency<br /> <input type=\"checkbox\" class=\"checkbox\" name=\"outsource\" value=\"3\">Design studio<br /> <input type=\"checkbox\" class=\"checkbox\" name=\"outsource\" value=\"4\">Marketing consultant<br /> <input type=\"checkbox\" class=\"checkbox\" name=\"outsource\" value=\"5\">Media agency<br /> <input type=\"checkbox\" class=\"checkbox\" name=\"outsource\" value=\"6\">Advertising agency<br /> <input type=\"checkbox\" class=\"checkbox\" name=\"outsource\" value=\"7\">Service or product design agency<br /> <input type=\"checkbox\" class=\"checkbox\" name=\"outsource\" value=\"8\">Customer experience agency<br /> if (isset($_POST['outsource'])) { } Any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/281663-keeping-radio-buttons-selected-after-post-checkboxes/ Share on other sites More sharing options...
Solution PaulRyan Posted August 29, 2013 Solution Share Posted August 29, 2013 Try this: <?PHP if($_SERVER['REQUEST_METHOD'] == 'POST') { echo '<pre>'; print_r($_POST); echo '</pre>'; } //### Put options in array $outsourceArray = array('1' => 'Marketing Agency', '2' => 'PR Agency', '3' => 'Design Studio', '4' => 'Marketing Consultant', '5' => 'Media Agency', '6' => 'Adevertising Agency', '7' => 'Service or Product Design Agency', '8' => 'Customer Experience Agency'); //### Create output $optionOutput = ''; foreach($outsourceArray AS $key => $value) { //### Checks to see if 'outsource' is posted AND whether its an array AND it the value exists i n the posted array $checked = (isset($_POST['outsource']) && is_array($_POST['outsource']) && in_array($key, $_POST['outsource'])) ? 'checked' : '' ; $optionOutput .= '<input type="checkbox" class="checkbox" name="outsource[]" value="'. $key .'" '. $checked .'>'. $value .'<br />'; } ?> <form method="POST"> <?PHP echo $optionOutput;?> <input type="submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/281663-keeping-radio-buttons-selected-after-post-checkboxes/#findComment-1447296 Share on other sites More sharing options...
JamesKoash Posted August 29, 2013 Author Share Posted August 29, 2013 Thank you, that does seem more efficient. Can't believe how much unnecessary code I seem to have wrote. Haha. I'm not too familiar with arrays so just to double-check, would I need to rename $key, $checked or $value to say $key2, $checked2 and $value2 if I were to use this code again for a different set of radio buttons/checkboxes on the same page? Quote Link to comment https://forums.phpfreaks.com/topic/281663-keeping-radio-buttons-selected-after-post-checkboxes/#findComment-1447301 Share on other sites More sharing options...
JamesKoash Posted August 29, 2013 Author Share Posted August 29, 2013 Actually ignore my last post. That was a stupid question. Haha. I've just tested it and $key, $checked and $value can be used over and over again. Now that I've got the checkboxes working properly, I have one more question... how would I go about storing the values from the checkboxes into a variable so I could save it to the session? And would I need to use separate variables for each checkbox option and indicate Boolean true or false whether selected or not? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/281663-keeping-radio-buttons-selected-after-post-checkboxes/#findComment-1447308 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.