Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php:提交后保留选择选项_Php_Mysql_Forms_Submit Button - Fatal编程技术网

php:提交后保留选择选项

php:提交后保留选择选项,php,mysql,forms,submit-button,Php,Mysql,Forms,Submit Button,我有一种杂货清单,我希望能够添加来自不同商店的物品 目前,我有一个表单,我发布商品编号和数量,然后按submit将商品添加到杂货店列表(下表) 在php中,您通常会根据发布的值将选项设置为SELECTED。但是您需要在select标记上设置一个名称 您是否尝试过在google中搜索这些内容?我遇到了类似的问题,需要快速解决。从下拉列表中选择一个值并提交后,页面将重新加载,并按原样重置下拉列表,而不保留所选值(即使已发送所选值($\u GET).对我来说,这个问题纯粹是审美问题,因为我想让用户看到

我有一种杂货清单,我希望能够添加来自不同商店的物品

目前,我有一个表单,我发布商品编号和数量,然后按submit将商品添加到杂货店列表(下表)


在php中,您通常会根据发布的值将选项设置为SELECTED。但是您需要在select标记上设置一个名称


您是否尝试过在google中搜索这些内容?

我遇到了类似的问题,需要快速解决。从下拉列表中选择一个值并提交后,页面将重新加载,并按原样重置下拉列表,而不保留所选值(即使已发送所选值($\u 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');
            }
        });

    });

我相信有很多更好的方法可以做到这一点,但我希望它能为您提供解决问题的方法。

您可以有多个GET变量,即
example.com?var1=hello&var2=bye
是的,我已经在谷歌上搜索了几天,我发现的唯一一件事是:如果我正确理解代码,脚本必须使用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');
            }
        });

    });