简体   繁体   English

php变量与javascript的串联

[英]Concatenation of php variable with javascript

var settings = {
  "async": true,
  "crossDomain": true,
  "url": 'https://example.com/something.aspx?i='<? echo urlencode($_GET['id']); ?>,
  "method": "GET",
  "headers": {
    "cache-control": "no-cache",
  }
}

It doesn't work this way, the concatenation is wrong I think. 这样行不通,我认为连接是错误的。 Tried few ways still doesn't work. 尝试了几种方法仍然无效。

You need to put the data inside the JavaScript string literal. 您需要将数据放入 JavaScript字符串文字中。 Move the ' to after the extra data you are outputting. '移到要输出的多余数据之后。

You just had the single quote the wrong side. 您只是单引号有错误的一面。

Don't forget you're outputting to HTML, so you don't have to concatenate a PHP variable to a JavaScript variable. 不要忘记您正在输出到HTML,因此您不必将PHP变量连接到JavaScript变量。

var settings = {
  "async": true,
  "crossDomain": true,
  "url": 'https://example.com/something.aspx?i=<?php echo urlencode($_GET['id']); ?>',
  "method": "GET",
  "headers": {
    "cache-control": "no-cache",
  }
}

Maybe you can try it the other way round. 也许您可以反过来尝试。 Like 喜欢

<?php
  echo 'var settings = {
    "async": true,
    "crossDomain": true,
    "url": "https://example.com/something.aspx?i='.urlencode($_GET['id']).'",
    "method": "GET",
    "headers": {
      "cache-control": "no-cache",
    }
  }'; 
?>

Please note the changes I made with the ' and " in the url attribute. 请注意,我对url属性中的'和'所做的更改。

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

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