从jquery发送到codeigniter控制器的国际电话是否发送为空?

时间:2019-02-28 10:46:01

标签: javascript php jquery codeigniter

我的HTML代码

<form  class="form" id="d_personal_form" action="<?php echo base_url('registerdoctor');?>"  method="post">
    <input type="tel" id="d_phone">
    <button class="btn" type="submit" id="d_send_personal_data">send</button>
</form>

javascript代码

 <script>
var input = document.querySelector("#d_phone");
var output = document.querySelector("#d_phone_error_msg");
var iti=intlTelInput(input, {
    allowDropdown: true,
    initialCountry: "auto",
    nationalMode: true,
    preferredCountries: ['ye'],
    utilsScript: "assets/js/utils.js?"
    });
$(document).ready(function(){
    $("#d_send_personal_data").click(function(){
        $.ajax({
            type:'POST',
            url:"<?php echo base_url();?>doctor_c/register_doctor",
            data:{d_phone:iti.getNumber()},
            success: function(data)
            {
                alert(iti.getNumber());           
                alert('true data ...');
            },
            error: function(){
                alert('something went wrong...');
            } 
        });
    });
});
</script>

我的代码点火器控制器代码

$phone= $this->security->xss_clean($this->input->post('d_phone'));echo $phone;

控制器未显示任何值,非常感谢您的帮助

2 个答案:

答案 0 :(得分:0)

我通过将视图添加为HTML代码来找到解决方案

<input type="hidden" id="d_mobile" name="d_mobile" value="">

并将JavaScript编辑为

<script>
    var input = document.querySelector("#d_phone");
    var output = document.querySelector("#d_phone_error_msg");
    var iti=intlTelInput(input, {
        allowDropdown: true,
        initialCountry: "auto",
        nationalMode: true,
        preferredCountries: ['ye'],
        utilsScript: "assets/js/utils.js?"
        });
    $(document).ready(function(){
        $("#d_send_personal_data").click(function(){
            var mobile =iti.getNumber();
            $('#d_mobile').val(mobile);                
        });
    });
    </script>

在控制器中,我收到了一个隐藏输入的值

$phone= $this->security->xss_clean($this->input->post('d_mobile'));echo $phone;

已正确发送值

答案 1 :(得分:0)

通常,我会通过以下方式获得国家和地区代码:

  1. 为拨号代码和国家/地区代码添加隐藏的输入。
  2. 将“ countrychange”事件侦听器添加到“ tel”类型的输入中。
  3. 只需读取输入元素的值即可。

在您的html文件中:

<input name="d_mobile" id="d_mobile" type="tel">
<input type="hidden" id="d_dial_code" name="d_dial_code" readonly>
<input type="hidden" id="d_country_code" name="d_dial_code" readonly>

在您的js文件中:

var d_mobile = document.querySelector("#d_mobile");
var d_dial_code = document.querySelector("#d_dial_code");
var d_country_code = document.querySelector("#d_country_code");

var d_country_code_picker = intlTelInput(d_mobile, {
    initialCountry: "auto", 
    geoIpLookup: function(success){
        $.get("https://ipapi.co/json/", undefined, "json")
        .done(function(response){
            success(response.country);
        });
    },
    utilsScript: "assets/js/utils.js"
});

d_mobile.addEventListener('countrychange', function(e) {
    d_dial_code.value = d_country_code_picker.getSelectedCountryData().dialCode;
    d_country_code.value = d_country_code_picker.getSelectedCountryData().iso2;
});

//Get the value of dial code, country code, and mobile number:
//.......
    d_dial_code: $("#d_dial_code").val(),
    d_country_code: $("#d_country_code").val(),
    d_mobile: $("#d_mobile").val(),
//.......