douyinyi7766 2014-04-15 09:56
浏览 74
已采纳

PHP在重新加载时选择选择选项

I have this form with 2 selext boxed. with all menu items from the webpage loaded from the database and all the current 3 languages.

When i change the option from any of the select boxes it should reload with the value from the selectbxo and should make a query that gets the content in a textare according to menu item and text.

this works partially, the content changes perfect, but he does not remember the chosen option and i cant find out why.

Here is the form, the objects you see are from seperate files who gather the menu items and language items.

<form style="border: 1px solid black; " name="form" method="post" action="">
Content selecteren om te wijzigen:
    <table>
        <td>
            <tr>
                <td width="78">Menu item:</td>
                <td><select name="Menu" onchange="this.form.submit()">' . $this->MenuItems->render() . '</select></td>
            </tr><br>

            <tr>
                <td width="78">Taal:</td>
                <td><select name="Language" onchange="this.form.submit()">' . $this->LanguageItems->render() . '</select></td>
            </tr>

            <tr>
                <td>Menu Item:</td>
                <td width="78"><input name="MenuItemTextBox" type="text" id="MenuItemTextBox" value="' . $_POST['Menu'] . '"></td><br>
            </tr>
            <tr>
                <td>content</td>
                <td><TEXTAREA style="" Name="content" id="NewContent" ROWS=10 COLS=50>' . $this->GetContent->render() . '</TEXTAREA></td>   
            </tr>

            <tr>
                <td><input type="submit" name="Submit" value="Wijzigen"></td>
            </tr>

        </td>
    </table>
</form>

and this is the code where i get the menu items from databse, and checks wether the post is the same as the menu item its looping through. If the post (the post created from the calue of the select box) and menu item are the same a "selected" html element gets pasted inside the html option.

class MenuItems extends content_part{

private $menuLabels = array();
private $menuIDs = array();

 public function __construct(DatabaseHandler $dbh)
{
  if(!isset($_POST['Language'])){
    $_POST['Language'] = 1;
  }

  $SQL = "SELECT menulanguage.Label AS Label, menulanguage.Title AS Title, menulanguage.MenuID AS MenuID FROM Menu JOIN menulanguage ON Menu.MenuID = menulanguage.MenuID WHERE LanguageID = 1;";
  $resultDB = $dbh->executeQuery($SQL);

  if(is_object($resultDB)){
    while($row = $resultDB->fetch(PDO::FETCH_ASSOC)){
      $this->menuLabels[] = $row["Label"];
      $this->menuIDs[] = $row["MenuID"];
    }
  }

  else{echo 'Could not retrieve menu items from database'; }
}

public function render(){
  $Selected = '';
  $result = '';
  $i = 0;

  foreach($this->menuLabels as $menuLabel){
    if (isset($_POST['Menu'])){
      if($menuLabel == $_POST['Menu']){ $Selected = 'selected'; }
      else {}
    }
      $result .= '<option value="' . $this->menuIDs[$i] . '"' . $Selected . '>' . $menuLabel . '</option>';
      $i++;
  }

  return $result;
}}

I have seen a few the same questions but i really couldnt get my answer.

This is the option after it gets populated with menu items,

<option value="1">Home</option>
<option value="2">Contactus</option>

As you can see, the "selected" html isnt populated it, meaning he cant find the POST value from the form, why is this?

  • 写回答

1条回答 默认 最新

  • drl37530 2014-04-15 10:16
    关注

    You'll need to change:

    if($menuLabel == $_POST['Menu']){ $Selected = 'selected'; }

    to be $menuId instead of $menuLabel because the render function is outputting the values of the ID instead of the label to the options.

    Something like this should do what you need:

      foreach($this->menuIDs as $menuID){
        if (isset($_POST['Menu'])){
          if($menuID== $_POST['Menu']){ $Selected = 'selected'; }
          else {}
        }
          $result .= '<option value="' . $this->menuIDs[$i] . '"' . $Selected . '>' . $menuLabel . '</option>';
          $i++;
      }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题