PHP返回codeigniter到javascript数组


php echo codeigniter into javascript array

我在codeigniter视图中有这段代码:

<script>
    var content = [];
    content[<?php echo $storageItem["id"]; ?>] = "<?php echo form_open("/account/edititem", array("class" => "form-inline"), array("id" => $storageItem["id"], "item_loc" => "inventory", "acctid" => $acct_data->account_id)); ?>
            <div class="panel-body">
                <div class="row">
                    <div class="col-xs-2">
                        <strong>Refine level:</strong>&nbsp;<input type="number" name="refine" class="form-control" value="<?php echo $storageItem["refine"]; ?>" <?php if ($storageItem["type"] != 4 && $storageItem["type"] != 5) { echo "readonly"; } ?> />
                    </div>
                    <div class="col-xs-2">
                        <strong>Broken?:</strong>&nbsp;<input type="checkbox" name="attribute" class="form-control" value="1" <?php if ($storageItem["attribute"] == 1) { echo "checked"; } if ($storageItem["type"] != 4 && $storageItem["type"] != 5) { echo "disabled"; } ?> />
                    </div>
                    <div class="col-xs-2">
                        <strong>Bound?:</strong>&nbsp;<input type="checkbox" name="bound" class="form-control" value="1" <?php if ($storageItem["bound"] == 1) { echo "checked"; } ?> />
                    </div>
                </div>
                <br />
                <div class="row">
                    <div class="col-xs-2">
                        <strong>Card 1:</strong>&nbsp;<input type="number" name="card0" class="form-control" value="<?php echo $storageItem["card0"]; ?>" <?php if ($storageItem["type"] != 4 && $storageItem["type"] != 5) { echo "readonly"; } ?> /></br>
                    </div>
                    <div class="col-xs-2">
                        <strong>Card 2:</strong>&nbsp;<input type="number" name="card1" class="form-control" value="<?php echo $storageItem["card1"]; ?>" <?php if ($storageItem["type"] != 4 && $storageItem["type"] != 5) { echo "readonly"; } ?> /></br>
                    </div>
                    <div class="col-xs-2">
                        <strong>Card 3:</strong>&nbsp;<input type="number" name="card2" class="form-control" value="<?php echo $storageItem["card2"]; ?>" <?php if ($storageItem["type"] != 4 && $storageItem["type"] != 5) { echo "readonly"; } ?> /></br>
                    </div>
                    <div class="col-xs-2">
                        <strong>Card 4:</strong>&nbsp;<input type="number" name="card3" class="form-control" value="<?php echo $storageItem["card3"]; ?>" <?php if ($storageItem["type"] != 4 && $storageItem["type"] != 5) { echo "readonly"; } ?> /></br>
                    </div>
                </div>
            <?php echo form_close(); ?>
        </div>";
</script>

我在javascript中创建了名为'content'的数组,然后需要将几乎整个表单填入其中,以便能够在DataTables中创建子行。

(有关我一直在尝试做什么以及这些变量来自何处的更多信息,请参阅Codeigniter的PHP数据的数据表子行)

我试过转义单引号和双引号(PHP抱怨这一点),json_encode (PHP也抱怨这一点,因为我仍然需要引号和PHP解释引号作为json_encode的结束),我试过用'"'"'"包围javascript数组的整个值,我也试过用' '+包围每一行,但没有成功。如何将整个字符串转换成javascript和PHP都能正确解析的形式?

看看你在做什么:

<script>
    var content = [];
    content[<?php [..snip..] ?>] = "<?php [..snip..] ?>
                                   ^---start of javascript string
            <div class="panel-body">  
                       ^---end of javascript string
                                  ^---start of another string

这意味着你基本上有:

variable = "some string stuff"variable-variable"more string stuff"

是完全非法的JS.

具体的修复方法是ESCAPE html中的所有字符串:

<script>
    yadayada
    <div class='"panel-body'"> yada yadayada
               ^-----------^

但这并不能改变这是一段非常糟糕的代码的事实。你不应该将html加载到JS字符串中,也不应该将PHP中的文本直接回显到JS上下文中。

如果没有别的,为什么不在PHP中生成所有内容,然后将其全部转储为json?

<?php
$data = "blah blah blah blah";
?>
<script>
var content = <?php echo json_encode($data); ?>;