drbd65446 2015-05-01 12:05
浏览 81
已采纳

如何将变量从JavaScript传递给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.

P.S. I'm new to CodeIgniter.

  • 写回答

1条回答 默认 最新

  • dongtuoleng8624 2015-05-01 12:09
    关注

    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
       }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?