jquery combobox +set selected value to a combobox array

352 views Asked by At

I was asked to work on a php application that has been created by someone else.

In the page there is a table that display rows of data with each row having the same cells names e.i: responsibles[]

I would like to set the combobox responsibles[] of the row that is being generated to a value i.e: "name2" how is it possible do to that. The code below generates the combobox for a row.

 <?php

                        $responsibleCmb = '<td style="width:180px; min-width: 180px;" <select id="responsibles[]"
                    name="responsibles[]" style="width:180px; height:16px;">
>                   <option>-</option>';
>                   // Get all the employees that have a role id of 1 (Auditor) and display an option for each one
>                   if (isset ( $data ['employees'] )) {
>                       while ( $rowCmb = $data ['employees']->fetch_assoc () ) {
>                           // The value of the option is their employee id
>     $responsibleCmb.=          "<option value='". $rowCmb['FirstLast']. "'";
> 
>     $responsibleCmb.=                          ">$rowCmb[FirstLast]</option>";
>                       }
>                       }
>     $responsibleCmb.=          "</select></td>";
>           
>                       
>                   while ( $row = $this->data ['filtered']->fetch_assoc () ) {
>                       ?>
>                           <tr>
>                       <td style="font-size: 7.5pt; width: 60px; min-width: 60px;"><a
>                           href='<?=ROOT?>demerit/view?id=<?=$row['id']?>'>
>                                       <?=$row['id']?>
>                                   </a> <input type="hidden" value="<?=$row['id']?>" name="ids[]">
>                       </td>
>                       <td style="font-size: 8pt; width: 100px; min-width: 100px;">
>                                   <?php $description = explode(';', $row['model_main_code']);?>
>                                       <?=$description[0]?>
>                                       <hr
>                               style="margin: 0 auto; border-top-style: dashed; border-top-color: black;">
>                                       <?=$description[1]?>
>                               </td>
>                       <td><textarea class="expand" name="actions[]"><?=$row['action']?></textarea>
>                       </td>
>                            <!--               //********************************************                           --> <!--               // PRINT COMBOBOX FOR RESPONSIBLE SO THAT USER CAN'T TYPE IN A WRONG NAME.                           --> <!--
>               //******************************************** -->
>                   <?php 
>                       echo($responsibleCmb);
>                   ?>
> 
> 
> Then when i enter the td of the comobobx
> 
> <?php echo($responsibleCmb); ?>
> 
> 
> <script> $("responsibles[]").val('Doina Angheluta'); </script>

Included is the output of my code. http://jsfiddle.net/v2quyxp3/

Thanks for your help.

also i tried this method instead of storing the combobox code in the $responsibleCmb variable but then it would fill up only the combobox on the first row.

                            echo '<td><select id="responsibles[]"
                                name="responsibles[]" style="width:180px;">
                                <option>-</option>';
                                // Get all the employees that have a role id of 1 (Auditor) and display an option for each one
                        if (isset ( $data ['employees'] )) {
                            while ( $rowCmb = $data ['employees']->fetch_assoc () ) {
                                // The value of the option is their employee id
                                echo "<option value='". $rowCmb['FirstLast']. "'";
                                if ($row ['responsible'] ==  $rowCmb['FirstLast']) {
                                echo ' selected=\'true\'';
                                }
                                echo ">$rowCmb[FirstLast]</option>";
                            }
                        }
                        echo "</select></td>";
0

There are 0 answers