Home > Back-end >  Set the id of a drop-down within a class
Set the id of a drop-down within a class

Time:02-22

I am trying to use a class to generate a drop down for input fields. The drop down works and is populated, however I am trying to find documentation how to set the id from the variable passed to the class. The reason is that the drop down should be used on various forms but at different positions, so I was hoping to set the id this way.

<?php
class ddUsers {
    public $dfID;
    
    function set_id($dfID) {
        $this->id = $dfID;
    }
    
    function getField() {
    // query code is here, standard select with mysqli //
?>
    <select class ="inline" id ='How to get the ID in here?' name = "UUID" value = "">
    <option> // query results via for loop go here // </option>

Calling the class works:

$field = new ddUsers();
$field->set_id(17);
$field->getField();

CodePudding user response:

I have solved this with simply echo $this->id as the <select> code is within the function:

    <select class ='inline' id ='<?php echo $this->id; ?>' name = 'UUID' value = ''>
<option>Please select a user:</option>"

And the result from "view source" is:

 <select class ='inline' id ='17' name = 'UUID' value = ''>
    <option>Please select a user:</option>"
<option value=2>Username1</option><option value=1>Username2</option></select>
  • Related