我在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> <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> <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> <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> <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> <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> <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> <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将引号解释为{{1}的结尾}),我尝试用json_encode
和'"'
包围javascript数组的整个值,我尝试用"'"
围绕每一行,但也没有成功。如何将整个字符串转换为一个表单,其中javascript和PHP将正确解析它们并且它们都不会被吓坏?
答案 0 :(得分:0)
看看你在做什么:
<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); ?>;