1

So I want to store the session in jquery. export_type is storing all the ids which is checked via checkbox I want to store all those ids in the session. Following is my code , But I have Uncaught SyntaxError: Invalid or unexpected token error. Any help will be appreciated. thanks in advance.

$("#export").click(function () {
    var id = [];
    $(':checkbox:checked').each(function (i) {
        id[i] = $(this).val();
    });
    var export_type = id;
    var set_session = "<?php $this->session->set_userdata('export_type', export_type); ?>";
    export_php();
});
Alek Stephanok
  • 193
  • 2
  • 17

2 Answers2

1

Javascript cannot read PHP. What you will have to do is to have your script make a separate request to a CodeIgniter controller.

Javascript:

$.get("export/set_session/" + export_type, function (result) {
    console.log(result);
})

PHP (application/controllers/Export.php):

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Export extends CI_Controller {

    public function set_session($export_type)
    {
        $this->session->set_userdata('export_type', $export_type);
        echo 'Session set!';
        return;
    }
}
naturaljoin
  • 475
  • 2
  • 7
0

if your above javascript code lies in the view file as inline javascript inside a script tag then it is should be fine but, if this code is inside in a different javascript file then it will not work.

zeeeuu
  • 19
  • 4