Viewed   89 times

I'm trying to create a common constants file to share between php and javascript, using JSON to store the constants. But I'm wondering why pass the JSON from PHP to javascript using json_encode() over echoing the json declaration.

Let's say I have the PHP JSON

<?php 

$json_obj = '{"const1": "val",
            "const2": "val2"             
                            }';

?>

Googling, it seems the typical way of passing back to javascript is using

<?php echo json_encode($json_obj); ?>

Then I believe I would have to use something like $.getScript() to read the php file to get $json_obj and then use parseJSON() to make it useable in javascript.

But why not instead

<?php  echo 'var json = '.$json_obj; ?>

This way all you have to do is load the script directly and you have the json ready to use directly.

Is there a particular reason why it is more favorable to use json_encode() then simply echoing the declaration to javascript?

 Answers

5

In your case $json_obj is already a string. So it is not necessary. But if you have an array you want to pass to javascript json_encode will help you with this.

Friday, December 9, 2022
3

HMAC is a standard. So is SHA-256. So their outputs, regardless of which implementation, has to be the same.

There could only be differences in the Base64 encoding. Normally, the non-alphanumeric characters are + and /, but you cannot count on that. I've checked, and both implementations use the same non-alphanumeric characters.

However, you should still "manually" check a few thousand strings. The implementation in PHP is well tested. But I do not know if the same is true for the implementation in jQuery...

The syntax for Base64 encoded output is:

Crypto.util.bytesToBase64(
    Crypto.HMAC(Crypto.SHA256, sign, accessKey, { asBytes: true })
);
Thursday, October 27, 2022
 
3

I think this part looks suspicious

<body id="body"  onclick="cmbRuleField('' + <?php echo json_encode($varArray);?> + '');"    >

maybe

<body id="body"  onclick="cmbRuleField(<?php echo json_encode($varArray);?>)">

is more like it.

One more tip, you can see the output on the rendered page to determine what the written out code looks like. So if you see something like:

<body id="body"  onclick="cmbRuleField('['a', 'b']')">

you know there is a problem. You want a native Javascript array to be passed like this

<body id="body"  onclick="cmbRuleField(['a', 'b'])">

EDIT

After talking on chat it became clear the top portion of OP's code needed a tweak as well.

header("location: Rules.php?varFields=".http_build_query($varFields));
Friday, August 26, 2022
 
4

Try this:

<script>
    var data = <?php echo json_encode( $data ); ?>;
</script>
Monday, August 1, 2022
 
sakana
 
1

PHP's associative arrays become objects (hashes) in javascript.

data.array.a === 1
data.array.b === 2
// etc

If you want to enumerate over these values

for ( var p in data.array )
{
  if ( data.array.hasOwnProperty( p ) )
  {
    alert( p + ' = ' + data.array[p] );
  }
}
Sunday, October 16, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :