这是我正在使用的一段代码。我必须根据用户选择的国家/地区自动显示国家/地区代码。
我有一个包含国家/地区和相应国家/地区代码的数据库。
现在我正在展示 +91 我怎么能把逻辑做到这一点呢?
这是代码 -
<div class="phone-number-verify-widget">
<p class="pnaw-verification-error"></p>
<div class="pnaw-step1">
<div class="phone-number-input-widget" id="phone-number-input-widget-d04854e1">
<label for="phone_country">Choose a country:</label>
<div class="select">
<select id="phone_country" name="phone_country">
<option value="AF" data-prefix="93">Afghanistan</option>
<option value="AD" data-prefix="376">....
<option value="ZW" data-prefix="263">Zimbabwe</option>
</select>
</div>
<label for="phone_number">Add a phone number:</label>
<div class="pniw-number-container clearfix">
**<div class="pniw-number-prefix">+91</div>**
<input type="text" class="pniw-number" id="phone_number">
</div>
<input type="hidden" data-role="phone_number" name="phone" value="91">
<input type="hidden" name="user_id" value="19935817">
</div>
请指导。
答案 0 :(得分:0)
由于我不知道你的代码库,我大致描述了我会做什么:
答案 1 :(得分:0)
如果您有国家/地区母版,请创建一个国家/地区数组或从您的数据库中获取。 您将获得阵列中的国家/地区的结果。 然后让你的国家选择充满活力。
答案 2 :(得分:0)
<?php
$countries = array(array('Country'=>'India','CountryCode'=>'IN'),array('Country'=>'US','CountryCode'=>'US'),array('Country'=>'United Kingdom','CountryCode'=>'UK'));
$selcountry = 'US';
if(count($countries) > 0)
{
for($i=0;$i<count($countries);$i++)
{
$longname[$i] = $countries[$i]['Country'];
$shortname[$i] = $countries[$i]['CountryCode'];
}
}
$countrycombo = '';
$countrycombo = '<select>';
for($i=0;$i<count($shortname);$i++)
{
if (trim($selcountry)==trim($shortname[$i]))
{
$countrycombo .= "<option value='".$shortname[$i]."' selected>".$longname[$i]." (".$shortname[$i].") </option>";
}
else
{
$countrycombo .= "<option value='".$shortname[$i]."'>".$longname[$i]." (".$shortname[$i].") </option>";
}
}
$countrycombo .= '</select>';
echo $countrycombo;
?>
答案 3 :(得分:0)
1) <script src="jquery-1.11.1.js"></script>
//--------------------------------------------------download jquery
<div class="phone-number-verify-widget">
<p class="pnaw-verification-error"></p>
<div class="pnaw-step1">
<div class="phone-number-input-widget" id="phone-number-input-widget-d04854e1">
<label for="phone_country">Choose a country:</label>
<div class="select">
<select id="phone_country" name="phone_country">
<option value="AF" data-prefix="93">Afghanistan</option>
<option value="AD" data-prefix="376">....
<option value="ZW" data-prefix="263">Zimbabwe</option>
</select>
</div>
<label for="phone_number">Add a phone number:</label>
<div class="pniw-number-container clearfix">
**<div class="pniw-number-prefix">+91</div>**
<input type="text" class="pniw-number" id="phone_number">
</div>
<input type="hidden" data-role="phone_number" name="phone" value="91">
<input type="hidden" name="user_id" value="19935817">
</div>
<script>
$('#phone_country').change(function(){
var country = $('#phone_country').val();
$.ajax({
url: "result.php",//place your url of php file
type: "Post",
data: {country : country},// this post value used to match the data from database
success: function (data) {
$('.pniw-number-prefix').html(data);
},
error: function(xhr, status, err) {
alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
alert("responseText: " + xhr.responseText);
}
});
});
</script>
2) Result.php file as i used in my code
<?php
$cc = $_POST['country'];
//using the $cc feth the code from database
// your query...............
// just to show the value i have assuming $isd value as 41
$isd = '41';
echo $isd;
exit;
?>