-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?

4
  • 3
    Possible duplicate of Passing javascript value to PHP Commented Oct 11, 2017 at 8: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. Commented Oct 11, 2017 at 8:42
  • 2
    Possible duplicate of What is the difference between client-side and server-side programming? Commented Oct 11, 2017 at 8:44
  • Can be achieved by converting php array to json then javascript array. Commented Oct 11, 2017 at 8:45

2 Answers 2

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';
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.