SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Php Variable Into Javascript Variable

    What's the notation to redefine a php variable as a javascript variable?
    PHP Code:
    <?php
    include 'connect_to_mysql.php';
    $results mysql_query("SELECT * FROM plan WHERE TIMESTAMP(NOW()) < TIMESTAMP(end)") or die(mysql_error());
    $phpVar mysql_num_rows($results);
     
    echo 
    '<script type="text/javascript">';
    echo 
    'var x  = ???????????';
    echo 
    'document.write("<p>" + x + "</p>")';
    echo 
    '</script>';
    ?>

  2. #2
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What's the notation to redefine a php variable as a javascript variable?
    Code:
    <script type="text/javascript">
    
    var myJsVar = <?php  echo $myPhpVar; ?> ;
    
    </script>

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks.

    Is there a way to code the whole script in php (just wondering)?

    PHP Code:
    <?php
    include 'connect_to_mysql.php';
    $results mysql_query("SELECT * FROM plan WHERE TIMESTAMP(NOW()) < TIMESTAMP(end)") or die(mysql_error());
    $myPhpVar mysql_num_rows($results);

    echo 
    '<script type="text/javascript">';
    echo 
    'var myJsVar  = ???????????';
    echo 
    'document.write(myJsVar)';
    echo 
    '</script>';
    ?>

  4. #4
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    echo 'var myJsVar  = '.$myPhpVar

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Can't get it to work. Seems pretty straight forward. Any ideas?

    PHP Code:
    <?php
    $myPhpVar 
    15;
    echo 
    '<script type="text/javascript">';
    echo 
    'var myJsVar  = ' $myPhpVar;
    echo 
    'document.write(myJsVar)';
    echo 
    '</script>';
    ?>

  6. #6
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You just need to do some basic debugging.

    Have a look at the js code generated by the php in your browser and the error should then be obvious.

  7. #7
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Page source says: <script type="text/javascript">var myJsVar = 15document.write(myJsVar)</script>

    Looks like I'm just echoing strings.

    Then, how should I format the javascript echos to keep the code all php?

  8. #8
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you need to put either a semi-colon or new line char (preferably a semi-colon) at the end of your js generated by php in order to separate the individual js statements.

  9. #9
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I thought that's what I did:
    PHP Code:
    <?php
    $myPhpVar 
    15;
    echo 
    '<script type="text/javascript">';
    echo 
    'var myJsVar  = ' $myPhpVar;
    echo 
    'document.write(myJsVar)';
    echo 
    '</script>';
    ?>
    What do you mean?

  10. #10
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In this line here

    PHP Code:
    echo 'var myJsVar  = ' $myPhpVar
    the semi-colon at the end is just the normal mandatory semicolon at the end of the php echo statement. It doesn't output a semi-colon at the end of the js code that the echo statement generates. Your echo statement needs to output a semi-colon at the end of the js statement it generates.

  11. #11
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Zehr gut!

    echo 'var myJsVar = ' . $myPhpVar . ';';

    Thanks webdev1958.

    Niche

  12. #12
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well done - glad it's finally sorted

    I'm going for a


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •