简体   繁体   English

如何将变量从JavaScript传递到CodeIgniter

[英]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. 我使用以下代码将值发送到服务器(PHP脚本)以采取相应的措施。

JS code: JS代码:

    $.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. 在我转到CodeIgniter之前,它工作得很好。 However I'm confused no how to save the details in DB or fetch data from DB in CodeIgniter. 但是我不知道如何在CodeIgniter中将详细信息保存在数据库中或从数据库中获取数据。

PS I'm new to CodeIgniter. PS我是CodeIgniter的新手。

You need to specify the absolute path in CI 您需要在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 使用base_url() -为此,您需要检查url帮助器

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

// your PHP file for controller create Controller.php //您的用于控制器的PHP文件创建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 //您的Model的PHP文件创建Controller_model.php

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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