Php 将变量从javascript函数传递到codeigniter中的控制器方法
我是CodeIgniter的新手。我正在做一个项目,在这个项目中我在视图中创建了一个javascript函数,在这个视图中我定义了一个变量。。看起来像这样Php 将变量从javascript函数传递到codeigniter中的控制器方法,php,javascript,codeigniter,codeigniter-2,Php,Javascript,Codeigniter,Codeigniter 2,我是CodeIgniter的新手。我正在做一个项目,在这个项目中我在视图中创建了一个javascript函数,在这个视图中我定义了一个变量。。看起来像这样 var $rowCount=0; function invest() { $rowCount=$('#ulinvestment').find('.rowInvestment').length; } 我的控制器函数包含 function input(parameter //i want to pass $rowcount value he
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();
}
我想访问controller函数中的变量
$rowCount
,我该怎么做?为了确保我理解您的意思,您正在尝试将一个变量从javascript传递到CodeIgniter的控制器函数,对吗
如果这是您的情况(希望是这样),那么您可以使用AJAX,或者制作一个锚定链接
首先,您将使用,特别是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>
最好是在“查看”文件中执行javascript内容。@Charlie:此javascript正在查看中。您不能在php中访问javascript变量。原因很简单,Javascript是客户端,而PHP是服务器端。@SureshKamrushi:我实际上想在我的Javascript代码中调用控制器函数(例如input(参数)),在视图$.post('codeigniter/controller/input',{param1:'param2',param2:'param2}函数(数据){$('.result').html(数据);});
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);