문제

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?

도움이 되었습니까?

해결책

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++;
  }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top