Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
it's better if you do javascript stuffs in 'VIEW' file. – Charlie Mar 30 at 7:46
@Charlie: this javascript is on view . . – Jay Mar 30 at 7:48
you can not access javascript variable in php. Simple reason is Javascript is client side whereas PHP is on server. – Suresh Kamrushi Mar 30 at 7:50
@SureshKamrushi : i actually want to call controller function(for example input(parameter)) in my javascript code on view – Jay Mar 30 at 7:57
1  
$.post('codeigniter/controller/input',{ param1: "param2", param2: "param2" } function(data) { $('.result').html(data);}); – Suresh Kamrushi Mar 30 at 8:05
show 3 more comments

1 Answer

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);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.