Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

Trying out PHP for the first time and I'm really stumped on this. It might be something obvious, but why isn't my php inside the function checkinfo() working? The alert works fine. Here's the code

<head>
    <script type="text/javascript">

            function checkinfo() {
                alert("hi");
                <?php
                echo 'hi';
                ?>
            };

        </script>

    </head>
    <body>
        <form id = "form" align = "center">

            <div id = "index">

                <div id = "unandpw">
                    Username:
                    <input type="text">
                    <br>
                    Password:
                    <input type ="password" >
                </div>
                <!-- end unandpw -->

                <div id = "buttons">
                    <button type = "button" onclick = "checkinfo()">
                        Login
                    </button>
                    <button type = "button" onclick = "register();">
                        Register
                    </button>
                </div>
                <!-- end buttons -->

            </div>
            <!--index end -->

        </form>
    </body>
</html>
share|improve this question

marked as duplicate by Charles, Sven, Hanlet Escaño, Starx, Jan Dvorak Aug 17 '13 at 9:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

6  
'hi' means nothing in JavaScript... What are you expecting? –  elclanrs Aug 17 '13 at 8:14
    
@elclanrs I take it what you're trying to tell me is that it's incorrect to call <?php echo "hi" ?> in that location? What would be the valid approach then? –  Ceelos Aug 17 '13 at 8:17
    
Also, please refrain from vandalizing your own questions. –  Charles Aug 17 '13 at 8:20
    
@Charles lol, "vandalizing" –  Ceelos Aug 17 '13 at 8:21
1  
Think of the client-server model, that's the basics of understanding what's happening. PHP runs before JavaScript. You can generate JavaScript with PHP but you can't execute PHP inside JavaScript. echo merely prints text that will be interpreted by JavaScript later. –  elclanrs Aug 17 '13 at 8:22

4 Answers 4

It gives error, because when the rendered your script becomes this:

function checkinfo() {
   alert("hi");
   hi
};

Which is an incorrect code. You may be trying to do:

alert("<?php echo 'hi'; ?>");
share|improve this answer
    
What do you mean by "FAQ dupe" ? –  Ping Aug 17 '13 at 8:23
    
@Ping, He means a duplicate of Frequently Asked Questions. –  Starx Aug 17 '13 at 8:24

You have to use like:

function checkinfo() {
    var string = "<?php echo 'hi' ;?>";
    alert(string);
}
share|improve this answer
    
note that this will break if the PHP string contains double quotes. Use json_encode instead –  Jan Dvorak Aug 17 '13 at 9:55

You can also try like this:

Your php here:

<?php
$var = 'hai';
?>

Your JS here

<script>
      function checkinfo(){
        var val = "<?php echo $var; ?>";
        alert(val);
      }
    </script>
share|improve this answer
    
note that this will break if the PHP string contains double quotes. Use json_encode instead –  Jan Dvorak Aug 17 '13 at 9:56

        function checkinfo() 
                   {
           alert("<?php echo 'hi';?>")
                   };
    </script>

Try this.This will alert 'hi'.

share|improve this answer
    
this will break if the PHP string contains double quotes. Use json_encode instead –  Jan Dvorak Aug 17 '13 at 9:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.