php - 使 PHP 函数相互协作

标签 php html function

我正在构建一个网页,使用户能够查看有关他们选择的特定产品的报告。步骤如下:

用户选择界面(测试版或云)-> 用户选择产品(产品基于所选界面。)-> 相应地显示信息。

显示信息的页面写法如下:

HTML

<div class="roundiv" >

        <h4>*Interface: </h4>
        <select class="form-control " style='width:250px;' onclick="INSERT SOMETHING HERE" >
            <option value="-1"  >Select Interface </option>
            <?php
                config::selectInterface($cao);
            ?>

        </select>

        <h4>
            *Product:
        </h4>
        <select class="form-control" style="width:250px;">
            <option value="-1" >Select Product</option>
            <?php
                config::selectIntProduct($cao);
            ?>
        </select>
        <button class="btn btn-success " value="Submit" onclick="" style="margin-left: auto; display:block;"  > Submit </button>
        <!-------------INFO WILL BE DISPLAYED HERE------------->
    </div>

我已经开始在配置类中编写函数,并希望继续使用这种方法。

这是我在配置类中写的函数:

函数 1

public static function selectInterface(CGenDb $cao) {

    $sel_interface_options = new CGenRS("select type_desc, type_id from lu_type where type_status = 't' and type_group = 'cloud' and type_sub_group = 'db'", $cao);
    $sel_interface_options->first();
    if ($sel_interface_options->rowcount()) {
        while (!$sel_interface_options->eof()) {
            echo "<option value='" . $sel_interface_options->valueof('type_id') . "'>" . $sel_interface_options->valueof('type_desc') . "</option>";
            $sel_interface_options->next();
        }
    }
    $interface_bool = True;
    return $interface_bool;
}

selectIntProducts 类看起来像这样:

功能 2

public static function selectIntProduct(CGendb $cao,$selected_interface){

    $selected_interface=$cao_interface= "The interface ID"

    $sel_product_options = new CGenRS("select prod_name, prod_id from lu_products where prod_active = 't'", $cao_interface);
    $sel_product_options->first();

            if ($sel_product_options->rowcount()) {
                while (!$sel_product_options->eof()) {
                    echo "<option value='" . $sel_product_options->valueof('prod_id') . "'>" . $sel_product_options->valueof('prod_name') . "</option>";
                    $sel_product_options->next();
                }
            }
}

这就是我需要帮助的地方:

  1. 我只想在选择/选择接口(interface)时显示产品选择器。(我的想法是从函数 1 返回 $interface_bool,测试它是否为真是,显示下一节。)

  2. 函数 1 中选择的值应该是函数 2 中的 $selected_interface

我怎样才能做到这一点?

最佳答案

你只能用php来实现,当你重新加载页面并将选择的界面发送回php时。

更好更方便的方法是使用 javascript 来填充选择,为此我建议使用 knockout.js .它有很好的文档可供遵循。

对于 php,我建议这样做:

<div class="roundiv" >
    <h4>*Interface: </h4>
    <select class="form-control " style='width:250px;' onchange="document.location.href='https://url-to-same-page&interface=' + this.options[this.selectedIndex].value" >
        <option value="-1"  >Select Interface </option>
        <?php
            config::selectInterface($cao, $_GET['interface']);
        ?>

    </select>

<?php if (!empty($_GET['interface'])): ?>
    <h4>
        *Product:
    </h4>
    <select class="form-control" style="width:250px;">
        <option value="-1" >Select Product</option>
        <?php
            config::selectIntProduct($cao, $_GET['interface']);
        ?>
    </select>
    <button class="btn btn-success " value="Submit" onclick="" style="margin-left: auto; display:block;"  > Submit </button>
    <!-------------INFO WILL BE DISPLAYED HERE------------->

<?php endif; ?>
</div>

同时更新 selectInterface 以包含选定的界面:

public static function selectInterface(CGenDb $cao, $selected_interface) {
    // ... code
    while (!$sel_interface_options->eof()) {
        echo "<option value='" . $sel_interface_options->valueof('type_id') . "' "
             . ($selected_interface == $sel_interface_options->valueof('type_id') ? 'selected="selected"' : '')
             . ">" . $sel_interface_options->valueof('type_desc') . "</option>";
        $sel_interface_options->next();
    }
    // .. code continues
}

关于php - 使 PHP 函数相互协作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37184438/

相关文章:

php - 如何更漂亮地编写这个循环?

php - 如何从刚刚执行的MYSQL插入中获取主ID?

mysql - Codeigniter 在 View 中显示信息?

php - HTML5分页

function - 试图在 Lua 中随机选择

php - 我如何找到在 wordpress 中定义 div 的位置

php - Wordpress PHP 函数_n() 只翻译单数文本

php - 如何在 FOR 循环中多次执行准备好的 SQL 语句?

php - 从附加的 html 内容生成 pdf

javascript - Href 在 Chrome 中不工作,在 IE 中工作