0

I'm trying to use a php array inside some jquery script. This is what I have:

$("#domainPackage").change(function (){
    "use strict";
    var packID;
    var info;
        packID = $("#domainPackage").val();
        info = "<?php echo $package[" + packID + "]['pages'] ?>";
    $("#domainPageLimit").val(info);
});

But, it is outputting <?php echo $package[2]['pages'] ?> into my input box as the value.

What is the correct way of using a PHP array in some jquery?

Thank you

Rick
  • 441
  • 1
  • 3
  • 15
  • 1
    You should utilize the `json_encode` function in php when dealing with arrays. Makes it much easier to access and iterate through the data. See http://php.net/manual/en/function.json-encode.php – coletrain Sep 29 '18 at 22:43
  • 2
    Are you running the page on a server that has PHP installed, and the page has a .php extension? PHP runs on the server and the result is sent to the user where the JavaScript then runs. – j08691 Sep 29 '18 at 22:43
  • Possible duplicate of [PHP code is not being executed, instead code shows on the page](https://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-page) – miken32 Sep 29 '18 at 23:41

2 Answers2

0

This should actually be generating a PHP error which makes me suspect you do not have PHP enabled on this server, or the file extension of the file you are editing is not .php.

Once you have fixed that issue, you will need to load the values from the array into the script at run-time, or use AJAX. You cannot interact directly between PHP and jQuery as the PHP runs on the server side before the page is rendered, whereas the jQuery runs in real-time on the client side in the browser.

To load in runtime you could try the following:

$("#domainPackage").change(function (){
    var data = JSON.parse(<?=json_encode($package)?>);
    "use strict";
    var packID;
    var info;
    packID = $("#domainPackage").val();
    info = data[packID]['pages'];
    $("#domainPageLimit").val(info);
});
lufc
  • 1,965
  • 2
  • 15
  • 19
  • I'm running php 7. The code I'm using is on an separate js file that is called into my php page with . – Rick Sep 29 '18 at 23:05
  • You need to put this code directly in the body of the PHP file or the server won't know to process it as PHP. – lufc Sep 30 '18 at 02:05
0

The problem with your approach:

"The code I'm using is on an separate js file that is called into my php page with <script></script>

is that the script is being processed on the client without ever being seen by PHP, so it cannot process the contents of the <?php ... ?> block. Hence your output of <?php echo $package[2]['pages'] ?>. To make this work you need to output the data into an array/object in the .php file (the json_encode approach suggested by @user1491032 and @coletrain is a good one) and then access that array in your script file i.e.

PHP file:

echo "<script>var package = JSON.parse('" . json_encode($package) . "');</script>";

JS file:

info = package[packID]['pages'];

Update

As was pointed out by @DavidBélanger, you don't need to use JSON.parse if the object is well built, you can just assign the variable directly i.e. in the PHP file:

echo "<script>var package = " . json_encode($package) . ";</script>";

and in the JS

info = package[packID].pages;
Nick
  • 138,499
  • 22
  • 57
  • 95