3

Let's say I have set up a PHP variable like this:

$phpurl = $_GET["url"] 

where url value will be from GET variable query. The value of "url" will be some sort of html page link with some content like "myContent.html" I want to load.

How can I get this "url" value, which I have assigned to the variable "$phpurl" and use it in the Ajax/Jquery page load request?

$('.content').load('  ** need the value of "$phpurl" to be placed here ** ');

Hope the question is clear. I am pretty new into programming. Thanks.

4

3 回答 3

4

编辑:

$('.content').load('<?php echo json_encode($phpurl); ?>');

会做

于 2012-12-27T06:28:54.273 回答
3

您需要采取预防措施正确转义该值

<script type="text/javascript>
  var url = decodeURIComponent('<?php echo rawurlencode($phpurl) ?>');
</script>

或者您可以尝试类似https://github.com/allmarkedup/jQuery-URL-Parser

// requires no PHP at all!
var url = $.url(window.location).attr('url');
$('.content').load(url);
于 2012-12-27T06:37:13.143 回答
2

作为一般规则,当您在两个领域之间移动变量时,您应该正确地转义变量,在本例中是从 PHP 到 JavaScript。

如果您无法完全控制变量内容,例如来自 , 等的内容,则尤其$_GET如此$_POST

这是一个安全的选择,json_encode()用于形成正确的 JavaScript 值:

$('.content').load(<?php echo json_encode($phpurl); ?>);
于 2012-12-27T06:37:02.813 回答