How can store javascript variable to php session variables?

Umpong_1947676

I am having trouble passing javascript arrays and other variables to my php controller. I am doing this in codeigniter. I have assigned some values on javascript like this:

var count = ($('#listOfProducts tr').length);
//loop start
var i=0;
grid=$('#listOfProducts input[type="checkbox"]:checked').each(function(){
    var $row = $(this).parents('tr'); 
    var $trid =$(this).closest('tr').attr('id');
    rowid[i]=$trid; 
        rowfields.push({itemname: $row.find('td:eq(0)').text(), productname:$row.find('td:eq(1)').text(), productdesc: $row.find('td:eq(2)').text(), unitprice:$row.find('td:eq(3)').text(), quantity:$row.find('td:eq(5) input').val(), amount:$row.find('td:eq(6) input').val()});
    i++;
});

i have to pass it to my controller so i could save the values to my database... Any help? I am thinking of storing this array as a session variable but I do not know how, considering the client-side and server-side issues.

Mr.Web

I use CI and the best way I found is to send it thru AJAX, like this:

$('.your-element').click(function(e){
    var id = 1;
    $.ajax({
        url: base_url()+'yourController/youFunction/'+id,
        type: 'get',
        dataType: 'html',
        success: function(html) {
            //YOUR ACTION
        }
    });

});

Let us know if you need more specifications. NOTE: This method creates problems if you have sessions data going on as every ajax call changes the session id or doubles the time of the session_update. To handle this see this post: HANDLING CODEIGNITER SESSION/AJAX PROBLEMS

Edit:

For the base_url() in JS I wrote this in the index.php file:

<script>
function base_url(){
  var url  = '<?= base_url() ?>';
  return url;
}
</script>

Array Edit

For an Array ajax-call I'd do it using jQuery $.post(), like this:

$('.your-element').click(function(e){
    e.preventDefault();
    var demoArray = ['1', '3', '7'];
    $.post(base_url()+'admin/test/', {array_send:demoArray}, function(result){
        alert(result);
    });
});

Where the controller admin/testis:

public function test(){
    print_r($_POST['array_send']);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to store $_SESSION variables in Javascript for XMLHttpRequest purposes?

How to store a variable inside a JavaScript session cookie?

How to store a variable in session?

How to store a JavaScript variable into a PHP variable?

Store php exec in a session variable

How to access php SESSION variable in javascript

How can I store and retrieve Django session variable to/from database?

How can I capture php session variables in javascript without page refresh?

How to store variable in session storage

How to store the variable throughout the session?

How can you store more than one word in a php $_SESSION?

How to store session at remote server with ajax request javascript/PHP

Store PhP variable in JavaScript variable

Modifying PHP Session Variables with JavaScript

PHP: How to store session in cookie

can i store a cookie value set from javascript into a php variable?

jQuery ajax pass variable to php session, but php session not store it

PHP $_SESSION only store the last variable

Store or get User ID as PHP session variable

PHP How can I store variables in an array and call them

Falcon framework - how to store variables in a session?

How can I access my session variable in a different page with php?

How do I access session variable from php in external javascript

How can I store a variable by passing it by reference in PHP?

How can I store a concatenated MYSQL string query into a PHP variable?

How can I store the previous value of a variable in Javascript?

How to store a primary key in a session variable

How to store the input field value in PHP into database and then assign it to variable in javascript

How to pass a Javascript variable to PHP to store in MYSQL database