简体   繁体   中英

Select Country and country code?

I need a help in this issue. I am selecting a country in drop down and in the phone text column the country code should come when we select it. This is my code.

<select name="country" id="country">
    <option value="" Selected>Country...</option>
    <option value="India">India</option>
    <option value="USA">USA</option>
    <optgroup label="Other countries">
        <option value="Algeria">Algeria</option>
        <option value="Andorra">Andorra</option>
        <option value="Angola">Angola</option>
    </optgroup>
</select>

<label for="phone"style="width:100%;margin-bottom:20px;">
    <span style="color:#000;">Phone Number</span> <span style="color:#FF0000;">*</span>
    <br>
    <input id="phone" name="phone" required="" type="number">
</label>

And my Client need the country name to be shown to them, so in the option value I have given the country name. Here in my option value there is only the Country names has values not the country code. Should I place the country code some where else? Can anyone help me how to do it?

Try the below code to get Country Code:

<select name="country" id="country">
<option value="" Selected>Country...</option>
<option value="+91">India</option>
<option value="+92">USA</option>
<optgroup label="Other countries">
    <option value="+93">Algeria</option>
    <option value="+91">Andorra</option>
    <option value="+95">Angola</option>
    </optgroup>
</select>
<label for="phone"style="width:100%;margin-bottom:20px;"><span 
style="color:#000;">Phone Number</span> <span style="color:#FF0000;">*
</span><br>
<input id="phone" name="phone" required="" type="number"></label>

jQuery Code:

jQuery("#counrty").change(function(){
   id=$(this).val();
   txt_str="id="+id;
   jQuery.get(get_country_code.php', txt_str,function(result){  
      jQuery("#phone").val(result);
   });
});

In the get_country_code.php code has below code:

<?php 
 include('connect.php');//database connection file

    $id=$_POST['id'];

    $query = $db->query("SELECT country_code FROM countries WHERE country_id =".$id);
    $row = $query->fetch_assoc();
    $counryCode = $row['country_code'];

    echo $counryCode; die;
?>

Hope this help!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM