Home > Enterprise >  How can i get the value of the country code Selected
How can i get the value of the country code Selected

Time:01-25

I am new to Js and i am able to get the phone number am passing but not able to get the country code i have selected how can i get it in my controller

<!DOCTYPE html>
<html lang="en">
 <head>
   
   <meta name="viewport" content="width=device-width, initial-scale=1" />
   <link
     rel="stylesheet"
     href="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.8/css/intlTelInput.css"
   />
   <script src="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.8/js/intlTelInput.min.js"></script>
 </head>
 <body>
   <input id="phone" type="tel" name="phone" />
 </body>
  <script>
   const phoneInputField = document.querySelector("#phone");
   const phoneInput = window.intlTelInput(phoneInputField, {
     utilsScript:
       "https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.8/js/utils.js",
   });
 </script>
</html>

this is my test.php view page

        public function send_mail() {
    
        $this->load->library('form_validation');
        $this->form_validation->set_rules('fname', 'First Name', 'required');
        $this->form_validation->set_rules('lname', 'Last Name', 'required');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
        $this->form_validation->set_rules('phone', 'Phone', 'required|regex_match[/^[0-9]{10}$/]');
        $this->form_validation->set_rules('esubject', 'Subject', 'required');
        $this->form_validation->set_rules('msg', 'Message', 'required');
        // $this->form_validation->set_rules('g-recaptcha-response', 'Capcha', 'required');
    
        if ($this->form_validation->run() == FALSE) {
          $this->load->view('contact');
          }
       else {
    
      $to = "";
        $cc = "";
        $fname = $this->input->post('fname');
        $lname = $this->input->post('lname');
        $email = $this->input->post('email');
        $phone = $this->input->post('phone');
        echo $phone; exit;
}
}

This is my controller Main.php

I need to get the country code and phone number in this variable $phone;

CodePudding user response:

Please refer related answer from here: How to get Country Code from (intl-tel-input) plugin after submitting the form?

CodePudding user response:

you need to try this way

const phoneInput = window.intlTelInput(phoneInputField, {
     utilsScript:
       "https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.8/js/utils.js",
   });
 var code = phoneInput.getSelectedCountryData().iso2;
  • Related