0

您好,我需要帮助来获取 PHP 变量中国家代码的值。我正在使用一个名为“国际电话输入”的 Jquery 插件。

这是我的输入和脚本。

   <div class="group">
    <!-- <input type="text" name="phone" > -->
    <!-- Changes -->
    <input type="tel" id="phone" name="phone" placeholder="Phone No." 
style="color: #916409;"><span class="highlight"></span><span class="bar">
</span>
<label style="color: #916409;"></label>
</div>
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script> 
<script src="src/js/intlTelInput.js"></script>
<script src="src/js/intlTelInput.min.js"></script>
<script>
 $("#phone").intlTelInput();
</script>

我想要国家代码值,以便我可以将其发送到数据库。任何帮助,将不胜感激

4

1 回答 1

0

这记录在自述文件中:

$("#phone").on("countrychange", function(e, countryData) {
  // do something with countryData
});

countrychange事件触发时,countryData将包含一个带有国家数据的对象:

{
    "name": "United States",
    "iso2": "us",
    "dialCode": "1",
    "priority": 0,
    "areaCodes": null
}

这一切都发生在客户端,在浏览器中。为了将其放入 Php 变量中,您需要在 HTML 表单或 ajax 请求中提交它。例如,使用jQuery 中的$.post您可以执行以下操作:

$("#phone").on("countrychange", function(e, countryData) {
  $.post("getCountryCode.php", {countryCode: countryData.iso2});
});

在 getCountryCode.php 中,您可以使用$_POST 超级全局获取值:

$_POST['countryCode']; // "us"
于 2018-01-25T16:09:34.357 回答