Page 1 of 1
how to print array through ALERT
Posted: Wed Dec 20, 2006 12:12 am
by eshban
I have an array like
Code: Select all
Array
(
[0] => check_files/toplogo.png
[1] => check_files/topphp.png
[2] => check_files/shadow_tr.jpg
[3] => check_files/shadow_bl.jpg
[4] => check_files/shadow_br.jpg
)
Can i print all the paths through JAVASCRIPT ALERT
Re: how to print array through ALERT
Posted: Wed Dec 20, 2006 12:19 am
by Christopher
In Javascript use the join method (e.g. myarray.join(",")). In php use the implode function (e.g. implode(",", $myarray)).
Posted: Wed Dec 20, 2006 12:52 am
by Kieran Huggins
you could use something like:
Code: Select all
function array_alert($array){
echo '<script type="text/javascript">alert("'.implode('\n',array_map(@addslashes,$array)).'");</script>';
}
but BE WARNED - this is not guaranteed to work - it could end up in the middle of an attribute or something!
Cheers,
Kieran
Posted: Wed Dec 20, 2006 1:08 am
by Kieran Huggins
OK - it bothered me a little that the alert function was so picky, here's a new one:
Code: Select all
function jAlert($var){
if(is_array($var)) return print('<script type="text/javascript">alert("'.implode('\n',array_map(@addslashes,$var)).'");</script>');
return print('<script type="text/javascript">alert("'.addslashes($var).'");</script>');
}
It accepts either an array OR a string.
Cheers,
Kieran
Posted: Wed Dec 20, 2006 11:01 am
by John Cartwright
a simple one liner
Code: Select all
function jAlert($var){
return '<script type="text/javascript">alert("'.addslashes(is_array($var) ? implode("\n", $var) : $var).'");</script>';
}
You had a couple problems,
a) whats the point of returning a print?
b) carriage needs to be processed in double quotes, else it's taken literally
c) your use of array_map isn't correct, you should consult the manual
Posted: Wed Dec 20, 2006 2:56 pm
by Kieran Huggins
Jcart wrote:a simple one liner
Code: Select all
function jAlert($var){
return '<script type="text/javascript">alert("'.addslashes(is_array($var) ? implode("\n", $var) : $var).'");</script>';
}
You had a couple problems,
a) whats the point of returning a print?
b) carriage needs to be processed in double quotes, else it's taken literally
c) your use of array_map isn't correct, you should consult the manual
a: Returning a print killed two birds with one stone - exiting the function as soon as the first print happened, AND providing a non-falsy return value that also happens to contain the output of the function.
b: Carriage returns need to be processed in double quotes
in PHP - in this case the single quotes are used to pass the
properly escaped carriage return string to Javascript. If you give Javascript a real carriage return, it breaks the alert() function.
c: As far as I can tell, my use of array_map() was correct, I just double checked the manual. Do you have any particular objection in mind to this usage?
Furthermore:
1: addslashes must not be run on the whole string, as it will escape our already "escaped" \n.
2: your function doesn't work (at least for arrays) - test it!
All that being said, I do like your idea of making it a one-line function, but that makes it more difficult to extend to more data types, like objects.
Cheers,
Kieran
Posted: Wed Dec 20, 2006 8:52 pm
by Ollie Saunders
c: As far as I can tell, my use of array_map() was correct, I just double checked the manual. Do you have any particular objection in mind to this usage?
array_map(@addslashes... triggers an error saying constant 'addslashes' not defined assuming 'addslashes' -- the string which, incidentally...is the correct usage

EDIT: someone very recently on planetphp has
blogged about this.
Code: Select all
// consider: define('addslashes', 'unexceptedRedirection');
array_map(@addslashes, $array); // bad
array_map('addslashes', $array); // good
read about the
callback type, its very interesting.
Posted: Thu Dec 21, 2006 2:40 am
by Kieran Huggins
I must have been thinking about Javascript when I used that callback!
Now that you mention it, it makes perfect sense

Thanks ole.