You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过PHP获取International Telephone Input插件的国家码值

获取International Telephone Input国家码并存入PHP变量

没问题,我来帮你搞定这个需求!要把插件的国家码传到PHP后端,咱们分两步走:前端获取国家码并随表单提交,后端PHP接收并处理。

第一步:修改前端代码,添加隐藏字段存储国家码

首先,你原来的代码里重复引入了intlTelInput.js和intlTelInput.min.js,只需要保留一个就行(推荐min版)。然后我们添加一个隐藏的input字段,用来存国家码,这样表单提交时就能把它一起传给后端。

修改后的HTML和JS代码如下:

<div class="group">
  <input type="tel" id="phone" name="phone" placeholder="Phone No." style="color: #916409;">
  <!-- 新增隐藏字段,用于存储国家码 -->
  <input type="hidden" id="country_code" name="country_code">
  <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.min.js"></script>
<script>
  // 初始化插件
  $("#phone").intlTelInput({
    // 可选:添加默认国家自动检测配置,也可以手动指定initialCountry为具体国家码,比如"cn"
    initialCountry: "auto",
    geoIpLookup: function(callback) {
      $.get('https://ipinfo.io', function() {}, "jsonp").always(function(resp) {
        var countryCode = (resp && resp.country) ? resp.country : "us";
        callback(countryCode);
      });
    }
  });

  // 监听国家选择变化,实时更新隐藏字段的值
  $("#phone").on("countrychange", function() {
    var countryData = $(this).intlTelInput("getSelectedCountryData");
    // 将国家拨号码存入隐藏字段(比如+86、+1这类)
    $("#country_code").val(countryData.dialCode);
    // 如果需要国家ISO两位代码(比如CN、US),就替换成下面这行
    // $("#country_code").val(countryData.iso2);
  });

  // 页面初始化时也设置一次初始国家的代码
  $(document).ready(function() {
    var countryData = $("#phone").intlTelInput("getSelectedCountryData");
    $("#country_code").val(countryData.dialCode);
  });
</script>

第二步:PHP后端接收并存储国家码

当用户提交表单后,你可以通过$_POST(如果是POST提交)或者$_GET获取隐藏字段的值,存入PHP变量后再写入数据库。

示例代码如下:

<?php
// 假设表单是POST提交,记得给表单添加method="post"属性
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // 获取国家码(拨号码,比如+86)
    $country_code = $_POST['country_code'];
    // 获取用户输入的手机号
    $phone_number = $_POST['phone'];

    // 重要:一定要做数据验证和过滤,防止安全问题
    $country_code = htmlspecialchars(trim($country_code));
    $phone_number = htmlspecialchars(trim($phone_number));

    // 接下来就可以将$country_code存入数据库了,这里用预处理语句示例更安全
    /*
    $servername = "localhost";
    $username = "你的数据库用户名";
    $password = "你的数据库密码";
    $dbname = "你的数据库名";

    // 创建连接
    $conn = new mysqli($servername, $username, $password, $dbname);
    // 检查连接
    if ($conn->connect_error) {
        die("连接失败: " . $conn->connect_error);
    }

    // 使用预处理语句防止SQL注入
    $stmt = $conn->prepare("INSERT INTO 你的表名 (country_code, phone) VALUES (?, ?)");
    $stmt->bind_param("ss", $country_code, $phone_number);

    if ($stmt->execute()) {
        echo "数据存入成功";
    } else {
        echo "存入失败: " . $stmt->error;
    }

    $stmt->close();
    $conn->close();
    */
}
?>

注意事项

  • 确保你的表单带有method="post"属性,POST提交比GET更安全,也更适合传输表单数据。
  • 数据验证和过滤是必须的,上面示例里的htmlspecialchars和trim是基础操作,用预处理语句能有效防止SQL注入。
  • 如果需要的是国家ISO两位代码而非拨号码,只需要修改JS里的countryData.dialCode为countryData.iso2即可。

内容的提问来源于stack exchange,提问作者user7671954

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:08:53