Use php variable in javascript function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • emilia.woo@gmail.com

    #1

    Use php variable in javascript function

    Hello guys.
    I have a question about js function and php variables.

    I want to use php variables in javascript function. For example,


    <?php
    $ex=(int)$_POST['Example'];
    ?>

    $ex value is 3

    There's js function named "practice"
    I want to run

    <input type="button" onclick="practi ce(3); ">

    in this case, 3 is $ex. However, I cannot use like this:

    <input type="button" onclick="practi ce( <? $ex ? ); ">

    How can I use php variables in javascript function? Thank you.

  • emilia.woo@gmail.com

    #2
    Re: Use php variable in javascript function

    Oh, I find out!.
    it's just

    <input type="button" onclick="practi ce( '<? echo $ex; ?>' ); ">

    right?

    emilia.woo@gmai l.com wrote:
    Hello guys.
    I have a question about js function and php variables.
    >
    I want to use php variables in javascript function. For example,
    >
    >
    <?php
    $ex=(int)$_POST['Example'];
    ?>
    >
    $ex value is 3
    >
    There's js function named "practice"
    I want to run
    >
    <input type="button" onclick="practi ce(3); ">
    >
    in this case, 3 is $ex. However, I cannot use like this:
    >
    <input type="button" onclick="practi ce( <? $ex ? ); ">
    >
    How can I use php variables in javascript function? Thank you.

    Comment

    • Kimmo Laine

      #3
      Re: Use php variable in javascript function

      <emilia.woo@gma il.comwrote in message
      news:1161250908 .553940.206610@ h48g2000cwc.goo glegroups.com.. .
      Oh, I find out!.
      it's just
      >
      <input type="button" onclick="practi ce( '<? echo $ex; ?>' ); ">
      >
      right?

      Yes, that works. You can assign javascript variables with php like that,
      just remember that it doesn't work the other way though, you can't assign
      php variables with javascript. :)

      --
      "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
      http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
      spam@outolempi. net | rot13(xvzzb@bhg byrzcv.arg)


      Comment

      • countach

        #4
        Re: Use php variable in javascript function

        You have to keep in your mind that, in fact, PHP only mission is to 'paint'
        the data that travel to the user browser.

        Knowin' that, you can 'send' html, javascritp, css... plain text, or what
        ever you want. Then the browser will treat is exactly as if it were reading
        a normal .html file for example.


        En las nuevas, el emilia.woo@gmai l.com escribió:
        Oh, I find out!.
        it's just
        >
        <input type="button" onclick="practi ce( '<? echo $ex; ?>' ); ">
        >
        right?
        >
        emilia.woo@gmai l.com wrote:
        >Hello guys.
        >I have a question about js function and php variables.
        >>
        >I want to use php variables in javascript function. For example,
        >>
        >>
        ><?php
        >$ex=(int)$_POS T['Example'];
        >>>
        >>
        >$ex value is 3
        >>
        >There's js function named "practice"
        >I want to run
        >>
        ><input type="button" onclick="practi ce(3); ">
        >>
        >in this case, 3 is $ex. However, I cannot use like this:
        >>
        ><input type="button" onclick="practi ce( <? $ex ? ); ">
        >>
        >How can I use php variables in javascript function? Thank you.

        Comment

        Working...