简体   繁体   English

用PHP和javascript包裹在php函数中的引号

[英]quotes with php and javascript wrapped in php function

Using jquery I do this: 使用jQuery我这样做:

$(function() {
$('#iButton').change(function() {
    $.ajax({
       url: 'index.php?option=com_cameras&task=globalmotiondetection&id_hash=<?php echo $id_hash; ?>&global_monitoring='+ (this.checked ? 1 : 0) + '&format=raw'

    });
});
});

This works great. 这很好。 But now I'm putting this into a php function (Joomla coding) but can't figure out the quotes: 但是现在我将其放入php函数(Joomla编码)中,但无法找出引号:

$doc->addScriptDeclaration('
$(function() {
$("#iButton").change(function() {
    $.ajax({
       url: "index.php?option=com_cameras&task=globalmotiondetection&id_hash='$id_hash'&global_monitoring="+ (this.checked ? 1 : 0) + "&format=raw"
    });
});

});
');

This gives me: Parse error: syntax error, unexpected T_VARIABLE on the url line. 这给了我:解析错误:语法错误,网址行上出现意外的T_VARIABLE。 Not sure how the quotes should look like. 不确定引号应如何。 I guess I can't just put $id_hash in there either (I'm guessing because of the error). 我猜我也不能只把$id_hash放在那儿(我猜是因为错误)。 Any ideas? 有任何想法吗?

To concatenate a variable into single-quoted string, you can use the concatenate operator . 要将变量连接为单引号字符串,可以使用concatenate运算符. :

$doc->addScriptDeclaration('
$(function() {
$("#iButton").change(function() {
    $.ajax({
       url: "index.php?option=com_cameras&task=globalmotiondetection&id_hash=' . $id_hash . '&global_monitoring="+ (this.checked ? 1 : 0) + "&format=raw"
    });
});

});
');

Alternatively, you can use a double-quoted string instead, which allows you to interpolate the variable right inside: 另外,您也可以使用双引号引起来的字符串,该字符串允许您在变量内插值:

$doc->addScriptDeclaration("
$(function() {
$('#iButton').change(function() {
    $.ajax({
       url: 'index.php?option=com_cameras&task=globalmotiondetection&id_hash=$id_hash&global_monitoring='+ (this.checked ? 1 : 0) + '&format=raw'
    });
});

});
");

To make it more simple, use Heredoc. 为了使其更简单,请使用Heredoc。 see http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc 参见http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

$str = <<<EOT
$(function() {
    $("#iButton").change(function() {
        $.ajax({
           url: "index.php?option=com_cameras&task=globalmotiondetection&id_hash='$id_hash'&global_monitoring="+ (this.checked ? 1 : 0) + "&format=raw"
        });
    });
});

EOT;

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

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