使用下拉选择器更改/覆盖PHP变量


Use a dropdown selector to change/override a PHP variable

我正试图使用我的表单,根据您从下拉表单(country_show)中选择的选项,将我的PHP变量$country_code覆盖到CA、US或EU。我在我的网站上使用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>

我的例子应该会引导您朝着正确的方向前进:

我现在没有云火炬访问来测试它是否正常,但它应该正常。而且你必须注意,这只会起作用一次。当用户重新加载页面时,counry代码将被设置回由cloudflare或默认值提供。如果它应该是持久的,那么您应该使用会话来保存该信息,并在随后的页面加载中读取它。

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

尝试以下操作。

您所在国家/地区的价值观没有为其设定价值。

另外,你的按钮在表单标签之外。

你还有2个id="country_show",我删除了表单标签中的那个;然而,将它放在form标签中也同样有效。

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