开发者

Call php file to return an array, and load it into a combo box

开发者 https://www.devze.com 2023-03-16 08:21 出处:网络
I have my index.php. I want to call 开发者_高级运维a function in load_data.php which would return an array. I then want to load that array into a combox box on index.php. Can anyone help me get starte

I have my index.php. I want to call 开发者_高级运维a function in load_data.php which would return an array. I then want to load that array into a combox box on index.php. Can anyone help me get started on this? I'm brand new to php and trying to get my head around it.


You can use something similar to this:

load_data.php

function get_data() {
    // May be you want to load data from DB
    // This is just a hint
    return array('key1' => 'Value 1', 'key2' => 'Value 2');
}

And in your main file:

<select name="myselect">
    <?php
    include 'load_data.php';
    $data = get_data();
    foreach($data as $key => $value) {
        echo '<option value="'.$key.'">'.$value.'</option>';
    }
    ?>
</select>


If you want to do it without reloading the page, you'll need to look into AJAX. If you want to do this at the time you request the page, you can include the page and call any functions out of it.

0

精彩评论

暂无评论...
验证码 换一张
取 消