Re: Passing values from PHP
- From: Jeremy <jeremy@xxxxxxxxxxx>
- Date: Wed, 30 Jan 2008 14:35:17 -0800
liamgegan@xxxxxxxxx wrote:
On Jan 30, 6:31 pm, David Dorward <dorw...@xxxxxxxxx> wrote:anush wrote:In a javascript file, I have to pass PHP values to the innerHTML. CaninnerHTML = "<?php echo $escapedToBeSafeForJSValue; ?>";
anybody tell how I could do it.
--
David Dorwardhttp://dorward.me.uk/http://blog.dorward.me.uk/
That may be a little ambiguous though - as innerHTML is a property of
DOM element nodes. Maybe:
html_value1 = "<?php echo $escapedToBeSafeForJSValue; ?>";
. . .
your_element.innerHTML=html_value1;
A tip: I like to use json_encode for this:
var value = (<?php echo json_encode($variable); ?>);
If it's a string, it will automatically be escaped properly and surrounded with quotes. But you can also pass numeric data, arrays, and anonymous objects in this fashion, which is pretty convenient.
json_encode/decode is included with PHP >= 5.2.
Jeremy
.
- References:
- Passing values from PHP
- From: anush
- Re: Passing values from PHP
- From: David Dorward
- Re: Passing values from PHP
- From: liamgegan@xxxxxxxxx
- Passing values from PHP
- Prev by Date: Re: Passing values from PHP
- Next by Date: Re: Why does this code NOT execute ?
- Previous by thread: Re: Passing values from PHP
- Next by thread: comp.lang.javascript FAQ - META 2008-01-30
- Index(es):
Relevant Pages
|