【问题标题】:Keeping selected options in php在 php 中保留选定的选项
【发布时间】:2017-05-08 14:49:42
【问题描述】:

我需要制作一个下拉选择表单,以便每次保存时它都保持相同的选项。

所以我想要的是当我从下拉列表中选择一个选项时,当我提交表单并再次返回表单时它保持不变。

https://hastebin.com/zafoxexoja.vbs

我在 google 上都试过了,但似乎没有用。

【问题讨论】:

  • 在会话中存储选定的值
  • 我该怎么做...
  • 您希望每次用户重新加载或返回该页面时都选择已选择的选项吗?
  • 这就是一个包应该持续多长时间的保管箱。我想要它,所以每次我回到它时,它都和我上次设置的一样。
  • 取决于会话时间

标签: php forms select


【解决方案1】:

Session 将为您提供帮助。你应该试试看。它会帮助你。 下面是您的主页,例如 index.php

<?php
session_start();
?>
<select id="editlength" name="editlength" class="form-control">
    <option value="1 Day" <?php if(isset($_SESSION['editlength']) && $_SESSION['editlength'] == '1 Day') { ?> selected <?php } ?>>1 Day</option>
    <option value="2 Days" <?php if(isset($_SESSION['editlength']) && $_SESSION['editlength'] == '2 Days') { ?> selected <?php } ?>>2 Days</option>
    <option value="3 Days" <?php if(isset($_SESSION['editlength']) && $_SESSION['editlength'] == '3 Days') { ?> selected <?php } ?>>3 Days</option>
    <option value="4 Days" <?php if(isset($_SESSION['editlength']) && $_SESSION['editlength'] == '4 Days') { ?> selected <?php } ?>>4 Days</option>
    <option value="5 Days" <?php if(isset($_SESSION['editlength']) && $_SESSION['editlength'] == '5 Days') { ?> selected <?php } ?>>5 Days</option>
    <option value="6 Days" <?php if(isset($_SESSION['editlength']) && $_SESSION['editlength'] == '6 Days') { ?> selected <?php } ?>>6 Days</option>
    <option value="10 Days" <?php if(isset($_SESSION['editlength']) && $_SESSION['editlength'] == '10 Days') { ?> selected <?php } ?>>10 Days</option>
    <option value="1 Week" <?php if(isset($_SESSION['editlength']) && $_SESSION['editlength'] == '1 Week') { ?> selected <?php } ?>>1 Week</option>
    <option value="2 Weeks" <?php if(isset($_SESSION['editlength']) &&$_SESSION['editlength'] == '2 Weeks') { ?> selected <?php } ?>>2 Weeks</option>
    <option value="3 Weeks" <?php if(isset($_SESSION['editlength']) && $_SESSION['editlength'] == '3 Weeks') { ?> selected <?php } ?>>3 Weeks</option>
    <option value="1 Month" <?php if(isset($_SESSION['editlength']) && $_SESSION['editlength'] == '1 Month') { ?> selected <?php } ?>>1 Month</option>
    <option value="2 Months" <?php if(isset($_SESSION['editlength']) && $_SESSION['editlength'] == '2 Months') { ?> selected <?php } ?>>2 Months</option>
    <option value="3 Months" <?php if(isset($_SESSION['editlength']) && $_SESSION['editlength'] == '3 Months') { ?> selected <?php } ?>>3 Months</option>
    <option value="4 Months" <?php if(isset($_SESSION['editlength']) && $_SESSION['editlength'] == '4 Months') { ?> selected <?php } ?>>4 Months</option>
    <option value="5 Months" <?php if(isset($_SESSION['editlength']) && $_SESSION['editlength'] == '5 Months') { ?> selected <?php } ?>>5 Months</option> 
</select>

用于 ajax 调用的 Javscript 将其放在您的主页上:

<script>
$(document).ready(function(){
    $('#editlength').on('change', function() {
      var val = jQuery("#editlength").val();
      $.ajax({
            url: "store_editlength.php",
            data: { editlength: val },
            type: 'POST',
            success: function (data) {
                // here no need of response
            }
        });

    });
});
</script>

store_editlength.php

<?php
session_start();
if (isset($_POST['editlength']) && $_POST['editlength'] != '') {
 $_SESSION['editlength'] = $_POST['editlength'];
}
?>

【讨论】:

  • 唯一的问题是我有多个计划/包,这意味着它们的选择都相同:/
【解决方案2】:

只需在您想要默认选择的选项中传递selected="selected",例如:

<option value="5 Days" selected="selected">5 Days</option>

Working Fiddle

【讨论】:

  • 我希望它设置为与用户设置的相同,这样每次我重新加载页面时它都不会改变。它适用于可购买的包裹,该选项是包裹的持续时间。我想要它,所以当我更改日期时,它会显示最后选择的日期,因为每次我去设置它们时都会重置选项。
  • selected="selected" 放入一个条件中,您必须根据用户给定值测试选项值
  • 对不起,这对我来说毫无意义。
  • &lt;option value="9 Months" &lt;?php if($editlength == '9 Months') { ?&gt; selected &lt;?php } ?&gt;&gt;9 Months&lt;/option&gt; 这行得通吗?只为其余的人这样做?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多