php:提交后保持选择选项

时间:2013-03-29 22:54:45

标签: php mysql forms submit-button

我有一种购物清单,我希望能够从不同的商店添加商品。

目前我有一个表格,我发布商品编号和数量,然后按提交将商品添加到购物清单(下表)。

<form action="<?php echo $current_file.$id; ?>" method="POST">

    Store:  <select>
                    <option value="gross1">Store 1</option>
                    <option value="gross2">Store 2</option>
                    <option value="gross3">Store 3</option>
            </select>

    Art no: <input type="text" size="15" name="number"  /> 

    Quantity: <input type="text" size="3" name="q" />

    <input type="submit" value="Add item" />

</form>

<table>
    <tr>
        <th>
            Art no
        </th>
        <th>
            Article name
        </th>
        <th>
            Price
        </th>
        <th>
            Quantity
        </th>
        <th>
            Total
        </th>
    </tr>

存储项目列表存储在mysql表中,因此我想通过下拉选择列表选择存储(或表格,如果你愿意)。但问题是,如果我选择商品的商店不是商店1,那么在每次按提交后反复选择商店会非常令人沮丧。

我认为我不能使用使用某种GET变量来指定所选商店的脚本,因为我已经使用GET来指定所选的购物清单。是否有其他方法可以让网站在页面刷新后记住所选商店?

提前致谢!

2 个答案:

答案 0 :(得分:0)

在你的php中,你通常会根据POSTed值设置SELECTED。但是您需要在select标签上设置名称。

您是否尝试过搜索谷歌中的任何一个?

答案 1 :(得分:0)

我遇到了类似的问题并且需要快速解决方案。从下拉列表中选择一个值并提交后,页面重新加载会重置下拉列表 - 不保留所选值(即使已发送所选值) $ _GET)。对我来说这个问题纯粹是审美的,因为我希望用户看到他们选择的价值。这就是我所做的:

// Change the parent dropdown selected value to the currently selected value
    $(window).load(function() {

        // Get url attributes which should contain the $_POST or $_GET value and split it. You have to refine this in case you have multiple attributes

        var str=window.location.href; var n=str.split("=");

         // This should be the selected value attribute
         var myAttrVal= n[1];

       // check all options and grab the value (or whatever other unique identifier you're using). Check this value against your url value and when they match, add the selected attribute to that option.
        $('#selectID option').each(function() {

            var selectedOptVal = $(this).attr('value');

            if ( myAttrVal  == selectedOptVal) {

                $(this).attr('selected', 'selected');
            }
        });

    });

我确信有更好的方法可以做到这一点,但我希望它能为您提供有关如何解决问题的建议。

相关问题