这是我正在使用的代码。我必须根据用户选择的国家自动显示国家代码。
我有一个数据库,其中有两个,国家和相应的国家代码。
现在在当前显示+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>
请导游。
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;
?>
由于我不知道你的代码库,我大致描述了我将做什么:
- 让jQuery
- 为更改事件#phone_country编写一个事件监听器
- 做一个ajax调用一个简单的phpscript查询数据库提供我想要的数据
- 用检索到的数据更新字段
创建国家数组或从数据库中获取(如果您有国家管理器)。您将在数组中获得国家的结果。然后使您的国家选择动态
<?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;
?>