提交表单时,如何在下拉菜单中保留所选值?

时间:2018-04-03 08:53:49

标签: php html

如何在提交表单后将所选值保留在select name="betalingsmethode" class="dropdown"中。现在发生的事情是当我提交表单时,下拉刷新并再次显示Visa。但我想保留所选的值,即用户选择的值。

我尝试了与复选框一样的操作:

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

但它不起作用。

<!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>

2 个答案:

答案 0 :(得分:2)

而不是:

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

尝试:

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

等等其他选项。

答案 1 :(得分:1)

你已经选择了#34; 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>