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

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码