2012-12-07 37 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' 
    }); 
}); 

}); 
");