我有一个网络应用,其形式在单个表单输入组中包含productType,productName和productPrice。当我单击添加按钮时,所有表单输入数据将显示在表列表中,并使用JS(jQuery)插入到数组中。
该数组(listProduct)值将用于填充另一个表单上的隐藏输入(我在下面的源代码中使用的表单)。名为 pdf-content 的隐藏输入将用作我的pdf制作者控制器的内容。
但在这里我只想知道如何访问和操作这些数据。我使用JSON.stringify
,然后我使用json_decode()
函数,但结果总是(例如在这里我添加2个列表)["productType1, product1, xxxxx", "productType2, anotherName, xxx"]
(这只是一个例子)。我不知道如何处理。实际上,我有一个想法,使用PHP将其转换为数组,但这不起作用。我想访问表格视图pdf页面的数据。
表格
<!-- form for sending the content from hidden input which contains all of product's data -->
<?php echo form_open(base_url().'index.php/ctest/makePDF', array('role'=>'form', 'name'=>'form-process', 'method'=>'POST')); ?>
<input type="hidden" name="pdf-content" id="pdf-content" />
<button type="submit" id="process" class="btn btn-success">
<span class="icon-step-forward"></span>
Process
</button>
</form>
的jQuery
// adding new content data to the table via jQuery
var listProduct = new Array();
$('#addContent').on('click', function() {
var productType = $('select[name="product_type"]').val();
var productName = $('input[name="product2"]').val();
var productPrice = $('input[name="price"]').val();
var dataList = '<tr> ' + '<td>' + Product Type + '</td>' + '<td>' + Product Name + '</td>' + '<td>' + Price + '</td>' + '</tr>';
$('#product-list').append(dataList);
listProduct.push(productType + ',' + ProductName + ',' + productPrice);
console.log('listProduct: ' + listProduct);
$('#pdf-content').val(JSON.stringify(listProduct));
console.log('#pdf-content: ' + $('#pdf-content').val());
});
控制器
// controller method: index.php/ctest/makePDF
$arr = json_encode($_POST['pdf-content']);
$arr1 = json_decode($arr);