Help with undefined variable

Discussion in 'Javascript' started by Jeremy Felt, Jan 23, 2007.

  1. Jeremy  Felt

    Jeremy Felt Guest

    Newbie here. I'm sure I'm missing something EXTREMELY simple, but an
    hour of searching has led to nothing.

    I'm playing around with ajax and trying to pass a variable to a
    function.

    If I do:

    onclick="myFunction(12345)"

    It works fine. If I do:

    onclick="myFunction($variable)"

    It doesn't work at all and IE gives an error stating that $variable is
    undefined.

    $variable is given a value right before the onclick statement with PHP
    as:

    $variable=12345;

    There's more code involved, but I'm guessing this is something simple
    for somebody with experience. Can anybody help?

    Thanks Much!
    Jeremy Felt, Jan 23, 2007
    #1
    1. Advertising

  2. Jeremy Felt wrote:

    > It doesn't work at all and IE gives an error stating that $variable is
    > undefined.


    > $variable is given a value right before the onclick statement with PHP


    PHP runs on the server and generates some data which the server sends to the
    client.

    JavaScript runs on the client.

    A variable in PHP won't magically appear in JavaScript. You have to have PHP
    generate some JavaScript which creates the variable.

    --
    David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
    Home is where the ~/.bashrc is
    David Dorward, Jan 23, 2007
    #2
    1. Advertising

  3. Jeremy  Felt

    Jeremy Felt Guest

    On Jan 23, 4:16 pm, David Dorward <> wrote:
    > Jeremy Felt wrote:
    > > It doesn't work at all and IE gives an error stating that $variable is
    > > undefined.
    > > $variable is given a value right before the onclick statement with PHPPHP runs on the server and generates some data which the server sends to the

    > client.
    >
    > JavaScript runs on the client.
    >
    > A variable in PHP won't magically appear in JavaScript. You have to have PHP
    > generate some JavaScript which creates the variable.
    >
    > --
    > David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
    > Home is where the ~/.bashrc is


    Right. I think I have that part, but maybe not. I wrote the previous
    message too quickly because I was on my way out the door.

    I am using PHP for the database stuff and "echo"ing it out as
    html/javascript. The real statement in question is along the lines of:

    ------------------------
    $variable=12345;
    echo '<span style="underlined text blah blah"
    onclick="myFunction($variable)">text</span>'; // doesn't work
    -----------------------
    vs.
    ----------------------
    echo '<span style="underlined text blah blah"
    onclick="myFunction(12345)">text</span>'; // works
    -----------------------

    A global.js file that defines myFunction is being called earlier in the
    code with another echo statement. This part seems to be working
    because I can replace $variable with 12345 and have it work.

    My admittedly weak understanding is that PHP would pass the value of
    $variable to the HTML/JS stuff when it was outputted on the client side
    just like any other PHP page working with variables and that Javascript
    would never see the $variable portion, just 12345.

    Does that change anything/make any sense?

    Thanks!
    Jeremy Felt, Jan 23, 2007
    #3
  4. Jeremy Felt wrote:

    > The real statement in question is along the lines of:
    > $variable=12345;
    > echo '<span style="underlined text blah blah"
    > onclick="myFunction($variable)">text</span>'; // doesn't work


    > My admittedly weak understanding is that PHP would pass the value of
    > $variable to the HTML/JS stuff when it was outputted on the client side
    > just like any other PHP page working with variables and that Javascript
    > would never see the $variable portion, just 12345.


    Which would make this a PHP question. You could View > Source and see the
    output wasn't what you wanted. Strings delimited by single quotes are not
    interpolated in PHP, so echo '$foo' outputs $foo and not the value of a
    variable named $foo.

    --
    David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
    Home is where the ~/.bashrc is
    David Dorward, Jan 24, 2007
    #4
  5. Jeremy  Felt

    Randy Webb Guest

    Jeremy Felt said the following on 1/23/2007 6:30 PM:
    >
    > On Jan 23, 4:16 pm, David Dorward <> wrote:
    >> Jeremy Felt wrote:
    >>> It doesn't work at all and IE gives an error stating that $variable is
    >>> undefined.
    >>> $variable is given a value right before the onclick statement with PHPPHP runs on the server and generates some data which the server sends to the

    >> client.
    >>
    >> JavaScript runs on the client.
    >>
    >> A variable in PHP won't magically appear in JavaScript. You have to have PHP
    >> generate some JavaScript which creates the variable.
    >>
    >> --
    >> David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
    >> Home is where the ~/.bashrc is

    >
    > Right. I think I have that part, but maybe not. I wrote the previous
    > message too quickly because I was on my way out the door.
    >
    > I am using PHP for the database stuff and "echo"ing it out as
    > html/javascript. The real statement in question is along the lines of:
    >
    > ------------------------
    > $variable=12345;
    > echo '<span style="underlined text blah blah"
    > onclick="myFunction($variable)">text</span>'; // doesn't work


    Right Click>View Source.

    What HTML is the browser receiving? If it is getting $variable (which
    the error indicates) then PHP isn't parsing it as you want and it's a
    PHP problem.


    onclick="myFunction(<? $variable >)"
    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
    Randy Webb, Jan 24, 2007
    #5
  6. Jeremy  Felt

    Jeremy Felt Guest

    On Jan 23, 6:09 pm, David Dorward <> wrote:
    > Jeremy Felt wrote:
    > > The real statement in question is along the lines of:
    > > $variable=12345;
    > > echo '<span style="underlined text blah blah"
    > > onclick="myFunction($variable)">text</span>'; // doesn't work
    > > My admittedly weak understanding is that PHP would pass the value of
    > > $variable to the HTML/JS stuff when it was outputted on the client side
    > > just like any other PHP page working with variables and that Javascript
    > > would never see the $variable portion, just 12345.Which would make this a PHP question. You could View > Source and see the

    > output wasn't what you wanted. Strings delimited by single quotes are not
    > interpolated in PHP, so echo '$foo' outputs $foo and not the value of a
    > variable named $foo.
    >
    > --
    > David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
    > Home is where the ~/.bashrc is



    Ahhh, that's making more sense. I usually use double quotes in PHP,
    but I was working off of another AJAX sample and hadn't thought to
    change the single quotes yet. That's what I get for jumping in without
    really knowing what I'm doing.

    I'll check this out when I'm back in tomorrow, but I think you have me
    on the right path. Thanks again.
    Jeremy Felt, Jan 24, 2007
    #6
  7. Jeremy  Felt

    Jeremy Felt Guest

    Thanks for the help guys and sorry for the misdirected PHP post. I
    separated the $variable from the single quotes and it works beautifully.
    Jeremy Felt, Jan 24, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mantorok Redgormor
    Replies:
    70
    Views:
    1,724
    Dan Pop
    Feb 17, 2004
  2. Kingdom

    Variable is undefined: 'JMail' help?

    Kingdom, Dec 10, 2003, in forum: ASP General
    Replies:
    4
    Views:
    162
    John Beschler
    Dec 10, 2003
  3. Anthony Ob

    Help! Undefined local variable

    Anthony Ob, Sep 26, 2010, in forum: Ruby
    Replies:
    3
    Views:
    120
    Josh Cheek
    Sep 27, 2010
  4. VK
    Replies:
    45
    Views:
    577
    Dr John Stockton
    Sep 12, 2006
  5. -Lost
    Replies:
    13
    Views:
    357
    Richard Cornford
    Jan 31, 2007
Loading...

Share This Page