简体   繁体   English

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

[英]PHP select the selection option when reloading

I have this form with 2 selext boxed. 我有2张selext装箱的表格。 with all menu items from the webpage loaded from the database and all the current 3 languages. 并从数据库中加载网页中的所有菜单项以及所有当前3种语言。

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. 当我从任何选择框中更改选项时,它应重新加载selectbxo中的值,并应进行查询,以根据菜单项和文本获取textare中的内容。

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. 如果帖子(通过选择框的标题创建的帖子)和菜单项相同,则将“选定”的html元素粘贴到html选项中。

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? 如您所见,“选定”的html没有填充,这意味着他无法从表单中找到POST值,这是为什么呢?

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. 成为$menuId而不是$menuLabel因为render函数将输出ID的值而不是标签的标签。

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++;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM