1

我是 CodeIgniter 的新手。我正在制作一个项目,其中我在视图中创建了一个 javascript 函数,并在其中定义了一个变量.. 它看起来像这样

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

我的控制器功能包含

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();
 }

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

4

1 回答 1

0

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

如果这是您的情况(希望是),那么您可以使用 AJAX,或制作锚链接。

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

假设这是您的控制器:

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();
    }
}

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

方法1:通过制作锚标签:

<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>

方法2:通过使用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);
于 2013-04-02T01:31:34.063 回答