0

I'm integrating codeigniter with phpgrid, and I'm having a trouble with passing the row values from phpgrid (in VIEW A) to another view (VIEW B) through javascript and codeigniter controllers

I have a virtual column like this in PHPGRID (VIEW A):

$col_formatter = <<<COLFORMATTER
function(cellvalue, options, rowObject, rowid){ 
    var sessid = rowObject[0];

    return '<input type="button" value="View" onclick="btnView('+sessid+')">';
} 
COLFORMATTER;

and the javascript in VIEW A:

function btnView(sessid){
    var dataRow = {
        sessid:sessid,
    };
    $.ajax({
        type:"POST",
        url: "<?php echo base_url()."index.php/main/tes"; ?>",
        data: dataRow,
        success: function(msg){
        }
    });
    return false;
}

in the Codeigniter CONTROLLERS:

public function tes(){
    $data['sessid'] = $_POST['sessid']; 
    $this->load->view('view_b', $data);
}

I can't seem to load the view. I used Mozilla's Firebug to know the response, it's true that the response is the code of my view_b view, but how can I switch to that view?

4
  • Where would the view_b data be placed? It seems you need to redirect the page or just place the response inside a html element Commented Jul 19, 2013 at 1:20
  • The view_b page is in codeigniter views folder, which should be opened if I code $this->load->('view_b', $data); but it didn't Commented Jul 19, 2013 at 3:23
  • AJAX calls return the response text to the success callback, if what you want is to just move from VIEW A to VIEW B and pass a POST value then use a form with method POST Commented Jul 19, 2013 at 5:23
  • @koala_dev Oh that's why! But, the problem is that the "view" button must be in javascript due to phpGrid library. I think I can't either put javascript form with method POST because I'm using CodeIgniter which is based on php, or, can I? Commented Jul 24, 2013 at 4:18

3 Answers 3

1
//Your are using ajax for some operation and want to reload the view page the you can test these options:
1) take a div in current view page and assing ajax retrun message to that div

function btnView(sessid){
var dataRow = {
    sessid:sessid,
};
$.ajax({
    type:"POST",
    url: "<?php echo base_url()."index.php/main/tes"; ?>",
    data: dataRow,
    success: function(msg){
         $("#divid").html(msg);
    }
});
return false;
}


//Or 2)just redirect to your view page again

function btnView(sessid){
var dataRow = {
    sessid:sessid,
};
$.ajax({
    type:"POST",
    url: "<?php echo base_url()."index.php/main/tes"; ?>",
    data: dataRow,
    success: function(msg){
        window.location.href=path to your view page;//<?php echo base_url()."index.php/controller/function"; ?>
    }
});
return false;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I see that your second answer will redirect to the view I wanted but without passing the sessid to that view, which will be used for showing data based on the sessid. Now the first answer could be the solution, but I still need to work some things out. Thank you @Girish
according to your question you have to return sessid from your class and pass sessid as argument to the url. on controllers function where you are loading the view pass the sessid to view and on view page check wheather sassid is exists or not. if exists the then show button <input type="button" value="View" onclick="btnView('+sessid+')">
0

If I understand correctly you want to go from VIEW A to VIEW B (meaning an actual change in the window location) and pass a value to VIEW B so it can generate some dynamic content. Well, AJAX is not the solution here since it will not trigger a change of page but instead will return the markup/text of the response as a string.

But there are still a number of ways you can achieve what you want, using Codeigniter the simpler way would be to use an argument for your controller method that you can send as part of the uri in a link:

HTML

<a href="http://example.com/index.php/main/tes/{sessid}">View</a>

Since you're using Javascript to generate the markup you would need something like this

return '<a href="<?php echo site_url('main/tes')?>/'+sessid+'">View</a>';

*Note that you will now have a link instead of a button but you can use CSS to style it any way you want it.

You would now retrieve the value in your controller like this:

PHP

public function tes($sessid){
    $data['sessid'] = $sessid; 
    $this->load->view('view_b', $data);
}

Pretty simple. A second option will be to use a form instead of your button to send the value using either GET or POST, forms do trigger a change in page whenever they are submitted:

HTML

<form action="http://example.com/index.php/main/tes" method="get">
    <input type="submit" value="{ssid}" name="sessid" />
</form>

Again using javascript:

return '<form action="<?php echo site_url('main/tes')?>" method="get">'
       +'<input type="submit" value="'+sessid+'" name="sessid" />'
       +'</form>';

And to get the value in your controller:

PHP

public function tes(){
    $data['sessid'] = $_GET['sessid']; //OR $_POST['sessid']
    $this->load->view('view_b', $data);
}

2 Comments

Thank you so much for your effort @koala_dev, but I've found the way to solve the problem. I posted the answer above
@Marsha Well your answer is basically what I explained above, but it would be simpler (and more efficient) to just use an anchor as in my first example instead of a button that calls a function on click
0

Turns out that passing a variable from javascript to codeigniter controller is just:

function btnView(sessid){
    window.location = "printvo/"+sessid;
}

I was using ajax because I don't know how to pass the variable, I always thought that passing variable is using brackets: window.location = "printvo("+sessid+")"; and that didn't work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.