Page 1 of 1

Is it possible to select a value in a dynamic dropdown list.

Posted: Fri Jul 03, 2015 3:50 pm
by hybris
Hi subject was too short for the tricky part:

/../dropdown list after it has been populated.

The reason I'm asking is I have a form with several dropdownlists containing the sites users so you can select different users to do various stuff..

Like
Who will take a sample -> <dropdownlist users>
bla bla
Who shall drive the truck -><dropdownlist users>
and so on.

Problem is I have an update form function that reads the values from DB and fill out the form with existing info when choosing update.

The other problem is im lazy so I made a function to populate the lists with option value = user ID and shown text = user name.

so when updating the form the function is populating the lists but I can't select the right name depending on the value (userID) stored in database.

I know I can fix this by skipping the function and do the db call manually at each place where i use the dropdown menu (see below) but since Im lazy I would prefer to use my function and then set the selected name after the list is populated if that is even possible.

Code for populate function (and yes I know I do not have to save it as a string before echo but this is v1 and I still mess up alot with " or ' :)

Code: Select all

private function populateUserList(){
     require("../LoginSystem/Includes/DBConnect.php");
      if ($stmt = $mysqli->prepare("SELECT userID, userName FROM Members order by userName asc")){
       $stmt->execute();
       $stmt->bind_result($uID, $uName);
        while($row=$stmt->fetch()){
           echo $uID.$uName."*";
          $temp="<option value=".$uID.">".$uName."</option>";
          echo $temp;
       }
     }
I use it like this

Code: Select all

<select name="blabla" id ="blablaID" size="1" <?php if($FormUpdate){echo 'disabled';} ?>><?php             
               $this->populateUserList(); ?>
              </select>
I have the value (userID) from db stored in $blablaUSER (different strings for the various jobs the users shall perform in the form)

I can solve it by skipping using the function and do like this

Code: Select all

<option <?php echo ($GoodsPickupWarehouseStatus==$WarehouseCode_Stop_ShortDateText[$Language])?'selected':'' ?> value="<?php echo $WarehouseCode_Stop_ShortDate ?>"><?php echo $WarehouseCode_Stop_ShortDateText[$Language] ?></option>
                <option <?php echo ($GoodsPickupWarehouseStatus==$WarehouseCode_Stop_ForeignObjectText[$Language])?'selected':'' ?> value="<?php echo $WarehouseCode_Stop_ForeignObject ?>"><?php echo $WarehouseCode_Stop_ForeignObjectText[$Language] ?></option>

but since Im trying to be better at php I wonder if there are other ways to do this so I can still use my function and then do the selected after the list is populated...

Any suggestions?

Thanks in advance.

Re: Is it possible to select a value in a dynamic dropdown l

Posted: Fri Jul 03, 2015 4:18 pm
by hybris
Err.. I just realized I can send a $var to the function and solve it that way...

But still is it possible to do the select after the list is already populated?

Re: Is it possible to select a value in a dynamic dropdown l

Posted: Sun Jul 05, 2015 3:21 pm
by Christopher

Code: Select all

          echo "<option value=" . $uID . ($theID==$uID?' selected="selected"':'') . ">" . $uName . "</option>";

Re: Is it possible to select a value in a dynamic dropdown l

Posted: Sun Jul 05, 2015 4:45 pm
by hybris
^^ Thanks, You guys rock!