将PHP集成到JavaScript中,从后端获取价值


Integration of PHP into JavaScript to take value from Backend

所以我要做的是从Backend(CMS就像wordpress,只是我的CMS是一个自定义开发的CMS)获取一个输入值,并在javascript代码中显示该值。

从后端代码获取值:

<?php 
   $effects_array=array( "sliceDown","sliceDownLeft", "sliceUp", 
                        "sliceUpLeft","sliceUpDown","sliceUpDownLeft",
                        "fold","fade","random","slideInRight",
                        "slideInLeft","boxRandom","boxRain","boxRainReverse",
                        "boxRainGrow","boxRainGrowReverse");
   foreach ($effects_array as $effects_key => $effect_value) {
?>
<label class="inline"><input type="radio" value="<?php echo $effect_value ?>" class="input-xxlarge" name="theme_options[effects_select]" <?php  if (isset($SITE['tmp']['dataList']->effects_select)){ ?> checked <?php } ?> ><?php echo $effect_value ?></label>
<?php
    }
?>

我试图集成的代码:

$(window).ready(function() {
    $('#slider').nivoSlider({
        effect: '<?php echo $SITE['theme_options']['effects_select']->$effects_value ?>', 

我需要从effects_array到效果的值

这是我在检查元件时遇到的错误

    $(window).ready(function() {
        $('#slider').nivoSlider({
            effect: '<br />
<b>Fatal error</b>:  Cannot use object of type stdClass as array in <b>C:'xampp'htdocs'limetraycms'themes'amalfi_latest'home.tpl.php</b> on line <b>135</b><br />

其中一个$SITE['theme_options']['effects_select']不是数组,而是对象。您可以打印_r($SITE)来查看它是什么类型。或者只需尝试其中一种:

  • $SITE->theme_options['effects_select']
  • $SITE['tem_options']->effects_select
  • $SITE->theme_options->effects_select

看看其中一个错误是否会消失。

看起来引号可能都是effect:行中的单引号。

试试这个:

// using double quotes on the outside
$(window).ready(function() {
    $('#slider').nivoSlider({
        effect: "<?php echo $SITE['theme_options']['effects_select']->$effects_value ?>",

或者这个:

// escaping the single quotes inside the outer single quotes
$(window).ready(function() {
    $('#slider').nivoSlider({
        effect: '<?php echo $SITE[''theme_options''][''effects_select'']->$effects_value ?>',