使用 PHP $_POST 记住选择框中的选项?

发布于 2024-12-22 00:37:12 字数 633 浏览 0 评论 0原文

我有一个可以发布到自身的表单,以便用户可以执行您在购物车中找到的操作。

例如增加数量,选择邮资类型。

我的问题是,每当表单重新加载时,我的表单选择名为“邮资”的元素,它就会忘记选择的内容。

我的所有其他字段都使用以下方法记住它们的值:

<input type="text" name="postcode" value="<?php echo $_POST['postcode']; ?> " />

How do I use the $_POST value toautomatic select the option in the select field that was made by the user?

我试过这个:

<select name="postage" selected="<?php echo $_POST['postage']; ?>" >

还有这个

<select name="postage" value="<?php echo $_POST['postage']; ?>" >

谢谢

I have a form which POSTs to itselft so the user can do things that you would find in a Shopping Cart.

e.g. Increase quantity, select postage type.

My problem is for my form Select element called "postage" whenever the form reloads itself , it forgets what was selected.

All my other fields remember their values using this:

<input type="text" name="postcode" value="<?php echo $_POST['postcode']; ?> " />

How do I use the $_POST value to automatically select the option in the select field that was done by the user?

I tried this:

<select name="postage" selected="<?php echo $_POST['postage']; ?>" >

and this

<select name="postage" value="<?php echo $_POST['postage']; ?>" >

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

半边脸i 2024-12-29 00:37:12

你几乎明白了。您需要在 上设置属性 selected="selected" (技术上您需要的确切形式取决于您的 HTML 文档类型,但这是安全的默认值) > 元素当且仅当 $postage 的值等于该元素的值。所以:

<select name="postage">
<option value="foo" 
    <?php if ($_POST['postage'] == "foo") echo 'selected="selected" '; ?>
 >
</select>

请注意,这违反了 DRY 原则,因为现在字符串 "foo" 出现了两次 在那里,所以它是重构的主要候选者。一个好的方法是将值/文本对保存在数组中,并使用 foreach 对其进行迭代以生成 标记。

You almost got it. You need to set the attribute selected="selected" (the exact form you need technically depends on your HTML doctype, but this is a safe default) on the <option> element if and only if the value of $postage equals the value of the element. So:

<select name="postage">
<option value="foo" 
    <?php if ($_POST['postage'] == "foo") echo 'selected="selected" '; ?>
 >
</select>

Note that this violates the DRY principle because you now have two occurrences of the string "foo" in there, so it's a prime candidate for refactoring. A good approach would be to keep value/text pairs in an array and iterate over it with foreach to produce the <option> tags.

掐死时间 2024-12-29 00:37:12

您需要遍历所有选项。

使用所有下拉选项创建一个数组,循环遍历该数组,然后与 post 中存储的内容进行比较。

例如:

<?php
$aDropd = array("apple","orange","banana");
echo "<select>";
foreach($aDropd as $sOption){
  $sSel = ($sOption == $_POST['postage'])? "Selected='selected'":"";
  echo "<option   $sSel>$sOption</option>";
}
echo "</select>";

You need to foreach through all the options.

Make an array with all the dropdown options, loop through that, and compare with what is stored in post.

E.G.:

<?php
$aDropd = array("apple","orange","banana");
echo "<select>";
foreach($aDropd as $sOption){
  $sSel = ($sOption == $_POST['postage'])? "Selected='selected'":"";
  echo "<option   $sSel>$sOption</option>";
}
echo "</select>";
陌伤浅笑 2024-12-29 00:37:12

不,它根本不起作用......你需要为此设置某种循环。

For Example : foreach($record => $values){
                   if($values == $_POST['postage']){
                       $selected = "selected='selected' ";
                      }else{
                       $selected = "";
                      }
             }

<input name="postage" value="1" <?=$selected?> >

编辑:

                      if($_POST['postage'] == 1){
                           $selected1 = "selected='selected' ";
                          }else if($_POST['postage'] == 2){
                           $selected2 = "selected='selected' ";
                          } and so on..........



    <select name="postage">
     <option value="1" <?=$selected1;?> />
     <option value="2" <?=$selected2;?> />
    </select>

我认为这可能对你有帮助......你可以问我是否还需要任何东西......

谢谢。

no its not working at all..you need to put some kind of loop for that.

For Example : foreach($record => $values){
                   if($values == $_POST['postage']){
                       $selected = "selected='selected' ";
                      }else{
                       $selected = "";
                      }
             }

<input name="postage" value="1" <?=$selected?> >

EDITED:

                      if($_POST['postage'] == 1){
                           $selected1 = "selected='selected' ";
                          }else if($_POST['postage'] == 2){
                           $selected2 = "selected='selected' ";
                          } and so on..........



    <select name="postage">
     <option value="1" <?=$selected1;?> />
     <option value="2" <?=$selected2;?> />
    </select>

I think this may be helpful to you..you can ask me if anything else needed...

Thanks.

椵侞 2024-12-29 00:37:12

selected 的值应该被选中

<select name="postage" value="1" <?php echo (($_POST['postage'] == 1)?'selected="selected"':''); ?> >

The value of selected should be selected

<select name="postage" value="1" <?php echo (($_POST['postage'] == 1)?'selected="selected"':''); ?> >
花想c 2024-12-29 00:37:12

你的html语法是错误的。正确的html写法是这样的:

<select>
<option value ="<?php echo $_POST['postage']; ?>" selected="selected"></option>
</select>

Your html syntax is wrong. The correct way to write the html is like this:

<select>
<option value ="<?php echo $_POST['postage']; ?>" selected="selected"></option>
</select>
瑾兮 2024-12-29 00:37:12

您还可以将其缩短:

<select>
<option value ="<?=$_POST['postage']; ?>" selected="selected"></option>
</select>

You can also make it shorter:

<select>
<option value ="<?=$_POST['postage']; ?>" selected="selected"></option>
</select>
無心 2024-12-29 00:37:12
<select name="foo">
<option value="1" <?php echo strcmp($_POST['foo'],"1")==0?"selected=\"selected\"":"" ?>>option1</option>
<option value="2" <?php echo strcmp($_POST['foo'],"2")==0?"selected=\"selected\"":"" ?>>option2</option>

<select name="foo">
<option value="1" <?php echo strcmp($_POST['foo'],"1")==0?"selected=\"selected\"":"" ?>>option1</option>
<option value="2" <?php echo strcmp($_POST['foo'],"2")==0?"selected=\"selected\"":"" ?>>option2</option>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文