简体   繁体   English

选择国家和地区代码?

[英]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代码:

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: 在get_country_code.php中的代码具有以下代码:

<?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!!! 希望这个帮助!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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