0

Is is possible to put javascript into a php function? If so could someone please tell me what is wrong with my code? because whenever i put more than one javascript into the php function, my whole functionality will be lost.

 <?php

    function headerA($title,$css) {
    $headContent = <<<HEAD
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
            <head>
                <title>$title</title>
                <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
                <link rel="stylesheet" type="text/css" href="$css" />   

                <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
                <script type="text/javascript">
                    $(function() {
                        $("li").click(function(e) {
                          e.preventDefault();
                          $("li").removeClass("selected");
                          $(this).addClass("selected");
                        });
                    });
                </script> 


            </head>
            <body>
            <div id = "header">

            </div>
    HEAD;
    $headContent .="\n";
    return $headContent;
    }

    ?>
6
  • 1
    Yes, you can, but you should use heredoc properly. $ is a key character in PHP, which jQuery uses the same. Escape the characters, or avoid using heredoc if you find it difficult to use Commented Nov 18, 2013 at 6:32
  • 1
    sidenote: Better to use HTML5 DOCTYPE; cleaner and more up-to-update. Commented Nov 18, 2013 at 6:32
  • And remember- PHP is parsed and executed in one place (on the server), and Javascript is parsed and executed completely separately (in the client's browser). Even though you can co-mingle JavaScript and PHP on the same source page, they're two completely different languages running in two completely different contexts, and they cannot "talk" directly to each other. Commented Nov 18, 2013 at 6:33
  • 1
    What you mean by "lost"? Also, "header" is a internal function name, change it to something else. php.net/manual/en/function.header.php Commented Nov 18, 2013 at 6:34
  • i had already change the "header" to "headerA" for tesing, but it still doesnt work. Commented Nov 18, 2013 at 6:39

3 Answers 3

1

Your code is fine. Just make sure you:

  • place HEAD; in the very start of a line (no prepended whitespaces)
  • escape the $ symbols in Javascript.

The $ symbols in your JavaScript (jQuery) is conflicting with PHP's in-string $ variable marker. Prepend $s will a backslash as follows: \$.

Sign up to request clarification or add additional context in comments.

3 Comments

there are no white spaces . and it works just fine if i remove the javascrip tags . But all my functionalities will be lost whenever i put in those javascript tags
@AuroraBlaze escape the symbol will fix your issue.
I used another way to fix it. Still thank you very much for the explanations ^^, I appreciate it very much =). I had learned something new from everyone.
1

You should escape the $

<<<HEAD
  $php_variable
  \$('jQuery');
HEAD;

or using output buffer

<?php ob_start(); ?>
  <?= $php_variable ?>
  $('jQuery');
<?php $head = ob_get_clean(); ?>

Comments

1

do not use header as a function name because it's a predefined function of php.

Heredoc text behaves just like a double-quoted string, without the double quotes. There should be no space before ending HEAD line as below.

check manual

<?php 
function header1($title,$css) {
    $headContent = <<<HEAD
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
            <head>
                <title>$title</title>
                <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
                <link rel="stylesheet" type="text/css" href="$css" />   

                <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
                <script type="text/javascript">
                    $(function() {
                        $("li").click(function(e) {
                          e.preventDefault();
                          $("li").removeClass("selected");
                          $(this).addClass("selected");
                        });
                    });
                </script> 


            </head>
            <body>
            <div id = "header">

            </div>
HEAD;
    $headContent .="\n";
    return $headContent;
    }

    echo header1("title", 'url');
?>

1 Comment

thank you very much for the explanations. I appreciate it. Umm actually I accidentally ate a word "make" while its suppose to be "makeHeader" when I was posting. but now I had changed it to "headerA" for testing and I had found another way to fix the problem. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.