DropDown菜单不会选择选项

时间:2015-08-07 07:52:49

标签: php sql drop-down-menu pfbc

这里是获取项目的代码。但不知何故它不起作用。当我点击'option2'选项时,它会回到'option1'。

include("pfbc3.1-php5.3/PFBC/Form.php");
include("pfbc3.1-php5.3/header.php");


if (isset($_POST['project'])) {
    $project = $_POST['project'];
} else {
    $project = 0;
}
if (isset($_POST['new_project'])) {
    $new_project = $_POST['new_project'];
} else {
    $new_project = "";
}
if (isset($_POST['edit'])) {
    $edit = $_POST['edit'];
} else {
    $edit = 0;
}
if (isset($_POST['submit_reset']) && $_POST['submit_reset'] == "Reset") {
    $project = 0;
    $projects = get_projects();
    $options = [];
    $options[0] = "please select";
    foreach ($projects as $project) {
        $options[$project[0]] = $project[1];
    }

    $form = new Form("project-form");
    $form->configure([
        "prevent" => ["bootstrap", "jQuery"],
        "action"  => "right.php?target=management/project"
    ]);
    $form->addElement(new Element\HTML('<legend>Projects</legend>'));
    $form->addElement(new Element\Select("Existing projects:", "project", $options,
        ["onchange" => "this.form.submit()", "value" => $project]));
    $form->addElement(new Element\Textbox("New Project:", "New_Project",
        ["placeholder" => "new project", "shortDesc" => "Add new project", "value" => get_project($project)]));

    $form->addElement(new Element\Button);

get_projects函数它有以下代码:

function get_projects(){
    global $db;
    $sql="SELECT ID, project, description FROM [Typematrix].[dbo].[Project] ";
    $arr = $db->GetArray($sql);
    //print_r($arr);
    return $arr;

有什么问题?

1 个答案:

答案 0 :(得分:0)

这是您填写$project

的方法
if (isset($_POST['project'])) {
    $project = $_POST['project'];
} else {
    $project = 0;
}

但稍后你会在代码中执行:

foreach ($projects as $project) {
    $options[$project[0]] = $project[1];
}

这将覆盖循环中的$project值。

如果重命名foreach()中的值,则不应再覆盖。

例如:

foreach ($projects as $p) {
    $options[$p[0]] = $p[1];
}