How to retrieve javascript object value in PHP ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • phdfromic@yahoo.com

    #1

    How to retrieve javascript object value in PHP ?

    I want to set an initial text in a TEXTAREA based on the value selected
    in an earlier SELECT box in the same document.

    I tried something like

    $init_text=set_ init_text() ;
    <TEXTAREA>
    <?php echo $init_text ; ?>
    </TEXTAREA>

    function set_init_text() {
    return <script>
    document.forms[0].SelectBox.opti ons[document.forms[0].SelectBox.sele ctedIndex].value
    </script }


    But I get a php syntax error with the <script> tag.

    What's the correct way to do it ?

  • Peter Fox

    #2
    Re: How to retrieve javascript object value in PHP ?

    Following on from 's message. . .[color=blue]
    >I want to set an initial text in a TEXTAREA based on the value selected
    >in an earlier SELECT box in the same document.[/color]
    Sorry but you have completely mis-understood how php works.

    Client browser - "Web server! send me this resource"
    Web server - "OK" [ FX rummages around while running AND FINISHING a php
    script] "Here it is now - Thank you and have a nice day"
    Client browser - "User I will show you the HTML just sent to me by the
    server"


    Javascript = client side
    php = server side. [ie no interaction]



    [color=blue]
    >
    >I tried something like
    >
    >$init_text=set _init_text() ;
    ><TEXTAREA>
    ><?php echo $init_text ; ?>
    ></TEXTAREA>
    >
    >function set_init_text() {
    >return <script>
    >document.for ms[0].SelectBox.opti ons[document.forms[0].SelectBox.sele ctedInde
    >x].value
    ></script }
    >
    >
    >But I get a php syntax error with the <script> tag.
    >
    >What's the correct way to do it ?
    >[/color]

    --
    PETER FOX Not the same since the pancake business flopped
    peterfox@eminen t.demon.co.uk.n ot.this.bit.no. html
    2 Tees Close, Witham, Essex.
    Gravity beer in Essex <http://www.eminent.dem on.co.uk>

    Comment

    • Steve

      #3
      Re: How to retrieve javascript object value in PHP ?

      On Mon, 09 Jan 2006 09:32:42 +0000, Peter Fox wrote:

      No interaction, but php can quite happily write js just as easily as it
      can write html/xhtml/xml/...
      [color=blue]
      > Following on from 's message. . .[color=green]
      >>I want to set an initial text in a TEXTAREA based on the value selected
      >>in an earlier SELECT box in the same document.[/color]
      > Sorry but you have completely mis-understood how php works.
      >
      > Client browser - "Web server! send me this resource" Web server - "OK"
      > [ FX rummages around while running AND FINISHING a php script] "Here it
      > is now - Thank you and have a nice day" Client browser - "User I will
      > show you the HTML just sent to me by the server"
      >
      >
      > Javascript = client side
      > php = server side. [ie no interaction]
      >
      >
      >
      >
      >[color=green]
      >>I tried something like
      >>
      >>$init_text=se t_init_text() ;
      >><TEXTAREA>
      >><?php echo $init_text ; ?>
      >></TEXTAREA>
      >>
      >>function set_init_text() {
      >>return <script>
      >>document.form s[0].SelectBox.opti ons[document.forms[0].SelectBox.sele ctedInde
      >>x].value
      >></script }
      >>
      >>
      >>But I get a php syntax error with the <script> tag.
      >>
      >>What's the correct way to do it ?
      >>[/color][/color]

      Comment

      • Ike

        #4
        Re: How to retrieve javascript object value in PHP ?

        Do an HTTP POST (or GET) of a form, where you send the object from
        Javascript, calling the same page to rerender. When the page loads, in php,
        check if the value was sent. Suppose I have a js variable 'x'. I then put it
        in a hidden text field say, on a form, and POST the form.

        When the page reloads, it tests to see if x has been "set" in javascript
        (i.e. POSTED):

        $x = $_POST['x'];

        if $x =="" then it is the first time the form was loaded and x has not been
        set in js. (or, a subsequent time for loading the form after a POST and x
        was never set in js). You may want to check too if x==null, not sure if it
        is null or "". -Ike


        Comment

        Working...