0

I am trying to use my form to override my PHP variable $country_code to either CA, US or EU based on the option you select from the drop down form (country_show). I am using Cloudflare on my site so

$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];

works for me. I have been learning PHP and HTML for only a few weeks now, and know nothing of javascript. The script below was a basic hide div function which was found online. If anybody has any suggestions on how to use the dropdown to override the $country_code variable, please let me know. My knowledge in HTML and PHP is very limited.

    <?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>
5
  • 1
    Seems like you're missing a closing </div> - Add one, see if it makes it kick in. Commented Aug 30, 2014 at 14:33
  • it's in my code, i actually just forgot to copy it onto the page. thanks, fixing now. Commented Aug 30, 2014 at 14:34
  • Plus, <option>United States</option> should be <option value="USA">United States</option> etc. Commented Aug 30, 2014 at 14:35
  • Am I able to use these values added to <option> to change the values of $country_code ?? Commented Aug 30, 2014 at 14:37
  • Consult my answer below. It worked for me. Commented Aug 30, 2014 at 14:56

2 Answers 2

1

my example that should direct you in right direction:

I don't have cloud flare access now to test this if it works ok but it should. And also you must note that this only will work one time. When user reloads page counry code will be set back to provided by cloud flare or default. If it should be persistent then you should use session to save that information and read it on subsequent page loads.

<?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>
Sign up to request clarification or add additional context in comments.

2 Comments

+1 - Let's see if OP accepts it, I don't see why not. I haven't gotten any feedback from OP yet.
This works AMAZING! thank you so much. At first I couldn't get it to work because you had changed $country_code to $countryCode, but since I changed it in my include files everything works perfect! Thank you so much again, you are a savior!!!!
1

Try the following.

Your country values did not have a value set for them.

Plus, your button was outside the form tags.

You also had 2x id="country_show" I removed the one in the form tag; however, having it in the form tag worked also.

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

hey Fred, sorry I was out and just got back to the computer. going to check it now. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.