使用下拉选择改变/重写PHP变量

问题描述 投票:0回答:2

我想用我的形式重写我的PHP变量$ COUNTRY_CODE要么CA,美国或欧盟根据您从下拉形式(country_show)选择该选项。我在我的网站使用的CloudFlare使

$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];

我的作品。我一直在学习PHP和HTML的只有几个星期,现在,什么都不知道的JavaScript。下面的脚本是这是在网上找到了一个基本的隐藏DIV功能。如果有人有关于如何使用下拉列表来覆盖$ COUNTRY_CODE变量的任何建议,请让我知道。我在HTML和PHP知识是非常有限的。

    <?php
         $country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];
          ?>

          <script>
            function showhide()
             {
                   var div = document.getElementById("country_show");
            if (div.style.display !== "none") {
                div.style.display = "none";
            }
            else {
                div.style.display = "block";
            }
             }
          </script>

          <div id="country_show">
            <?php

                    if ($country_code == "CA") {
                    $message = "We have set your delivery country to Canada. If this is incorrect, please select your country";
                    }

                    elseif ($country_code == "US") {
                    $message = "We have set your delivery country to United States. If this is incorrect, please select your country";
                    }

                    else {
                    $message = "We have set your shipping charges to International. If this is incorrect, please select your country";
                    }
                    echo $message
           ?>


            <form id="country_show">
              <select>
            <option value="">Choose location</option>
            <option>Canada</option>
            <option>United States</option>
            <option>International</option>
              </select>
             </form>

          <button id="button" onclick="showhide()">Hide</button>
</div>
javascript php html
2个回答
1
投票

我的例子应该指导您正确的方向:

我没有云耀斑访问现在来测试这一点,如果它的工作原理确定,但它应该。还必须注意,这只是将工作一次。当使用者重新载入页面counry代码将被设置回云耀斑或默认提供的。如果它应该是持久的,那么你应该使用会话保存该信息,并阅读它在随后的页面加载。

<?php

$allowedCountryCodes = array(
    'CA',
    'US',
    'INTERNATIONAL',
);

// load country code based on IP
if(empty($_SERVER["HTTP_CF_IPCOUNTRY"]) === true) {
    // cloud flare do not provided country code, set default
    $countryCode = 'INTERNATIONAL';
} else {
    $countryCode = $_SERVER["HTTP_CF_IPCOUNTRY"];
}

// set country code based on user input
if($_POST && empty($_POST['country_code']) === false && in_array($_POST['country_code'], $allowedCountryCodes)) {
    $countryCode = $_POST['country_code'];
}

?>

<div id="country_select">
    <?php

    if ($countryCode == "CA") {
        $message = "We have set your delivery country to Canada. If this is incorrect, please select your country";
    } elseif ($countryCode == "US") {
        $message = "We have set your delivery country to United States. If this is incorrect, please select your country";
    } else {
        $message = "We have set your shipping charges to International. If this is incorrect, please select your country";
    }
    echo $message
    ?>

    <form id="country_select_form" method="post">
        <select id="country_select" name="country_code">
            <option value="">Choose location</option>
            <option value="CA">Canada</option>
            <option value="US">United States</option>
            <option value="INTERNATIONAL">International</option>
        </select>
    </form>

    <button id="country_select_hide_button">Hide</button>
</div>

<script>

    // init modular pattern
    var app = {};

    app.selectCountryCode = function() {
        var $countrySelectForm = document.getElementById("country_select_form");
        var $countrySelect = document.getElementById('country_select');

        $countrySelect.addEventListener('change', function() {
            $countrySelectForm.submit();
        });
    };

    app.countrySelectHide = function() {
        var $countrySelectHideButton = document.getElementById('country_select_hide_button');
        $countrySelectHideButton.addEventListener('click', function() {
            var $countrySelect = document.getElementById("country_select");

            if ($countrySelect.style.display !== "none") {
                $countrySelect.style.display = "none";
            }
            else {
                $countrySelect.style.display = "block";
            }
        });
    };

    app.selectCountryCode();
    app.countrySelectHide();

</script>

1
投票

请尝试以下操作。

您的国家价值观并没有为它们设置的值。

此外,您的按钮是表单标签之外。

你也有2个id="country_show"我删除了一个表单标签;然而,有它的形式标记也工作。

<?php

         $country_code = $_REQUEST['choice'];
          ?>

          <script>
            function showhide()
             {
                   var div = document.getElementById("country_show");
            if (div.style.display !== "none") {
                div.style.display = "none";
            }
            else {
                div.style.display = "block";
            }
             }
          </script>

          <div id="country_show">
            <?php

                    if ($country_code == "CA") {
                    $message = "We have set your delivery country to Canada. If this is incorrect, please select your country";
                    }

                    elseif ($country_code == "US") {
                    $message = "We have set your delivery country to United States. If this is incorrect, please select your country";
                    }

                    else {
                    $message = "We have set your shipping charges to International. If this is incorrect, please select your country";
                    }
                    echo $message;
           ?>


            <form action="" method="post">
              <select name = "choice">
            <option value="">Choose location</option>
            <option value="CA">Canada</option>
            <option value="US">United States</option>
            <option>International</option>
              </select>

          <button id="button" onclick="showhide()">Hide</button>
             </form>


</div>
© www.soinside.com 2019 - 2024. All rights reserved.