简体   繁体   中英

Get option value from select menu with variable

There is the code in index.php:

<select name="select" id="select_invoice">
    <option value="">--- Please select ---</option>
    <?php echo $obj->printOption(); ?>
</select>

The class is follow:

class SelectOption {
    private $query_names;
    private $selected_index = '';

    public function SelectOption($query_names=''){
        if($query_names) $this->query_names = $query_names;
        if($query_names) $this->query_names = $query_names;
    }

    public function printOption(){
        echo $this->getOption();
    }

    public function get_diretory($query_names) {
        print '<select name="files">';
        $directory = dir($query_names) or die($php_errormsg);
        print '<option> --Please select-- </option>';
        while (false !== ($f = $directory->read())) {
            if (is_file($directory->path.'/'.$f)) {
                print '<option> ' . $f . '</option>';
            }
        }
        $directory->close();
    }
} 

$query_names = 'file_directory';
$obj = new SelectOption($query_names);
$obj->get_diretory($query_names);

How can I get the option value from this select menu and find the file in the directory and read it?

You're calling getOption() in your class, but there isn't such a function in your class definition.

public function printOption(){
    echo $this->getOption(); // where is this function in your class??
}

Since you're actually making use of the printOption() in your html page, which is using getOption() , you're not getting anything out of it. In fact, it should be throwing an error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM