Outnedwaste
Outnedwaste

Reputation: 33

How to select option and post into session

I need to display something based on the input of a select option. The option gets written away in the session but it always gives the last value of the choices.
Hopefully someone can help me with this.
First page;

<form class="form-horizontal" action="postchange.php" method="POST">
        <fieldset>
        <div class="form-group">
            <label class="col-md-4 control-label" for="Naam">Naam</label>  
                <div class="col-md-4">
                    <select id="gebruiker" name="gebruiker" placheholder="kies een gebruiker" class="form-control" required>
                        <?php

                                $conn = dbconnect();
                        // selecteer alle users waar stats == 0 en sorteert deze oplopend op ID
                                $query = "SELECT * FROM users WHERE Status LIKE 0 ORDER BY id DESC"; 
                                $result = mysqli_query($conn, $query);
                            while($row1 = mysqli_fetch_array($result)):;

                        ?>
                    <option>
                        <?php
                        // Geeft het ID, voornaam + achternaam weer
                        echo "$row1[0] $row1[2] $row1[3]";
                        $test=$row1[0];
                        endwhile;
                        ?>
                    </option>
                    </select>

              </div>

        </div>
                    <div class="form-group">
              <label class="col-md-4 control-label" for="singlebutton"></label>
                  <div class="col-md-4">
                      <button id="volgende" name="volgende" class="btn btn-success">volgende</button>
                  </div>
        </div>
        </fieldset>
    </form>       
                            <?php 
                        $_SESSION['gebruiker']=$test;
                    ?> 

2th page (postchange.php):
The session does echo some value here but not the selected. As mentioned previously it always gives the last value of the select option.

<form class="form-horizontal" action="#" method="post">
            <fieldset>              
<div class="form-group">
              <label class="col-md-4 control-label" for="soort">Configuratie item</label>
              <div class="col-md-4">
                        <select id="gebruiker" name="gebruiker" placheholder="kies een gebruiker" class="form-control" required>
                            <?php

                                    $conn = dbconnect();
                                    $query = "SELECT * FROM config WHERE gebruiker='".$_SESSION['gebruiker']."';"; 
                                    $result = mysqli_query($conn, $query);
                                while($row1 = mysqli_fetch_array($result)):;
                            ?>
                        <option>
                            <?php
                            echo "$row1[0] $row1[2] $row1[3]";?>
                        </option>
                            <?php 
                                endwhile;
                            ?>
                        </select>
              </div>
            </div>

            </fieldset>
        </form>
<?php 
                                    var_dump($gebruiker, $aanvraag, $_SESSION['userid']);

        ?>

Upvotes: 2

Views: 616

Answers (1)

Nirav Madariya
Nirav Madariya

Reputation: 1508

Just replace line

$query = "SELECT * FROM config WHERE gebruiker='".$_SESSION['gebruiker']."';";

with,

$query = "SELECT * FROM config WHERE gebruiker='".$_POST['gebruiker']."'";

This should work, as it sends selected item from options.

Just a small Sample :

<form action="" method="POST">
    <select name="bac">
        <option name="1">1</option>
        <option name="2">2</option>
        <option name="3">3</option>
        <option name="4">4</option>
        <option name="5">5</option>
        <option name="6">6</option>
    </select>
    <input type="submit" />
</form>

<?php
    if(isset($_POST['bac']))
        echo $_POST['bac'];
?>

Also, remove the below lines from your first file,

<?php 
   $_SESSION['gebruiker']=$test;
?> 

Updated

replace :

while($row1 = mysqli_fetch_array($result)):;

with :

$row1 = mysqli_fetch_array($result);

and remove,

<?php 
    endwhile;
?>

Above code changes will fetch single row once.

Upvotes: 1

Related Questions