Maurice
Maurice

Reputation: 9

How do i keep the selected value in the dropdown menu when the form is submitted?

How do i keep the selected value in select name="betalingsmethode" class="dropdown" after the form is submitted. What happens now is when I submit the form the drop-down refreshes and shows Visa again. but I want to keep the selected value in there, the one the user selected.

I tried the same as I did with the checkboxes:

?php if(isset($_POST['visa'])) echo "selected='selected'"; ?

But it doesn't work.

<!DOCTYPE html>
<html lang="nl">
    <head>
        <meta http-equiv="Content-Type"
              content="text/html;
              charset=UTF-8" />
        <title>Mijn Muziek</title>
    </head>
    <body>
        <!-- shoppingcart starts here -->
        <table border=0 cellpadding=0 cellspacing=0 width=100%>
            <form name="order"
                  action="lab07.php"
                  method="POST">
                <tr>
                    <td>
                        <img src="images/evora.jpg" width="100px" alt="X" />
                    </td>
                </tr>
                <tr>
                    <td>
                        Cesaria Evora "Em Um Concerto" Track:10 Prijs: 9.99
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="hidden" name="albumcode[0]"
                               value="001" />
                        <input type="hidden" name="artiest[0]"
                               value="Cesaria Evora" />
                        <input type="hidden" name="titel[0]"
                               value="Em Um Concerto" />
                        <input type="hidden" name="tracks[0]"
                               value="10" />
                        <input type="hidden" name="prijs[0]"
                               value="9.99" />
                        <input type="hidden" name="genre[0]"
                               value="World" />

                        Aantal: <input type="text" size=2 maxlength=3
                                       name="aantal" value="<?php echo(isset($_POST['aantal'])) ? $_POST['aantal'] : '0'; ?>"
                                       style="background-color:#f8ce6c" />

                        <hr />
                    </td>
                </tr>

                <tr>
                    <td>Korting:<br />
                        <input type="checkbox" name="student" id="student"
                               value="15" <?php
                               if (isset($_POST['student']))
                                   echo
                                   "checked='checked'";
                               ?> />
                        Student 15%<br />
                        <input type="checkbox" name="senior" id="senior"
                               value="10" <?php
                               if (isset($_POST['senior']))
                                   echo
                                   "checked='checked'";
                               ?> />
                        Senior 10%<br />
                        <input type="checkbox" name="klant" id="klant"
                               value="5" <?php
                               if (isset($_POST['klant']))
                                   echo
                                   "checked='checked'";
                               ?> />
                        Klant 5%<br />
                    </td>
                </tr>

                <td> Selecteer een betalingswijze: </td>


                <tr>
                    <td>
                        <select name="betalingsmethode" class="dropdown">
                            <option value="visa"       name="visa">Visa             <?php if (isset($_POST['visa'])) echo "selected='selected'"; ?></option>
                            <option value="mastercard" name="mastercard">Mastercard <?php if (isset($_POST['mastercard'])) echo "selected='selected'"; ?></option>
                            <option value="paypal"     name="paypal">PayPal         <?php if (isset($_POST['paypal'])) echo "selected='selected'"; ?></option>
                            <option value="ideal"      name="ideal">Ideal           <?php if (isset($_POST['ideal'])) echo "selected='selected'"; ?></option>
                        </select>
                        <input type="submit" width="300px" name="submit"
                               value="      Bestellen      " />
                        <hr />
                    </td>
                </tr>
            </form>
        </table>
        <!-- Shoppingcart ends here-->
        <?php
        echo "Aantal is: ";
        if (isset($_POST['aantal'])) {
            echo $_POST['aantal'];
        }

        $korting = 0;
        if (isset($_POST["student"]))
            $korting = $korting + 15;
        if (isset($_POST["senior"]))
            $korting = $korting + 10;
        if (isset($_POST["klant"]))
            $korting = $korting + 5;
        echo "<br>Korting is: $korting %</br>";

        if (isset($_POST["submit"])) {
            switch ($_POST['betalingsmethode']) {
                case "visa" :
                    echo "<p>Betalingswijze: Visa</p>";
                    break;
                case "mastercard" :
                    echo "<p>Betalingswijze: Mastercard</p>";
                    break;
                case "paypal" :
                    echo "<p>Betalingswijze: PayPal</p>";
                    break;
                case "ideal" :
                    echo "<p>Betalingswijze: iDeal</p>";
                    break;
                default:
                    echo "<p>Kies een Betalingsmethode om door te gaan</p>";
            }
        }
        ?>
    </body>
</html>

Upvotes: 0

Views: 121

Answers (2)

LBS
LBS

Reputation: 538

Instead of :

if(isset($_POST['visa'])) echo "selected='selected'";

try :

if($_POST['betalingsmethode'] == 'visa') echo "selected='selected'";

And so on for the others options.

Upvotes: 2

Siva
Siva

Reputation: 1529

You have "selected" attribute inside the option

<select name="betalingsmethode" class="dropdown">
  <option value="visa"       name="visa" <?php 
  if(isset($_POST['betalingsmethode']) && $_POST['betalingsmethode']=='visa') echo "selected='selected'"; ?>>Visa             </option>
  <option value="mastercard" name="mastercard" <?php 
  if(isset($_POST['betalingsmethode']) && $_POST['betalingsmethode']=='mastercard') echo "selected='selected'"; ?>>Mastercard </option>
  <option value="paypal"     name="paypal" <?php 
  if(isset($_POST['betalingsmethode']) && $_POST['betalingsmethode']=='paypal') echo "selected='selected'"; ?>>PayPal         </option>
  <option value="ideal"      name="ideal" <?php 
  if(isset($_POST['betalingsmethode']) && $_POST['betalingsmethode']=='ideal') echo "selected='selected'"; ?>>Ideal           </option>
 </select>

Upvotes: 1

Related Questions