简体   繁体   English

使用PHP变量并在Jquery / Ajax页面加载请求中使用它

[英]Using PHP variable and use it in Jquery/Ajax page load request

Let's say I have set up a PHP variable like this: 假设我已经设置了一个像这样的PHP变量:

$phpurl = $_GET["url"] 

where url value will be from GET variable query. 其中url值来自GET变量查询。 The value of "url" will be some sort of html page link with some content like " myContent.html " I want to load. “url”的值将是某种html页面链接,其中包含一些内容,例如我想加载的“ myContent.html ”。

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? 我如何获得这个“url”值,我已将其分配给变量“ $phpurl ”并在Ajax / Jquery页面加载请求中使用它?

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

Hope the question is clear. 希望问题很清楚。 I am pretty new into programming. 我很喜欢编程。 Thanks. 谢谢。

EDIT: 编辑:

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

will do 会做

You'll want to take precaution to escape the value properly 您需要采取预防措施以正确地逃避价值

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

Or you could try something like https://github.com/allmarkedup/jQuery-URL-Parser 或者你可以尝试像https://github.com/allmarkedup/jQuery-URL-Parser这样的东西

// requires no PHP at all!
var url = $.url(window.location).attr('url');
$('.content').load(url);

As a generic rule you should properly escape variables when you move them between two realms, in this case from PHP to JavaScript. 作为通用规则,当您在两个领域之间移动变量时,您应该正确地转义变量,在本例中是从PHP到JavaScript。

This is especially true if you don't have full control over the variable contents, such as those coming from $_GET , $_POST , etc. 如果您无法完全控制变量内容,例如来自$_GET$_POST等的变量内容,则尤其如此。

This is a safe bet, using json_encode() to form a proper JavaScript value: 这是一个安全的赌注,使用json_encode()来形成一个合适的JavaScript值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM