How to use variable in javascript funtion ?

Discussion in 'JavaScript' started by CasTex, Sep 26, 2010.

  1. #1
    I want to echo the variable when I use test(); fuction. I meanwhen I use the variable with this function like test(john) , how to use it in that php snippet. Anyone knows ?

    <script type="text/javascript">
    function test(){
    document.getElementById("php_code").innerHTML="
    <?php echo "hello"; ?>
    ";
    }
    </script>
    PHP:
     
    CasTex, Sep 26, 2010 IP
  2. bigrollerdave

    bigrollerdave Well-Known Member

    Messages:
    2,112
    Likes Received:
    52
    Best Answers:
    0
    Trophy Points:
    140
    #2
    
    <script type="text/javascript">
    	function test(name){
    		document.getElementById("php_code").innerHTML= name;
    	}
    </script>
    <div id="php_code"></div>
    HTML:
     
    bigrollerdave, Sep 26, 2010 IP
  3. CasTex

    CasTex Member

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    I mean I wanted to use the value in that php script, like <? echo hello $val1; ?>
     
    CasTex, Sep 27, 2010 IP
  4. bigrollerdave

    bigrollerdave Well-Known Member

    Messages:
    2,112
    Likes Received:
    52
    Best Answers:
    0
    Trophy Points:
    140
    #4
    ohh gotcha well the code that you wrote should work just fine. However if you're trying to change the php variable after the page has been loaded this isn't possible. All php variables are created before the page loads and can't be altered unless you reload the page.
     
    bigrollerdave, Sep 27, 2010 IP
  5. GFX_Lover

    GFX_Lover Peon

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I know, why it doesn't work.
    There is a line break after the double quotes.

    Now, it'll work ;)

    document.getElementById("php_code").innerHTML = "<?php echo "hello"; ?>";
     
    GFX_Lover, Oct 3, 2010 IP