简体   繁体   中英

Passing values from view to controller and again from controller to view?

I have a controller suppose a and it has a function suppose index() where i passed data from my view suppose b using javascript like this:

var formURL = "<?php echo base_url();?>a/index/"+ $("#someid").val();
$.post(formURL).done(function(data){$("#something").html(data);

now i am receiving it in my controller like this:

public function index($somevalue= ""){
....
....
}

now after execution from the controller a i am again passing array values to the view b like this:

public function index($somevalue= ""){
....
....
$data['value1'] = $value1;
$data['value2'] = $value2;
$this->load->view('b', $data);
}

now when i am accessing the data in the view b like this:

<?php if (isset($value1)) {
echo $value1;
}?>

i am not getting the value of value1 . what did i do wrong in this case???

It would be a lot lot better if you use AJAX . This is what it precisely does.

AJAX request to controller ie (sending datas to controller) Get the AJAX result ie (sending datas back to views)

and from client side you can update it.

You can get the form values like

var formDatas = $('#form').serialize();

Then make an ajax request like

$.ajax({
    type : 'POST',
    url : 'url',
    data : {formdata : formDatas },
    success: function(result){
      //update the view with the result
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM