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

时间:2014-08-30 14:30:11

标签: javascript php html

我正在尝试使用我的表单根据您从下拉表单(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>

2 个答案:

答案 0 :(得分:1)

请尝试以下操作。

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

另外,您的按钮位于表单标记之外。

你还有2x 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>

答案 1 :(得分:1)

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

我现在没有云端访问权限来测试它,如果它运行正常但它应该。而且你必须注意,这只会工作一次。当用户重新加载页面时,区域代码将被设置回由云眩光或默认提供。如果它应该是持久的,那么你应该使用session保存该信息并在后续页面加载时读取它。

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