0

I'm making a website that has 3 documents. The Php document that has all html structure and script, the css document that has no interest in the question, and the javascript document.

I'm trying to input html contents of a javascript variable through .innerHTML statement. And to do that, I need (of course) to code the HTML content, and so I do the code within the javascript file itself. Like so:

document.getElementById("exp").innerHTML = "<div class=\"cell\">\
                                                <div class=\"project_cell\"><img src=\"img.png\"></div>\
                                                <div class=\"project_cell\">text</div>\
                                               </div>\";

And this works. However, the code is obviously not just this. It's a complex HTML structure that I do not wish to see in the javascript file. So I would like to put the HTML content to be inside this variable into a text file, using PHP, and make that variable on the innerHTML statement instead of all the code. My PHP file is like this.

<html>
    <head>
        <title>sample</title>

    <?php
        $filename = "thisishtml.txt";
        $doc = file_get_contents($filename);
    ?>

        <script src="javascriptfile.js" type="text/javascript"></script>

    </head>

    <body>
...call javascript function...

It seems ok to me, but if I do like:

document.getElementById("exp").innerHTML = "<?php echo $doc ?>"

It doesn't work. I've tried simple text and I've tried with a new project, and it works on a new project, but not in this one. What am I doing wrong? I've looked to many questions and tutorials and didn't help and that's why I'm looking for help in here.

Thank you very much for your time. I hope we could solve this.

1
  • document.getElementById("exp").innerHTML = <?php echo '"'.$doc.'"' ?>
    – nikodz
    Commented Oct 17, 2013 at 21:40

2 Answers 2

1

Chances are you need to escape the contents of $doc for JS output.

document.getElementById("exp").innerHTML = <?php echo json_encode($doc) ?>;
1

If your javascript code is in a javascript file, PHP does not process that so you cannot execute PHP code there.

So you can either put the javascript code directly into the PHP page

<script type="text/javascript">
function someFunction()
{
    document.getElementById('exp').innerHTML = "<?php echo $doc ?>";
}
</script>

or you can do the follow:

<script type="text/javascript">
var doc = "<?php echo $doc ?>";
</script>

Then in the javascript file do:

function someFunction()
{
    document.getElementById('exp').innerHTML = doc;
}
3
  • This seems like it's a really good answer but for some reason I'm not getting it right this way. The path is correct and I'm sure the variable has it's content because when I do "echo $doc" on the php part, it appears on the screen. But I'm not getting to achieve how to link that variable and put it's content inside javascript the right way.
    – Telmo Vaz
    Commented Oct 17, 2013 at 21:54
  • Ok to be honest, I would recommend just loading this text via ajax. Use jQuery (or some other library) to do this quickly and easily.
    – Populus
    Commented Oct 18, 2013 at 14:44
  • Also you could try this: document.getElementById('exp').innerHTML = "<?php echo addslashes($doc) ?>";
    – Populus
    Commented Oct 18, 2013 at 14:45

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.