0

I have to pass a PHP JSON array into a JavaScript function's on click event.

Before JSON encode PHP array structure:

Array
(
    [group_id] => 307378872724184
    [cir_id] => 221
)

After JSON encode array structure:

{"group_id":"307378872724184","cir_id":"221"}

The PHP JSON array looks like:

if(!empty($cirGrpArr))

   $jsonGrpArr = json_encode($cirGrpArr);

I need to pass the same into a javascript on click function like below:

<span onclick="Login(this,JSON.stringify('<?php echo $jsonGrpArr; ?>'))">click here</span>

The PHP JSON array is available in this page but how to pass it on click function so that i can iterate this JSON array to do some other stuff.

4
  • 3
    Why do people insist on writing JavaScript as java-script?! Nowhere, not in any documentation, or any half-decent tutorial, is it written that way. And yet it's a very common error here on SO. Dec 10, 2013 at 12:23
  • A JSON string is in itself valid javascript. When the string is printed it can be iterated as an array, or an object if it contains key/value pairs.
    – Flosculus
    Dec 10, 2013 at 12:24
  • What is $jsonGrpArr? Don't show us PHP when you have a question about JavaScript. Show us the generated code that is sent to the browser.
    – Quentin
    Dec 10, 2013 at 12:33
  • 1
    What a convoluted way of assigning data to an event: make php print out html code with javascript inside it, make javascript dynamic by passing it json encoded php variables... all in one line. This will be a nightmare when the time for maintenance comes
    – hanzo2001
    Dec 10, 2013 at 12:43

2 Answers 2

0
<span onclick="Login(this,'<?php echo $jsonGrpArr; ?>')">click here</span>

and

function Login(e, jsonString){
var data = JSON.parse(jsonString);
}
9
  • but when i try to debug data via console.log(), its throwing Uncaught SyntaxError: Unexpected token ILLEGAL in console. any idea? Dec 10, 2013 at 12:30
  • i am getting e as this object.. but when trying to debug data after parsing it is showing unexpected token error... Dec 10, 2013 at 12:33
  • in <?php echo $jsonGrpArr; ?> I am assuming this a json string. If not then encode using json_encode() in php
    – Bilal
    Dec 10, 2013 at 12:36
  • ok then go step by step. first of all pass json and console.log(jsonString) to verify you are getting data as expected then move to next step and please print the actual error you see in browser console. copy and paste.
    – Bilal
    Dec 10, 2013 at 12:42
  • this is what i see when i debug via console... <span onclick="Login(this,'{"group_id":"307378872724184","cir_id":"221"}')"="">click here</span>.. actually there seems some issue at last of json array when i did copy html from console. But while looking console the code was correct like Login(this,'{"group_id":"307378872724184","cir_id":"221"}')" and this "="" was not there... is it something causing issue??? Dec 10, 2013 at 12:43
0

You can pass encoded object in function by this method.

*.php

<a class="pointer" href="javascript:void(0)"  onclick="return pass( event.target.dataset.param)" data-param="<?php echo htmlspecialchars(json_encode(array('key'=>value))) ?>">Click it</a>

*.js

function pass(json_string='{}'){
    let data=JSON.parse(json_string);
    let value=data['key']??'';
    console.log('value',value);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.