Page 1 of 1

Making sticky a Multipal select box

Posted: Fri Jul 10, 2009 10:57 am
by Addos
I’m looking for a few pointers as I need to try and make the code below sticky (if that’s the correct term). When I select a few options from the option list and I submit the page if there are any other conditional statements that don’t get passed then this part looses it’s selection. I’m not sure how to do this with the ‘multiple’ feature here and would appreciate some advise.
Thanks

Code: Select all

<?PHP 
echo "<select size=\"5\" multiple=\"multiple\" name=\"count_idpk[]\" id=\"count_idpk\">\n
      ";
      while($dbRow = mysql_fetch_array($GetAreas)){ 
      echo "<option value='"
      . $dbRow["count_idpk"]
      . "'>" 
      . $dbRow["count_name"]  
      ."</option>\n";   
      } 
      echo "</select>\n";?>

Re: Making sticky a Multipal select box

Posted: Fri Jul 10, 2009 11:27 am
by Christopher
You need to see if each value is set in the $_POST array for that form variable. If it is then add ' selected="selected"' to that <option>.

Re: Making sticky a Multipal select box

Posted: Fri Jul 10, 2009 11:45 am
by Addos
Ok thanks for that. I was working with the following below this last while and although I’m getting somewhere I’m having a few problems. I need this to insert multiple selected options and have to use count_idpk[] but the [] on the end is stopping the script working.

When running this code below I’m deliberately making other conditional statements stop it submitting so that I can see how this script below behaves.

If I remove this array count_idpk[] to count_idpk and then load the page and I try to submit the form the echo $message_location is not called however if I select 'Select location(s)' then the error message gets called. If I try after that to select multiple options and try to submit the page it only holds one value in my selection and not the 3 or so that I selected.

Am I close even though these errors are there?
Thanks

Code: Select all

<?PHP echo "<select size=\"5\" multiple=\"multiple\" name='count_idpk[]' >\n
      <option value='%'>Select location(s)</option>"; 
        while($dbRow = mysql_fetch_array($GetAreas)){
        echo "<option value='"
        . $dbRow["count_idpk"]  
        . "'"
        . (isset($_POST['count_idpk']) ? $_POST['count_idpk'] == $dbRow["count_idpk"] ? "selected='selected'" : "" : "") 
        . "'>" 
        . $dbRow["count_name"]
        ."</option>\n"; 
        } echo "</select>\n"; 
 
        if (isset($message_location) && !empty($message_location)) {
           echo $message_location; } ?>
Head of page has

Code: Select all

//Location Conditional
  if ($_POST['count_idpk'] =='%') {
      $message_location = 'You haven\'t selected a location';
    }

Re: Making sticky a Multipal select box

Posted: Fri Jul 10, 2009 4:16 pm
by Christopher
I think you need to check $_POST['count_idpk'][$dbRow["count_idpk"]] because that field is returning an array. You should try var_dump() or print_r() to see what is actually in $_POST.

Re: Making sticky a Multipal select box

Posted: Sun Jul 12, 2009 1:28 pm
by Addos
Thanks for this. I managed to run those var_dump() or print_r() and although things were working ok it did however give me some pointers as to what I was doing wrong. In the end I managed to use the following. I’m sure there are many more better ways to do this but it’s my attempt and thankfully it’s working ok in test.

This is my solution after a lot of searching. I didn’t realise that multiple select boxes were so difficult to add a sticky to!
Thanks

Code: Select all

<form method="post" name="form1" action="">
   <?PHP 
$output = "<select size=\"5\" multiple=\"multiple\" name='count_idpk[]' >\n >"; 
        while($dbRow = mysql_fetch_array($GetLoc)){
        
$output .= "<option value='"
        . $dbRow["count_idpk"]  
        . "'" ;
        
    if(isset($_POST['count_idpk'])){
    
     $aLocations = $_POST['count_idpk'];
  
     for($i=0; $i < count($aLocations); $i++)
    {
      $results = ($aLocations[$i]);
 
     $output.= ($results == $dbRow["count_idpk"] ? "selected='selected'" : "") ;
    }
  } 
$output.= "'>" 
        . $dbRow["count_name"]
        ."</option>\n"; 
        } "</select>\n"; 
 
           echo $output ?> 
         
  <input type="submit" value="Insert record">  
   <input type="hidden" name="MM_insert" value="form1">
 </form>