0

So I am currently doing some coding for the website of someone of my outer family and I had to create a bar chart and now I need to fill it with data from their SQL-Database. First I just echo'd the array like this:

PHP:

<?php
$exampleArray = ["212", "33", "7"]
?>

and JS:

<script> var jExampleArray = <? echo json_encode($exampleArray);?>;  </script>

And then I used jExampleArray in my bar chart code. Now I was very happy but the they say that it needs to be more secure (it involves their company) and I have to find a way to make sure nobody can just see the data while looking through the code on the page. I thought about using Ajax and what not but it just didn't work for me. I got it to alert me the array, but was not able to fill a Javascript array with it.

I never did stuff with JS, PHP oder SQL before and only learned Java in school so I am pretty clueless and most of the stuff I did was with help of the Internet. Luckily I managed to at least understand the code I wrote/copied.

Edit: [] for a JS array not {}

7
  • 5
    ExampleArray is not an array. Commented Jul 19, 2017 at 7:32
  • Also, whilst it may not be visible in 'View Source', a simple F12 (Developer Console) in IE/Chrome will allow them to see the resulting JS code, objects, data. Commented Jul 19, 2017 at 7:34
  • For it to be an array, It should be $exampleArray = ["212", "33", "7"]; Commented Jul 19, 2017 at 7:35
  • Woops sorry. Tried to type this without looking at my normal code to practise it. Commented Jul 19, 2017 at 7:44
  • for the future, copy the exact code from your IDE Commented Jul 19, 2017 at 7:48

3 Answers 3

1

Your syntax for creating the PHP array is incorrect.

Use the function json_encode to transform PHP arrays into Javascript arrays and objects.

<?php

$arr = ['hello', 'world', 'foo', 'bar'];
$obj = ['hello' => 'world', 'foo' => 'bar'];

echo 'var Array = ' . json_encode($arr) . PHP_EOL .
     'var Obj = ' . json_encode($obj, JSON_FORCE_OBJECT) . PHP_EOL;

Will result in the following:

var Array = ["hello","world","foo","bar"]
var Obj = {"hello":"world","foo":"bar"}
Sign up to request clarification or add additional context in comments.

4 Comments

I tried this but it did not work. It always said there is something wrong with the first line $arr = .....
What version of PHP are you running? Find out by creating a file that is just echo phpversion(). If it is less than 5.4, you need to create arrays using $arr = array('hello', etc');
Thanks! Seems like I am actually using 5.2
I will have to. But I'll only be here for about 8 other days. This said. I tried it with this and it now says "var ArrayTest = ['hello, 'test', 'wow'] (used them for a test). Now is there a way I can let JS output it by using document.write()?
0

Some pseudo code to show how you might accomplish the stated goal

$data=array();
while( $rs=$db->fetch() ){
    $data[]=array(
        'field_1'   =>  $rs->field_1,
        'field_2'   =>  $rs->field_2,
        'field_3'   =>  $rs->field_3
    );
}

$json=json_encode( $data );


<script>
    <?php
        echo "var jExampleArray={$json};";
    ?>
    /* rest of barchart code */
</script>

Comments

0

If the PHP array is available at page load use:

<?php
$phpArray = array('data1' => 2, 'data2' => 3);
?>
<script>var arr = <?php echo json_encode($phpArray); ?>;</script>

To retrieve PHP array in JS after page load using ajax:

var arr = JSON.parse(responseArray);

2 Comments

var arr = JSON.parse(responseArray); wouldn't work
@LucaKiebel JSON.parse is to convert raw text into JS array (if formatted correctly). Updated the thread specifying the method is to be used with ajax response.

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.