Passing values from PHP to JavaScript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • darksteel21
    New Member
    • Jul 2008
    • 36

    Passing values from PHP to JavaScript

    can anyone tech me how yo pass values from php script to javscript??
  • Gulzor
    New Member
    • Jul 2008
    • 27

    #2
    A simple output does the job :

    [php]
    <script type="text/javascript">
    function hello(name) {
    alert("hello "+name);
    }

    <?php
    while ( $rec = mysql_fetch_ass oc($query) ) {
    echo 'hello("',$rec['firstname'],'");', "\n";
    }
    ?>

    // or

    hello("<?php echo $var; ?>");

    </script>
    [/php]

    Comment

    • dlite922
      Recognized Expert Top Contributor
      • Dec 2007
      • 1586

      #3
      we usually put the echo or print line in the value of a "hidden" text box.

      Code:
      <input type="hidden" name="someVar" id="someVar" value="<?php echo $myVar; ?>" />
      and then you can reach it via javascript once the page has been written (remember javascript = runs on client computer, PHP executes on the server before its delivered to the client computer)

      So the browser sees the page as

      [HTML]

      <input type="hidden" name="someVar" id="someVar" value="YOUR_MY_ VAR_VALUE_HERE" />

      [/HTML]

      and now JavaScript can access it this way:

      Code:
      alert("Value is " + document.getElementById("someVar").value);

      If you're printing this value elsewhere on the page, put inside a tag and give that tag an id for example:

      Code:
      <div id="container">
         <span id="someVal"><?php echo $myVal; ?></span>
      </div>
      and you can access that with the attribute "innerHTML" such as

      Code:
      alert("Value is " + document.getElementById("someVar").innerHTML);
      if you STILL don't get it, Google: "miracle".


      Dan

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Originally posted by dlite922
        we usually put the echo or print line in the value of a "hidden" text box.
        ...
        Just a thought.
        Do you do this just to pass the values into JavaScript?

        If so, you could simply add it to the header as a global variable.
        Like:
        [code=html]
        <html>
        <head>
        <title>Test stuff</title>
        <script type="text/javascript">
        // Create a var in the global scope.
        // Can be accessed by any Javascript that follows, anywhere in the page.
        var phpValue = "<?php echo $someValue; ?>";
        </script>
        </head>

        <!-- This should print the value when the page loads -->
        <body onload="javascr ipt: alert(phpValue) ;">
        <h1>Nothing to see here... move along</h1>
        </body>
        </html>
        [/code]
        Doesn't really make sense to me to add actual HTML markup for each variable, when you can simply add it to a Javascript block.

        Comment

        • samikhan83
          New Member
          • Sep 2007
          • 33

          #5
          i agree with atli ....

          if u want to pass php value to javascript as parameter then u can do it this way...

          say u have a function in javascript by name passPhpValue(pa rm1);
          [code=html]
          passPhpValue(pa rm1)
          {
          alert("This is PHP value:"+parm1);
          }

          javascript:onlo ad="passPhpValu e(<?=$phpvalue? >)";
          [/code]
          Last edited by Atli; Aug 7 '08, 12:13 AM. Reason: Added [code] tags

          Comment

          • Gulzor
            New Member
            • Jul 2008
            • 27

            #6
            Originally posted by samikhan83
            i agree with atli ....

            if u want to pass php value to javascript as parameter then u can do it this way...

            say u have a function in javascript by name passPhpValue(pa rm1);

            passPhpValue(pa rm1)
            {
            alert("This is PHP value:"+parm1);
            }

            javascript:onlo ad="passPhpValu e(<?=$phpvalue? >)";
            You should not use this <?=$var?> (on its way to deprecation), but this <?php echo $var; ?> instead.

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              Originally posted by Gulzor
              You should not use this <?=$var?> (on its way to deprecation), but this <?php echo $var; ?> instead.
              A good point, but the short-tags have not been made deprecated just yet.
              They will still exist in PHP6, but like in PHP5, they will not be enabled by default.
              To use them you must reconfigure PHP.

              So, like you say, using the short-tags should be avoided at all costs. By using the standard tags <?php ... ?>, you ensure that your code will always work as expected, no matter how the server is configured.

              Comment

              • samikhan83
                New Member
                • Sep 2007
                • 33

                #8
                Originally posted by Atli
                A good point, but the short-tags have not been made deprecated just yet.
                They will still exist in PHP6, but like in PHP5, they will not be enabled by default.
                To use them you must reconfigure PHP.

                So, like you say, using the short-tags should be avoided at all costs. By using the standard tags <?php ... ?>, you ensure that your code will always work as expected, no matter how the server is configured.


                thanx Atli

                for the information
                from now on i will use <?php echo $id?> instead of <?=$id?>

                Comment

                • MerilFernando
                  New Member
                  • Mar 2017
                  • 4

                  #9
                  Code:
                  <?php  $xphp= "My PHP WORLD"; ?>   <!-- Define a PHP variable -->
                  <script> var xjs='My Java Script world'; </script> <!-- Define a JAVASCRIPT variable -->
                  <input type='hidden' id='myhtml' value='My HTML world!' > <!-- Define a HTML variable -->
                  
                  <BR>sending PHP variable value into JAVASCRIPT <BR>
                  <script>
                  var xphp='<?php echo $xphp; ?>';
                  document.write(xphp);    
                  </script>
                  
                  <BR>getting PHP variable value into HTML <BR>
                  <?php echo $xphp; ?>
                  
                  <BR><BR>getting JAVASCRIPT  variable value into PHP <BR>
                  <?php
                  $xjs = "<script>document.write(xjs);</script>";
                  echo $xjs;
                  ?>
                  
                  <BR>getting JAVASCRIPT  variable value into HTML <BR>
                  <script>document.write(xjs);</script>
                  
                  <BR><BR>getting HTML variable value into JAVASCRIPT  <BR> 
                  <script>
                  var xhtml=document.getElementById('myhtml').value;
                  document.write(xhtml);    
                  </script>
                  
                  <BR>getting HTML variable value into PHP <BR> 
                  <?php
                  $xhtml = "<script>document.write(document.getElementById('myhtml').value);</script>";
                  echo $xhtml;
                  ?>

                  Comment

                  Working...