在jquery中使用php include [英] using php include in jquery

查看:56
本文介绍了在jquery中使用php include的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试做的是在 jquery append属性中使用php include 。类似这样的事情:

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>');

我的脚本位于php文件,服务器端,所以我可以完成这个,但我不知道如何去做。当涉及到html,css等我可以在php文件中将它和php结合起来,但是当谈到javascript时,它的引号会让我困惑,何时以及如何使用括号。这可能听起来令人困惑大声笑。无论如何,CDATA与它有什么关系吗?我以前从未使用它,但我应该至少学习它的使用。

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.

推荐答案

PHP解释器只会查找<?php ?> 标签并尝试评估两者之间的任何内容。它不关心周围的引号。你需要确保whatev的结果呃PHP确实是有效的Javascript。

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'; ?>';

变为

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

如果 foo.php 中有任何报价,它可能会变为:

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

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

这是无效的Javascript语法。您需要转义任何可能导致此类无效语法的 foo.php 字符,例如 addslashes 。这可能非常麻烦,所以我建议首先寻找替代方案。

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.

您可以使用JSON对值进行编码,这绝对是语法安全的:

You can encode the value using JSON, which is definitely syntax safe:

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

在代码中生成代码总是很棘手,尝试不这样做并坚持语言中立的数据交换格式像JSON或XML。

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

这篇关于在jquery中使用php include的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆