如何获得一个国家的电话代码通过地理名称检测

How to get the telephone code of a country detected by geonames?

本文关键字:代码 检测 电话 何获得 国家 一个      更新时间:2023-09-26

我使用地理名称的功能来定位我们连接的国家。我的问题是,我必须在开头填写一个电话号码字段和一个电话代码,这个电话代码取决于国家。在我的解决方案中,如果我选择一个国家,代码就可以工作,但是当页面初始收费时(由Geonames国家检测到),显示国家,但我没有得到电话代码。

解决方案是什么?

的例子:

country = FRANCE
telephone code = +33
In my code :
country = FRANCE
telephone code = +93 (First country in the list and not that of FRANCE !!)


    setIndicatif = function(element){
        var indicatif = $(element).find('option:selected').attr('indicatif') ; 
        $('#tel_struct').val('+('+indicatif+') '); 
    }
    setIndicatif($('#country'));
    $('#countrySelect').on('change', function() {
        setIndicatif(this);
    });
  <!-- Country ------>	
    <select id="countrySelect" name="country" onchange="check()">
    	<?php
    		$reponse = $bdd->query('SELECT * FROM pays'); 
    		echo '<OPTION VALUE="" indicatif="">Pays</OPTION>';
    		while ($donnees = $reponse->fetch(PDO::FETCH_ASSOC))
    			{ 
    			echo '<OPTION VALUE="'.$donnees["id_pays"].'" INDICATIF="'.$donnees["code_pays"].'">'.$donnees["pays"].'</OPTION>';
    			} 
    	?>
    </select>
    <!--Telephone-->
    	<input type="tel" name="tel_struct" id="tel_struct" placeholder="Téléphone générique">