So I've got this PHP that's writing some JavaScript that looks something like this:

echo '<script type="text/javascript">

var videoArr=["'.implode('", "', $videos).'"];
var titleArr=["'.implode('", "', $titles).'"];
var explanationArr=["'.implode('", "', $explanations).'"];
var i = 0;

function appendVideo(i) {

var url ="http://www.youtube.com/embed/" + videoArr[i] + "?rel=0&amp;autohide=1&amp;showinfo=0";

$("#stage").append("<div class=\"video\">");
$("#stage").append(titleArr[i] + "<br />");
$("#stage").append("<iframe width=\"400\" height=\"225\" src=\"" + url + "\" frameborder=\"0\" allowfullscreen></iframe>");
$("#stage").append("<br /><img src=\"images/btn_play.png\" onclick=\"play(\'" + videoArr[i] + "\')\" />");
$("#stage").append("<img src=\"images/btn_show.png\" onclick=\"show(\'" + videoArr[i] + "\')\" /><br />");
$("#stage").append("</div>");
}

</script>';

But after reading this: http://littleurl.info/wdl I've learned that I have to do this all in one append for my DIV tags to appear correctly. However I'm finding it impossible to get my syntax correct because I'm doing this all through PHP. Can anyone help me debug the code below? Please remember it's all contained in an echo with single inverted commas.

$("#stage").append("<div class=\"video\">"+ titleArr[i] + "<br />
<iframe width=\"400\" height=\"225\" src=\"" + url + "\" frameborder=\"0\" allowfullscreen></iframe><br />
<img src=\"images/btn_play.png\" onclick=\"play(\'" + videoArr[i] + "\')\" />
<img src=\"images/btn_show.png\" onclick=\"show(\'" + videoArr[i] + "\')\" /><br />
</div>");
share|improve this question

Single-quotes are valid for HTML attributes. That will at least get rid of some of the horrible escaping. You might also want to look into using a JS templating language to make this easier. – Andrew Marshall Jul 16 at 0:19
1  
You have PHP that writes JavaScript that writes HTML... there must be a better way. – sachleen Jul 16 at 0:19
3  
You're making this harder on yourself by mixing everything up in PHP. Either break out of the PHP block and embed the php itself, like <?php echo $var; ?> or use heredoc syntax. Also see output buffering if you need to assign the output to a variable. – Wesley Murch Jul 16 at 0:20
Look at your code highlighting on the question above; something is not right. – Jared Farrish Jul 16 at 0:20
@sachleen It's a necessary evil I'm afraid. – AzzyDude Jul 16 at 0:22
show 1 more comment
feedback

1 Answer

up vote 0 down vote accepted

Try saving your HTML/Javascript in variables in PHP using heredoc syntax, so you don't need to escape everything:

<?php

$javascript_part_1 = <<<END_SCRIPT

    <script>
    ...

END_SCRIPT;

$javascript_part_2 = <<<END_SCRIPT

    ...

END_SCRIPT;

$javascript_part_3 = <<<END_SCRIPT

    ...
    </script>

END_SCRIPT;

?>

Then, simply use echo with PHP's string concatenation:

echo $javascript_part_1 . $videos . $javascript_part_2 . $titles . $javascript_part_3;
share|improve this answer
Found out where the syntax problem was. But yeah, this would be a good way to avoid confusion. – AzzyDude Jul 16 at 0:37
Just a little correction to your pseudo code: the closing of heredoc sections allows no characters on the left (incl. spaces, tabs etc.) – inhan Jul 16 at 0:50
@inhan Good point. I only put the whitespace for readability. – Alex W Jul 16 at 0:54
feedback

Your Answer

 
or
required, but never shown
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.