php:提交后保持选择选项

时间:2022-04-17 07:57:03

I have a type of grocery list where I want to able to add items from different stores.

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

Currently I have a form where I post the article number and quantity and then press submit to add the item to the grocery list (the table below).

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

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

The stores item lists are stored in mysql tables so I want to choose the store (or the table if you so will) via a drop down select list. But the problem is, if the store I'm choosing items from isn't store 1, its going to pretty frustrating to select the store over and over again after every time I press submit.

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

I don't think I can use a script that uses some kind of GET variable to specify the selected store, because I already use GET to specify the selected grocery list. Is there another way to get the site to remember the selected store after a page refresh?

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

Thanks in advance!

提前致谢!

2 个解决方案

#1


0  

In your php you would normally set SELECTED against the option based on the POSTed value. But you need to set a name on the select tag.

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

Have you tried searching google for any of this?

您是否尝试过在谷歌搜索这些内容?

#2


0  

I had a similar problem and needed a quick solution.After selecting a value from the dropdown list and submitting, the page reloads which resets the dropdown list as was - without keeping the selected value (even though the selected value was sent ($_GET). For me the issue was purely aesthetic since I wanted the user to see the value they selected. Here is what I did:

我有一个类似的问题,需要一个快速解决方案。从下拉列表中选择一个值并提交后,页面重新加载会重置下拉列表 - 不保留所选值(即使已发送所选值($ _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');
            }
        });

    });

I'm sure there are much better ways of doing this but I hope it gives you ideas on how to solve your problem.

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

#1


0  

In your php you would normally set SELECTED against the option based on the POSTed value. But you need to set a name on the select tag.

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

Have you tried searching google for any of this?

您是否尝试过在谷歌搜索这些内容?

#2


0  

I had a similar problem and needed a quick solution.After selecting a value from the dropdown list and submitting, the page reloads which resets the dropdown list as was - without keeping the selected value (even though the selected value was sent ($_GET). For me the issue was purely aesthetic since I wanted the user to see the value they selected. Here is what I did:

我有一个类似的问题,需要一个快速解决方案。从下拉列表中选择一个值并提交后,页面重新加载会重置下拉列表 - 不保留所选值(即使已发送所选值($ _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');
            }
        });

    });

I'm sure there are much better ways of doing this but I hope it gives you ideas on how to solve your problem.

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