user4960371
user4960371

Reputation:

Passing value of dynamically filled dropdownlist to PHP file

I need to pass the option value to php/lend.php to make a query with that value. I tried to post the value and echo them in the lend.php file but it says 'undefined index'. Can someone please look through my code to see if I'm doing something wrong?

Dropdownlist file:

<form action="php/lend.php" method="post" accept-charset="utf-8">
    <div class="input-group">
        <select id="selectapparaat" name="selectapparaat" class="selectpicker form-control" data-live-search="true" onchange="grabDevice(this.value)">
            <option value="" disabled selected>Naam apparaat</option>
            <?php
                $queryapparaten = "SELECT apparaatnaam FROM apparaten ";
                $db = mysqli_query($db,$queryapparaten);
                while ( $d=mysqli_fetch_assoc($db)) {
                    echo "<option value='".$d['apparaatnaam']."'>".$d['apparaatnaam']."</option>";
                }
            ?>
        </select>
        <span class="input-group-btn">
            <button class="btn btn-default" disabled type="button" data-toggle="modal" data-target=""><span class="glyphicon glyphicon-plus" ></span></button>
        </span>
    </div>
    <button type="submit" class="btn btn-lg btn-primary btn-block" name="uitlenen" style="width: 100%;">Uitlenen</button>
</form>

php/lend.php:

<?php
    require 'database.php';

    if (isset($_POST['uitlenen'])) {
       $a = $_POST['apparaatnaam'];
       $b = $_POST['clientnaam'];
       $c = $_POST['organisatienaam'];

       echo $a;
       echo "</br>";
       echo $b;
       echo "</br>";
       echo $c;
   }
?>

Upvotes: 2

Views: 256

Answers (1)

kscherrer
kscherrer

Reputation: 5766

In your form is no input named apparaatnaam, clientnaam and organisatienaam. Therefore those 3 post variables you want to read will not be defined.

What you do have in your form is selectapparaat, so you could read the selected option with $_POST['selectapparaat']

edit: I can also recommend using the apparaatid for the value of the options, not their names. (Only if you use the selected value for further DB-querys.) This will make the further queries easier and does not require apparaatnaam to be unique.

$queryapparaten = "SELECT id, apparaatnaam FROM apparaten ";
$db = mysqli_query($db,$queryapparaten);
while ( $d=mysqli_fetch_assoc($db)) {
    echo "<option value='".$d['id']."'>".$d['apparaatnaam']."</option>";
}

edit2: oh and one more thing, it seems you are overwriting your db-connection-variable with the result of the query. further db queries on this page will therefore not work. try this instead

$queryapparaten = "SELECT id, apparaatnaam FROM apparaten ";
$result = mysqli_query($db,$queryapparaten);
while ( $d=mysqli_fetch_assoc($result)) {

Upvotes: 1

Related Questions