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.

Goodday, I am having trouble including css and js files in a PHP file.

Whenever I want to include a js file, the containing script element contains the whole html output.

Same goes for any included stylesheets.

It might be a output problem, but i really dont know what to edit to fix this.

Here is the code,

    /* Output*/
    $html                        = '<!DOCTYPE html>';
    $html                       .= '<html>';
    $html                       .= '<head>';
    $html                       .= '<title>'.SITE_TITLE.'</title>';
    $html                       .= '<script type="text/javascript">var HLINK = "'.SITE_URL.'/";</script>';
    $html                       .= '<script type="text/javascript" src="public/js/core.js"></script>';
    $html                       .= '<link rel="stylesheet" type="text/css" href="public/css/screen.css" />';
    $html                       .= '</head>';


    $html                       .= '<body>';
    $html                       .= '<section id="wrap">';

    switch ($pageurl[0]){
        default               :  $post =   require_once ('pages/homepage.php');  break;
    }

    $html                       .= '<div class="block">';
    $html                       .= 'Doe het<br>';
    $html                       .= '</div>';
    $html                       .= '<pre>';

    $html                       .= $post['post_id'];
    $html                       .= $post['post_title'];
    $html                       .= $post['post_subject'];

    $html                       .= '</pre>';
    $html                       .= '</section>';
    $html                       .= '</body>';
    $html                       .= '</html>';

    echo                           $html;

This might be stupid, but i cant figure it out. I appreciate any help.

This is what the browser outputs:

<script src="public/js/core.js" type="text/javascript">
<!DOCTYPE html><html><head><title>BLOG</title><script type="text/javascript">var HLINK = "localhost/Ronanversendaal_Dev/";</script><script type="text/javascript" src="public/js/core.js"></script><link rel="stylesheet" type="text/css" href="public/css/screen.css" /></head><body><section id="wrap"><div class="block">mawfacking doe het<br></div><pre>1Eerste PostTestondwerp</pre></section></body></html>
</script>

How do I get it to output just the intended html?

share|improve this question
    
you don't have a break in your switch statement. –  DevZer0 Jun 16 '13 at 9:44
    
Doh, didn't see that. But unfortunatly did'nt fix the issue –  Rodracover Jun 16 '13 at 10:05

1 Answer 1

up vote 0 down vote accepted

you can't get output by $post = require_once ('pages/homepage.php'), you should try following code for store output of file into variable. replace your code line

switch ($pageurl[0]){
    default               :  $post =   require_once ('pages/homepage.php');  break;
}

with

switch ($pageurl[0]){
    default               :  $post =   get_include_contents('pages/homepage.php');  break;
}

and here is get_include_contents function.

function get_include_contents($filename) {
    if (is_file($filename)) {
        ob_start();
        include $filename;
        return ob_get_clean();
    }
    return false;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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