How do I pass variables out of PHP

Discussion in 'PHP' started by Imozeb, Jan 31, 2010.

  1. #1
    How do I pass variables out of PHP and into html or javascript. I want to use this function to make an error message appear on the html form so the user knows which field she entered wrong.

    Thanks.

    ~Imozeb
     
    Imozeb, Jan 31, 2010 IP
  2. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #2
    This is how you can set a php variable as a javascript variable.

    
    <script type="text/javascript">
    var test = <?php echo $phpVariable ?>;
    </script>
    
    HTML:
     
    Silver89, Jan 31, 2010 IP
  3. neo1211

    neo1211 Active Member

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    93
    #3
    Alternatively, if you're the kind of person who prefers shorter solutions, rather than <?php echo $phpVariable; ?>, you can simply use <?=$phpVariable?>
     
    neo1211, Jan 31, 2010 IP
  4. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #4
    Yeh I actually had that written but changed it because some people have short tags turned off :rolleyes: Anyone that preferred a shorter solution probably wouldn't need to pass php variables into javascript anyway...
     
    Silver89, Jan 31, 2010 IP