使PHP函数彼此协同工作

时间:2016-05-12 10:37:11

标签: php html function

我正在构建一个网页,使用户可以查看他们选择的某个产品的报告。步骤如下:

User chooses interface(Beta或Cloud) - > User Chooses Product(产品基于所选的界面。) - >信息会相应显示。

显示信息的页面如下:

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>

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

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

功能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. 我只想在选择了一个界面时显示产品选择器。(我的想法是从功能1返回$interface_bool,测试是否为真,如果是,则显示下一节。)

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

  3. 我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

当你重新加载页面并将选定的界面发送回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
}