2012-12-07 36 views
0

使用jQuery我这样做:报价与PHP和JavaScript包裹在PHP函数

$(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' 

    }); 
}); 
}); 

这个伟大的工程。但现在我把这个变成一个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" 
    }); 
}); 

}); 
'); 

这给了我:解析错误:语法错误,意想不到的T_VARIABLE在URL上线。不知道报价应该如何。我想我不能只把$id_hash在那里(我猜是因为错误)。有任何想法吗?

回答

0

要连接一个变量为单引号字符串,可以使用连接操作符.

$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" 
    }); 
}); 

}); 
'); 

或者,你可以使用一个双引号字符串,它可让你插右里面的变量:

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

}); 
");