0

Hi there I need help in getting the value of the country code in a PHP variable. I am using a Jquery plugin named "International Telephone Input".

Here is my input and scripts.

   <div class="group">
    <!-- <input type="text" name="phone" > -->
    <!-- Changes -->
    <input type="tel" id="phone" name="phone" placeholder="Phone No." 
style="color: #916409;"><span class="highlight"></span><span class="bar">
</span>
<label style="color: #916409;"></label>
</div>
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script> 
<script src="src/js/intlTelInput.js"></script>
<script src="src/js/intlTelInput.min.js"></script>
<script>
 $("#phone").intlTelInput();
</script>

I want the country code value so that I can send it to database. Any help would be appreciated

Yusuf Kandemir
  • 810
  • 7
  • 16

1 Answers1

0

This is documented on the README:

$("#phone").on("countrychange", function(e, countryData) {
  // do something with countryData
});

When the countrychange event fires, countryData will contain an object with the country data:

{
    "name": "United States",
    "iso2": "us",
    "dialCode": "1",
    "priority": 0,
    "areaCodes": null
}

This all happens client side, in the browser. In order to get it into a Php variable you will need to submit it within an HTML form or an ajax request. For example, using $.post from jQuery you could do something like:

$("#phone").on("countrychange", function(e, countryData) {
  $.post("getCountryCode.php", {countryCode: countryData.iso2});
});

In getCountryCode.php you can get the value using the $_POST super global:

$_POST['countryCode']; // "us"