【问题标题】:php echo codeigniter into javascript arrayphp echo codeigniter到javascript数组中
【发布时间】:2015-09-30 21:46:02
【问题描述】:

我的 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 中创建一个子行。

(有关我一直在尝试做的事情以及这些变量来自何处的更多信息,请参阅Datatables child row with PHP data from Codeigniter

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

【问题讨论】:

    标签: javascript php codeigniter


    【解决方案1】:

    看看你在做什么:

    <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); ?>;
    

    【讨论】:

      猜你喜欢
      • 2011-05-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多