What im trying to do, is use php include
within a jquery
append attribute. something like this:
$('a.popup[href^=#]').click(function() {
$('body').append('<div id="content" class="popup_block"><?php include( SITE_URL . 'activity/popup.php' ) ?></div>');
My script is in a php file, server side, so that i could accomplish this, but im not sure how to go about it. When it comes to html, css etc. I can combine it and php within the php file, but when it comes to javascript, its the quotes that confuses me, and when and how to use the brackets. This might sound confusing lol. Anyways, does CDATA have anything to do with it? I’ve never used it before, but I should atleast learn it’s use.
The PHP interpreter will only look for
<?php
and?>
tags and try to evaluate anything in between. It doesn’t care about surrounding quotes. You need to make sure though that the result of whatever PHP does is valid Javascript.becomes
after PHP is done with it.
If there are any quotes in
foo.php
, it may become this:which is invalid Javascript syntax. You’ll need to escape any character of
foo.php
that may cause such invalid syntax, for example withaddslashes
. This can be quite cumbersome though, so I’d advise to look for an alternative this to begin with.You can encode the value using JSON, which is definitely syntax safe:
Generating code in code is always tricky, try to not do it and stick to language neutral data interchange formats like JSON or XML.
If you are 100% sure you don’t have any single quotes in your include, there should be no problems with how you have it.
If you want to visualize it, copy all of your generated code from the included php file and paste it right into the main page inside of the
append()
. See how it looks. This will give you a good idea of what the browser will end up with.