Re: Passing values from PHP



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. Can
anybody tell how I could do it.
innerHTML = "<?php echo $escapedToBeSafeForJSValue; ?>";

--
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
.



Relevant Pages

  • Re: Changing the Text In a Text Box
    ... David, thanks once again. ... books and learning new languages. ... back and forth between std:string and System:: String. ... Is there some sort of destructor needed in association with gcnew? ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Process arbitrary #lists (continued)
    ... I think what you might have meant was: "the gloriously elegant C++ solution". ... there's no need for me to have to read in an entire file so I can check how many lists there are before I can allocate space for them. ... in C++ if you do have a file in memory someplace (at least in a string) you can read from it as if it were a stream. ... Let me clarify std::map for you a little bit, David. ...
    (comp.lang.pl1)
  • Re: Macro in Powerpoint
    ... David, I got it to let me enter the information, but where does the ... displays that information on a new slide. ... Dim vAddress As String ...
    (microsoft.public.powerpoint)
  • Re: Naming conventions -- was: Re: DRYing a Regex
    ... class SpecialString < String ... Yet, if I understand you correctly, you and James are claiming that it ... type-checking completely falls down with this pattern. ... David A. Black/Ruby Power and Light, ...
    (comp.lang.ruby)
  • Re: Bijective 2 State Arithmatic coding question
    ... David A. Scott July 2004 ... string again, ... It actaully compresses the string that is in the byte file. ...
    (comp.compression)