1

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);
dda
  • 6,030
  • 2
  • 25
  • 34
Hernanda
  • 57
  • 7
  • Help us help you. Please format your code better. It is all in 1 line and very difficult to read that way. – ryantxr Sep 02 '17 at 16:43
  • `$arr = json_decode($_POST['pdf-content']);` instead of those two lines in your PHP file. You already converted to JSON your products list, no need to convert it again, only decode left. – Anis Alibegić Sep 02 '17 at 16:48
  • What this line `console.log('#pdf-content: ' + $('#pdf-content').val());` outputs in the console? – Anis Alibegić Sep 02 '17 at 16:49
  • @Spectarion that's correct. I just wrongly put in that one. The result is the same as my example output [....] – Hernanda Sep 02 '17 at 16:51
  • Possible duplicate of [How do I extract data from JSON with PHP?](https://stackoverflow.com/q/29308898/218196) – Felix Kling Sep 02 '17 at 16:54

0 Answers0