简体   繁体   中英

How to pass variables from JavaScript to CodeIgniter

I use the following code to send values to a server (PHP script) to take action accordingly.

JS code:

    $.ajax({
    type: "POST",
    url: "/application/helpers/fb_login_assist.php",
    data: {fbname: name, 
           fbid: id, 
           fbemail: email, 
           fbgender: gender,               
           fbimageURL: imageURL},              
    success: function(data){        
    console.log("Data sent to server");
    confirm("You have successfully logged in with FB");
    window.location.reload();       
    }
});

It worked completely fine before I moved to CodeIgniter. However I'm confused no how to save the details in DB or fetch data from DB in CodeIgniter.

PS I'm new to CodeIgniter.

You need to specify the absolute path in CI

  $.ajax({
type: "POST",
url: "<?php echo base_url(); ?>/index.php/controller/update_function",
 // path to the controller 
data: {fbname: name, 
       fbid: id, 
       fbemail: email, 
       fbgender: gender,               
       fbimageURL: imageURL},              
success: function(data){        
console.log("Data sent to server");
confirm("You have successfully logged in with FB");
window.location.reload();       
}
});

the problem is only with your path

use base_url() - for that you will need to check the url helper

https://ellislab.com/codeigniter/user-guide/helpers/url_helper.html

// your PHP file for controller create Controller.php

 class Controller extends CI_Controller
 {  
      public function update_function()
      {
         // load your model here 
        $this->load->model("Controller_model");

      }
 }

// your PHP file for Model create Controller_model.php

class Controller_model extends CI_Model
{
   public function update_data()   
   {
        // use database for updating values using post
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM