-2

I use the function of Geonames to locate countries where we connect. And my problem is that I have to fill in a phone number field at the beginning with a telephone code, this telephone code depends of the country. In my solution, the code works if I select a country, but when the page is initially charged (detected by Geonames country), the country is displayed but I do not get the phone code.

What is the solution?

Example:

country = FRANCE
telephone code = +33
In my code :
country = FRANCE
telephone code = +93 (First country in the list and not that of FRANCE !!)
sqluser
  • 5,502
  • 7
  • 36
  • 50

1 Answers1

0
    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">