How should I pass a php array or object as parameter to some javascript function?

2,651 views
Skip to first unread message

Shivam Garg

unread,
Dec 21, 2013, 6:56:52 AM12/21/13
to wncc...@googlegroups.com
I have written the following code to pass  a php array or object as parameter to some javascript function, but no alert is coming up on the page. What is the error in this code?

function testfunc(obj1,obj2)
{
var test1=json.parse(obj1);
alert(test1.el1);
}
<?php
$arr1=array("el1"=>"1","el2"=>"2","el3"=>"3");
$arr2=array("el21"=>"21","el22"=>"22");
?>
<button type="button" onclick="testfunc(<?=json_encode($arr1)?>,<?=json_encode($arr2)?>)">Merge</button>

Dilli Babu K

unread,
Dec 21, 2013, 8:18:17 AM12/21/13
to wncc...@googlegroups.com
Hi,

Its because of in your onclick attribute of button tag. I checked it, after execution the page source seems like:
<button el1":"1","el2":"2","el3":"3"},{"el21":"21","el22":"22"})"="" onclick="testfunc({" type="button">Merge</button>
So you have to change that logic only.


Note :-
I changed your code like this. Because i'm using windows system. it won't understand the <?= ?> tag.

<script>

    function testfunc(obj1,obj2)
    {
        var test1=json.parse(obj1);
        alert(test1.el1);
    }
</script>

<?php
$arr1=array("el1"=>"1","el2"=>"2","el3"=>"3");
$arr2=array("el21"=>"21","el22"=>"22");
?>
<button type="button" onclick="testfunc(<?php echo json_encode($arr1); ?>,<?php echo json_encode($arr2); ?>)">Merge</button>


--
--
The website for the club is http://stab-iitb.org/wncc
To post to this group, send email to wncc...@googlegroups.com
 
---
You received this message because you are subscribed to the Google Groups "Web and Coding Club IIT Bombay" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wncc_iitb+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Thanks & Regards,
Dillibabu K
Pentamine Technologies Pvt Ltd.
#364/78, 2nd Floor,
Nagaprabha Plaza, 19th Main,
1st Block, Rajaji Nagar,
Bangalore-560 010,
Karnataka

INDIA
T : (080-40946297)
M:7845627277,
Landmark : Near(Diacon Hospital)
.

Shivam Garg

unread,
Dec 21, 2013, 12:43:07 PM12/21/13
to wncc...@googlegroups.com
What did you change in the code except <?= ?> tag. What changes are needed?

Shivam Garg
2nd year Undergraduate
CSE
IIT Bombay

Dilli Babu K

unread,
Dec 22, 2013, 1:40:29 AM12/22/13
to wncc...@googlegroups.com
Nothing, Just changed this "<?=json_encode($arr1)?>" to "<?php echo json_encode($arr1); ?>" to make it work. Because i'm using lower version of PHP in window OS so that. if you are executing in linux means it will work.


Here is the working code :
<script>
    function testfunc(obj1,obj2)
    {
        var test1=JSON.parse(obj1);
        alert(test1.el1);
    }
</script>
<?php
    $arr1 = array("el1"=>"1","el2"=>"2","el3"=>"3");
    $arr2 = array("el21"=>"21","el22"=>"22");
?>
<button type="button" onclick='testfunc(JSON.stringify(<?php echo json_encode($arr1); ?>),JSON.stringify(<?php echo json_encode($arr2); ?>))'>Merge</button>

Note: in that function should be passed as value(number or string) or variable, can't pass json result, Because of syntax error.

SIDDHARTH BIDWAN

unread,
Dec 22, 2013, 1:43:11 PM12/22/13
to wncc...@googlegroups.com
There is this chance that when you use json_encode, it adds the keys of JSON object with DOUBLE_QUOTES.
Hence your html markup breaks.
so instead use:
<button onclick='testFunc({"el1": 234, "el2": 4},2)'>sidbid</button>
[observe single quotes for onclick]

FE example:

BE example:

echo json_encode(array(
'el1' => 2,
'el2' => 4,
));

gives:
{"el1":2,"el2":4}

BTW, please note that this is not the right way to use objects. It is one of the JS anti-pattern.





Shivam Garg

unread,
Dec 23, 2013, 1:57:22 AM12/23/13
to wncc...@googlegroups.com
Thanks Dilli Babu and Siddharth, it was JSON.stringify() function which I missed. Now its working. :)

Shivam Garg
2nd year Undergraduate
CSE
IIT Bombay


Reply all
Reply to author
Forward
0 new messages