0

i have a little confusion while assigning a jquery array to php array, i have multiple checkboxes on a page, i want to assign its value on their click event, i retrieve values perfectly, but fails to assign to php array, here is my code during testing, finally i want it to assign to php session after working:

function updateSession() {         
     var allVals = [];
             <?php $ids = array()?>
     $('#c_b :checked').each(function() {
       allVals.push($(this).val());
       <?php $ids ?> = allVals;
     });
     // i am testing here in alert!
              alert(<?=$ids?>);
  }
 $(function() {
   $('#c_b input').click(updateSession);
   updateSession();
 });

};

Many thanx in advance!

2
  • Javascript runs on client side,But php runs on server.You canot assign value to a php variable using php.(Use ajax to psss vslues from javascript to php)
    – Shijin TR
    Commented May 13, 2013 at 6:19
  • This is not doable this way as php is server side while javascript is client side. You should use ajax to send allVars to the server then on the server you set your variable
    – CodeBird
    Commented May 13, 2013 at 6:22

1 Answer 1

0

You can't do this. PHP executes on the server before your Java Script runs. You can use PHP to build dynamic JavaScript, but you can't update PHP variables from JavaScript directly... To pass values from JavaScript to the server you can do it via AJAX or query string

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.