-1

Javascript:

function setitem(id, phpvalue){
                var data = {
                    Sub_category: $("#Subcategory").val()
                }
                var formsubmission = '<?php echo base_url().'index.php/AJAX/get_item'?>';
                $.ajax({
                    method: "POST",
                    url: formsubmission,
                    data: data,
                    success: function(response){
                      $("#Item_"+id).find('option').not(':first').remove();
                      $("#Item_"+id+" option:first").after(response);
                      $("#Item_"+id).prop("disabled", false);
                      var item_value = '<?php echo $Items["+phpvalue+"]["Item"]?>';
                      $("#Item_"+id).val('<?php echo $Items[0]["Item"]?>');
                      $("#Item_"+id).selectpicker('refresh');
                    }
                })
            }

Anyone can please tell me How can I use phpvalue as an array index?

  • 3
    Possible duplicate of [Passing javascript value to PHP](https://stackoverflow.com/questions/37086813/passing-javascript-value-to-php) – anteAdamovic Oct 11 '17 at 08:41
  • in simple words you cannot do it that way - when the javascript is to be executed (and phpvalue exist), the php would have already run. – Calimero Oct 11 '17 at 08:42
  • 2
    Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Calimero Oct 11 '17 at 08:44
  • Can be achieved by converting php array to json then javascript array. – Naga Oct 11 '17 at 08:45

2 Answers2

0

You want to use the PHP variable in your Javascript script so you don't want to convert javascript value to PHP but the opposite : convert php value to javascript like :

var formsubmission = <?php echo base_url() ?> + 'index.php/AJAX/get_item';
Fabien Salles
  • 1,101
  • 15
  • 24
0

If you want to transfer a javascript variable to a php variable, there are 2 ways of doing the same.

Here's How

You can run an ajax request and then shoot an ajax request to your server and save the data into a variable using $_SESSION and then get the data back onto 'a php backed page' using the same session variable.

P.S. Make sure you wrap everything inside an if block because if the ajax request fails due to any reason,then you won't be very glad to have an unassigned php variable being used somewhere.

Yash Kumar Verma
  • 9,427
  • 2
  • 17
  • 28