4

I'm doing something which I need to pass an array from my php to a function on an external javascript. I'm currently doing some test right now which looks like this:

<input type="text" onclick="show_error_message('<?php echo $array_sample ?>')" readonly="readonly">

and the javascript function is this one:

function show_error_message(test) {
alert(test)
}

my $array_sample contains data which is array("1","2","3"); but the return alert would be Array and whenever I change the $array_sample into $array_sample[0] when passing the parameters I got a data which is 1 as alert. I wonder how can I pass the whole array on this to be fetched also by an array in javascript. Well as you can see, I intended it to be a popup message for error handling which is why I need it to be dynamic.

0

3 Answers 3

15

Use json_encode

Also, all PHP output in HTML context must be encoded with htmlspecialvchars(). So:

onclick="show_error_message(<?= htmlspecialchars(json_encode($array_sample)) ?>)"

Notice the lack of quotes(') around the php code, this way an array literal is passed to show_error_message and not a string.

7
  • Oh I see, there's a return value saying function Array() { [native code] } in my alert. using your first edit this one : onclick="show_error_message(<?php echo $array_sample ?>)" but after I use the json_encode part again the alert box won't appear using the function in javascript which is not yet edited. Is that normal? Commented Oct 29, 2012 at 6:26
  • @KaHeL I don't see why it won't work, do you get any errors in the console? are you using php version 5.2 or greater? Commented Oct 29, 2012 at 6:32
  • Oh, there yes it has an error. SyntaxError: "syntax error show_error_message([" As for PHP I'm using 5.3 64bit wampserver and also I'm using codeigniter as framework. Can that be the cause of problem? Commented Oct 29, 2012 at 6:37
  • 1
    @KaHeL I just realized that strings will be encoded with " which will terminate your onclick attribute prematurely, onclick="show_error_message(["1","2","3"])". You can use single quotes for you attributes or escape the double quotes in the php echo. Commented Oct 29, 2012 at 6:45
  • at last! this works! It displays 1,2,3 I think the next step is for me to contain the passed value into a variable array for me to fully control it am I correct? And moreover, thanks for this! I really appreciate your help. :) Commented Oct 29, 2012 at 6:47
4

Encode PHP array to json data using json_encode() function.

And in Javascript use JSON.parse() to parse the Json string.

2
  • I see, I tried to change my onclick into onclick="show_error_message(<?php echo json_encode($array_sample) ?>)" and the javascript function in tact but after I clicked my text box the alert won't appear. Commented Oct 29, 2012 at 6:20
  • Try something like this: onclick="show_error_message(<?php echo "'".json_encode($array_sample)."'" ?>)" Commented Oct 29, 2012 at 6:26
3

The below code shows how to pass php array to javascript:

<script type="text/javascript">
    function mufunc(a)
    {
        var temp = new Array();
        temp = a.split('~');
        for(i=0;i<temp.length;i++)
        {
            alert(temp[i]); 
        }
    }
</script>

<?php
    $a = array('a','b','c');
    $b = implode("~",$a);
?>
<a href="javascript:void(0)" onClick="mufunc('<?php echo $b; ?>')">Click Here</a>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.