douyinyi7766 2014-04-15 01: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 02: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 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 潜在扩散模型的Unet特征提取
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部