How to get the Country Code plugin from (intl-tel-input) after submitting the form?

I installed the plugin and its display of countries is correct and works fine, but when I submit a form that has an input of type = "tel", the form does not send the country code, I don’t know how it works, but I read all the instructions in https: // github.com/jackocnr/intl-tel-input , but I did not understand how to get the full number + country code so that I could embed them in the MySQL database.

I am so new and just confused how to make this work fine. Sorry if the question is not that complicated, but I really don't know how this works.

I have jQuery code: -

$("#telephone").intlTelInput({ 
    initialCountry: "sd",
    separateDialCode: true 
});
+4
source share
3

:

HTML:

<form action="post">
  <div class="select">
    <input type="hidden" id="phone2" name="phone"/>
    <input type="tel" id="phone" name="phone-full">
  </div>
</form>

JAVASCRIPT:

$("form").submit(function() {
    $("#phone2").val($("#phone").intlTelInput("getNumber"));
});
+1

// intlTelInput.js,

$currentCountry = 'us';
    $.ajax({
        url: 'http://ip-api.com/json/',
        async: false,
        success:function(data){
            $currentCountry = data.countryCode.toLowerCase();
            // console.log(data.countryCode.toLowerCase());
        }
    })

    $selected = $('.country_code').attr('country_iso_code2') != undefined ? $('.country_code').attr('country_iso_code2') : $currentCountry;
    // console.log($selected);
    var pluginName = "intlTelInpu'enter code here't", defaults = {
        preferredCountries: [ $selected ],
        // united states and united kingdom
        initialDialCode: true,'enter code here'
        americaMode: false,
        onlyCountries: []
    };
0

Searching a bit in the link you posted, I found a good example of what you are asking

Taking inspiration from an example, here is the code

HTML

<form action="post">
  <div class="select">
    <input type="hidden" id="country" name="country"/>
    <input type="tel" id="phone" name="phone">
  </div>
</form>

Js

var input = $('#phone');
var country = $('#country');
var iti = intlTelInput(input.get(0))

// listen to the telephone input for changes
input.on('countrychange', function(e) {
  // change the hidden input value to the selected country code
  country.val(iti.getSelectedCountryData().iso2);
});
0
source

Source: https://habr.com/ru/post/1668108/


All Articles