Preserving a select option after PHP submit

后端 未结 3 1397
伪装坚强ぢ
伪装坚强ぢ 2021-01-15 06:32

I have a html select box with a list of countries. When I select one, it posts to the PHP method with no problem, but the select box resets to the top. How can I preserve th

相关标签:
3条回答
  • 2021-01-15 07:12

    Add the following to each OPTION. You will need to change this value for each.

    <?php if($_POST['countryCd'] == '*this value*'){ php?>selected<?php } php?>
    
    0 讨论(0)
  • 2021-01-15 07:17

    You may find this question useful:

    Keep form values after submit PHP

    Essentially you can use something like:

    <select name="countryCD">
          <option value="AFG" 
             <?php if(isset($_POST['countryCD']) && $_POST['countryCD'] == 'AFG') 
             echo 'selected= "selected"';
              ?>
          >Afghanistan</option>
    </select>
    
    0 讨论(0)
  • 2021-01-15 07:19

    Try:

    <select name="countryCd"  onChange="submit();return false;">
        <option value="AFG" <?= $_POST['countyCd'] == AFG ? 'selected' : '' ?>>Afghanistan</option>
        <option value="ALA" <?= $_POST['countyCd'] == ALA ? 'selected' : '' ?>>Aland Islands</option>
        <option value="ALB" <?= $_POST['countyCd'] == ALB ? 'selected' : '' ?>>Albania</option>
    </select>
    
    0 讨论(0)
提交回复
热议问题