【问题标题】:Getting country code through form in php . intl-tel通过 php 中的表单获取国家代码。国际电话
【发布时间】:2018-01-25 15:32:34
【问题描述】:

您好,我需要帮助来获取 PHP 变量中国家代码的值。 我正在使用一个名为“International Telephone Input”的 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>

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

【问题讨论】:

    标签: javascript php jquery database intl-tel-input


    【解决方案1】:

    这记录在README:

    $("#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 super global 获取值:

    $_POST['countryCode']; // "us"
    

    【讨论】:

      猜你喜欢
      • 2017-04-09
      • 2015-06-27
      • 1970-01-01
      • 2018-10-16
      • 1970-01-01
      • 2015-01-19
      • 2021-08-24
      • 2020-12-28
      • 2018-02-22
      相关资源
      最近更新 更多