每天用javascript包含不同的php文件

I'm want to show every day different content. Just plain text or a php echo is working fine, but when I trying to include a php file or load an image etc. my script stops to work.

My code so far:

<div id="box-wrapper"></div>

<script>
    function myFunction() {
        var d = new Date();
        var weekday = new Array(7);
        weekday[0] = "This is working well.";
        weekday[1] = "<?php echo 'This is also working well.' ?>";
        weekday[2] = "Tuesday";
        weekday[3] = "Wednesday";
        weekday[4] = "Thursday";
        weekday[5] = "Friday";
        weekday[6] = "Saturday";

        var n = weekday[d.getDay()];
        document.getElementById("box-wrapper").innerHTML = n;
    }
</script>

Any ideas how I can realize that? We don't have somebody in the company who has very profund knowledge in this topic and I'm just learning by doing.

Greetings and thanks.

Edit: Sorry for being unclear. For example if I include a file on wednesday and a file on thursday both are showing, but thenmy page is messed up and also shows some plain text like:

*"; weekday[5] = "Friday"; weekday[6] = "Saturday"; var n = weekday[d.getDay()]; document.getElementById("box-wrapper").innerHTML = n; } –*

I'm guessing your included PHP file generates " which will interfer with the javascript " delimiting the strings. You certainly need to escape those " using \"

So that:

weekday[4] = "My string with "  a double quote";

becomes

weekday[4] = "My string with \"  a double quote";

Your PHP echo is only working because it's producing exactly the same thing as your regular text.

PHP is processed on the server, before the browser even sees your JavaScript. So this:

weekday[1] = "<?php echo 'This is also working well.' ?>";

Simply becomes this:

weekday[1] = "This is also working well.";

Your browser doesn't even know the difference.

When you try to include another file, the contents of the file are being output into that JavaScript string, too. You should be able to see that by using your browser's View Source tool. If you scroll down to your JavaScript you'll see something like this:

weekday[4] = "[file contents]";

The file probably contains quotation marks. When your browser encounters the quotation marks in the string, it assumes the string is ended. It makes your JavaScript invalid.

You probably shouldn't use JavaScript for this. Instead, you should do all of this on the server with PHP.

For example, it could look like this:

$day = date('l'); // That gets the name of today.  http://php.net/date
switch ($day) {
    case 'Sunday':
        echo 'This is working well.';
        break;
    case 'Monday':
        echo 'This is also working well.';
        break;
    case 'Tuesday':
        echo 'Tuesday';
        break;
    case 'Wednesday':
        include 'filename.php';
        break;
    // etc.
}

That's just one approach. You could also use an array like you did in your JavaScript, but that would make it harder to include other PHP files.

If you’re already using PHP, why clutter your page with JavaScript?

<?php
$day = getdate('wday');
$files = array(
    'days/sunday.php',
    'days/monday.php',
    'days/tuesday.php',
    'days/wednesday.php',
    'days/thursday.php',
    'days/friday.php',
    'days/saturday.php'
);
?>

<div><?php include $files[$day]; ?></div>

If you really want to use JavaScript, go use AJAX.

var day = new Date().getDay(),
    req = new XMLHttpRequest();

req.open('GET', 'my-dynamic-file.php?day=' + day, true);
req.onload = function() {
    document.body.innerHTML = req.responseText;
};

req.send();