passing php date variable to javascript function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gubbachchi
    New Member
    • Jan 2008
    • 59

    passing php date variable to javascript function

    Hi all,

    How to pass the php date variable to javascript function. Here is the code I have tried out, but this is not recovering the date correctly in the javascript function

    Code:
    <html>
    <script type="text/javascript">
    
    function add($date1){
    
    alert($date1)
    }
    </script>
    <body>
    <?php
    $date1 = date("y-m-d");
    <input type="submit" value="GO" onclick="add('.date1.')">
    ?>
    </body>
    </html>
    When I alert the date1 variable in javascript, it will give some junk value instead of date. How to get the date value into javascript variable

    With regards
  • nehas
    New Member
    • Jan 2008
    • 13

    #2
    please ignore this see the below one
    Last edited by nehas; Feb 23 '08, 11:52 AM. Reason: it had a mistake

    Comment

    • nehas
      New Member
      • Jan 2008
      • 13

      #3
      Originally posted by gubbachchi
      Hi all,

      How to pass the php date variable to javascript function. Here is the code I have tried out, but this is not recovering the date correctly in the javascript function

      Code:
      <html>
      <script type="text/javascript">
      
      function add($date1){
      
      alert($date1)
      }
      </script>
      <body>
      <?php
      $date1 = date("y-m-d");
      <input type="submit" value="GO" onclick="add('.date1.')">
      ?>
      </body>
      </html>
      When I alert the date1 variable in javascript, it will give some junk value instead of date. How to get the date value into javascript variable

      With regards

      Its simple instead of writing
      Code:
       
      function add($date1){
      
      alert($date1)
      }
      just write it like this
      Code:
       
      function add(date1)
      {
      alert(date1);
      }
      
      <input type="submit" value="GO" onclick="add('<? echo $date1; ?>')">
      Do above changes and it will definately work. BEST OF LUCK

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Remember to keep your php seperate from your javascript!

        JS will have a fit if you try and include a php $ variable ;)

        Comment

        • gubbachchi
          New Member
          • Jan 2008
          • 59

          #5
          thank you all for your response

          Comment

          • MerilFernando
            New Member
            • Mar 2017
            • 4

            #6
            Data passing from Javascript, PHP, HTML

            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...