Jump to content

Keeping radio buttons selected after POST / checkboxes


JamesKoash
Go to solution Solved by PaulRyan,

Recommended Posts

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.

Link to comment
Share on other sites

  • Solution

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>
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.