将变量从javascript函数传递给codeigniter中的控制器方法 [英] pass variable from javascript function to controller method in codeigniter

查看:111
本文介绍了将变量从javascript函数传递给codeigniter中的控制器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是CodeIgniter的新人。我正在做一个项目,我在其中我定义一个变量的JavaScript函数。它看起来像这样

  var $ rowCount = 0; 
function invest(){
$ rowCount = $('#ulinvestment')。find('。rowInvestment')。
}

我的控制器函数包含

 函数输入(参数//我想在这里传递$ rowcount值){
$ this-> load-> helper('form');
$ this-> load-> helper('html');
$ this-> load-> model('mod_user');
$ this-> mod_user-> insertdata();
}

我想访问变量 $ rowCount 在控制器函数中,我该怎么做?

解决方案

为了确保我理解你,尝试将一个变量从javascript传递给CodeIgniter的控制器函数,对吗?



如果是这样,那么你可以使用AJAX, 。



首先,您要使用 URI类,特别是 segment()函数。



让我们假设这是你的控制器:

 类MyController扩展CI_Controller 
{
function input b $ b $ parameter = $ this-> uri-> segment(3); //假设此函数可通过MyController / input访问

$ this-> load-> helper '形成');
$ this-> load-> helper('html');
$ this-> load-> model('mod_user');
$ this-> mod_user-> insertdata();
}
}



现在使用javascript可以制作锚标签,或使用AJAX:



方法1:制作锚标签:

 < a href =id =anchortag>点击我将一些数据发送到MyController / input< / a> 


< script>

var $ rowCount = 0;
function invest(){
$ rowCount = $('#ulinvestment')。find('。rowInvestment')。
}
$('#anchortag')。prop('href',localhost / index.php / MyController / input /+ $ rowCount);
< / script>方法2:使用AJAX:

b

  var $ rowCount = 0; 
function invest(){
$ rowCount = $('#ulinvestment')。find('。rowInvestment')。
}
//发送AJAX get请求(你也可以发送post请求)
$ .get('localhost / index.php / MyController / input /'+ $ rowCount);


I'm new in CodeIgniter. I am making a project in which I make a javascript function in view an in this I define a variable .. it looks like this

var $rowCount=0;
function invest() {
  $rowCount=$('#ulinvestment').find('.rowInvestment').length;
}

my controller function contains

function input(parameter //i want to pass $rowcount value here ){
$this->load->helper('form');
$this->load->helper('html');
$this->load->model('mod_user');
$this->mod_user->insertdata();
 }

I want to access the variable $rowCount in controller function, how can I do that?

解决方案

Just to make sure I understand you, You are trying to pass a variable from javascript to CodeIgniter's controller function, right?

If that's your case(hopefully it is), then you can use AJAX, or craft an anchor link.

First of all, you're going to use the URI class, specifically the segment() function.

Let's assume this is your controller:

class MyController extends CI_Controller
{
    function input(){
    $parameter = $this->uri->segment(3);//assuming this function is accessable through MyController/input

    $this->load->helper('form');
    $this->load->helper('html');
    $this->load->model('mod_user');
    $this->mod_user->insertdata();
    }
}

Now with javascript you can either craft an anchor tag, or use AJAX:

Method 1: By crafting an anchor tag:

<a href="" id="anchortag">Click me to send some data to MyController/input</a>


<script>

var $rowCount=0;
function invest() {
  $rowCount=$('#ulinvestment').find('.rowInvestment').length;
}
$('#anchortag').prop('href', "localhost/index.php/MyController/input/"+$rowCount);
</script>

Method 2: By using AJAX:

var $rowCount=0;
function invest() {
  $rowCount=$('#ulinvestment').find('.rowInvestment').length;
}
//Send AJAX get request(you can send post requests too)
$.get('localhost/index.php/MyController/input/'+$rowCount);

这篇关于将变量从javascript函数传递给codeigniter中的控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆