I have a web app that has a form which contains productType, productName and productPrice in a single form-input-group. When I click the add button, all that form input data will be displayed on table List and also inserted on an array using JS (jQuery).
That array (listProduct) value will be used to fill a hidden input on another form (the form that I use on source code below). And that hidden input named pdf-content will be used as a content of my pdf maker controller.
But in here I just want to know how to access and manipulate that data. I use JSON.stringify
, then I use the json_decode()
function, but the result is always (for example in here I add 2 lists) ["productType1, product1, xxxxx", "productType2, anotherName, xxx"]
(that's just an example). I don't know how to handle that. Actually, I have an idea to convert it as an array using PHP but that doesn't work. I want to access that data for a table-view pdf page.
Form
<!-- 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
// controller method: index.php/ctest/makePDF
$arr = json_encode($_POST['pdf-content']);
$arr1 = json_decode($arr);