3

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.

2 Answers 2

4

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.

var foo = '<?php include 'foo.php'; ?>';

becomes

var foo = 'This is the content of foo.php.';

after PHP is done with it.

If there are any quotes in foo.php, it may become this:

var foo = 'This is the 'content' of foo.php.';

which is invalid Javascript syntax. You'll need to escape any character of foo.php that may cause such invalid syntax, for example with addslashes. 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:

var foo = <?php echo json_encode("Some string with 'quotes'."); ?>;

Generating code in code is always tricky, try to not do it and stick to language neutral data interchange formats like JSON or XML.

Sign up to request clarification or add additional context in comments.

6 Comments

Do i need a special plugin to call JSON? Right now i'm using jQuery 1.4.4
@Sin JSON is Javascript Object Notation, it's valid Javascript adapted to be used as simple data interchange format. No, you don't need anything special to use it.
ok cool, one more question. what im trying to do, is fill this modal with a php file that holds some server side code in it, but is mostly html. Its been buggin the hell out of me, if include the file in my index for example and hide it, it shows up perfectly. But i only want it called when needed, to help speed things up. i tried .load, but it wont load pass the php, so thats why im trying this route
if i should be going this route in the first place to achieve what i want, really im to the point where i'll go with what ever works lol
@Sin You seem to be loading some massive amounts of data, which is indeed not very good. If you can't break it down and handle it via more native JS (again, data interchange, not HTML interchange), then some AJAX is probably the better way to go. Hard to say without details though, best to open a new question for it.
|
1

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.

3 Comments

hmmm, you know, I never thought about that. Does that apply to the contents of the included php file?
oh ok then, so thats a legal markup? the <?php?> inside the jquery? Just making sure im not leaving anything out
Oh yes definitely. Think about it. The page gets processed at the server. The server reads the php and outputs html (or whatever). Then it send the page to your browser which reads the js and html.

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.