user1511766
user1511766

Reputation: 125

Get output when submit dropdown form

I'm making a dropdown menu with information out of my database. It has to get different brands in my dropdown and when selecting a brand and submit search it will show information about that brand. It gets the brands correct from the database but after submit it only shows my last brand. Can somebody help me out... Here is my dropdown menu and search (submit) code:

<div class="bandwielkolom">
    <form action="index.php?lang=nl&p=<?php echo $_GET['p']; ?>#keuze" method="post">
        <table class="bandentabel">
        <tr>
            <th colspan="2">Op merk zoeken<a name="band"></a></th>
        </tr>
        <tr>
            <td>Merk:</td>
            <td>
                <select name="band_merk">
                <option value="0">- Merk -</option>
<?php
    $wielen = $wielclass->getMerkenWielen($website);
    foreach($wielen as $wiel)
    {
        echo "\t\t\t\t\t\t\t\t\t\t\t<option value=\"".$wiel->merk_code."\"";
        if(isset($_GET['search']) && $_GET['search'] == "band" && isset($_GET['wiel']) && $_GET['wiel'] == $wiel->merk_code || isset($_POST['band_submit']) && $_POST['band_merk'] == $wiel->merk_code) { echo " selected=\"selected\""; }
        echo ">".$wiel->merk_naam."</option>\n";
    }
?>
                </select>
            </td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input type="submit" name="band_submit" value="Zoek"/></td>
        </tr>
        </table>
    </form>
</div>  

This is my class:

Class:

<?php
  class wielen extends connect {
    private $wielenlijst;
    public function getMerkenWielen($database) {
        $sql = "SELECT * FROM ".$database."_wielen ORDER BY merk_nummer";
        try {
            $stmt = $this->db->prepare($sql);
            $stmt->execute();
            $this->wielenlijst = $stmt->fetchAll(PDO::FETCH_OBJ);
            $stmt->closeCursor();
            return $this->wielenlijst;
        }
        catch (Exception $e) {
            die ($e->getMessage());
        }
    }
    public function __construct($dbo) {
        parent::__construct($dbo);
    }
  }
?>

This only show the last brand after submit:

<?php
  if(isset($_POST['band_submit']))
    echo $wiel->merk_naam;
?>

Can somebody help me with this because I cant get it to work

Maybe a fresh look at it will help

thnx for any help

UPDATE:

I got this peace of code now and I will explain what is happening and what I want:

<div class="bandwielkolom">
                        <form action="index.php?lang=nl&p=<?php echo $_GET['p']; ?>#keuze" method="post">
                            <table class="bandentabel">
                                <tr>
                                    <th colspan="2">Op merk zoeken<a name="band"></a></th>
                                </tr>
                                <tr>
                                    <td>Merk:</td>
                                    <td>
                                        <select name="band_merk">
                                            <option value="0">- Merk -</option>
<?php
    $wielen = $wielclass->getMerkenWielen($website);
    $get_wiel = isset($_GET['wiel']) ? $_GET['wiel'] : '';
    $post_wiel = isset($_POST['band_merk']) ? $_POST['band_merk'] : '';
    foreach($wielen as $wiel) {
      if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $selected = $post_wiel['wiel'] === $wiel->merk_code ? ' selected="selected"' : '' ;
      }
      else {
        $selected = $get_wiel['wiel'] === $wiel->merk_code ? ' selected="selected"' : '' ;
      }
      echo '\t\t\t\t\t\t\t\t\t\t\t<option value="' . $wiel->merk_code . '"' . $selected . '>' . $wiel->merk_naam . '</option>';
    }
?>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td>&nbsp;</td>
                                    <td><input type="submit" name="band_submit" value="Zoek"/></td>
                                </tr>
                            </table>
                        </form>
                    </div>
                                                            <?php
if(isset($_POST['band_submit']) && $_POST['band_merk'] == $wiel->merk_code)
    echo $wiel->merk_naam;
?>

Precess code:

if(isset($_POST['band_submit']) && $_POST['band_merk'] == $wiel->merk_code)
    echo $wiel->merk_naam;

I get a selectbox with four options out of my database so that is working correct. These options has data in it like a picture and text. When I select one and press submit he has to show the information of the selected option and that should stay selected.

What is happening now is that when I select a option and press submit nothing happens... The button is working but I dont get any output of the selected option and it wont stay selected.

Doing something wrong but I cant figure out what

Any help is welcome:)

Upvotes: 0

Views: 409

Answers (1)

Adrian
Adrian

Reputation: 1046

You need to use your for loop again to list them all.

foreach($wielen as $wiel)
{
    echo $wiel->merk_naam;
}

Upvotes: 1

Related Questions