0

My Javascript sends an array in JSON to dbinsert.php

function SendTableToPHP() {
  //Store HTML Table Values into Multidimensional Javascript Array Object
  var TableData;
  TableData = storeTblValues()

  function storeTblValues() {
    var TableData = new Array();
    $('#itemtable tr').each(function(row, tr) {
      TableData[row] = {
        "ritem": $(tr).find('td:eq(0)').text(),
        "rqnty": $(tr).find('td:eq(1)').text(),
        "rbale": $(tr).find('td:eq(2)').text(),
        "ramt": $(tr).find('td:eq(3)').text(),
        "rrmk": $(tr).find('td:eq(4)').text(),
      } //tableData[row]
    });

    TableData.shift(); // first row will be empty - so remove
    //console.log(TableData);
    return TableData;
  }

  var Data;
  Data = $.toJSON(TableData);
  console.log(Data);

  $.ajax({
    type: "POST",
    url: "dbinsert.php",
    data: "pTableData=" + Data,
    success: function(msg) {
      alert(msg) //return value stored in msg variable "success";
    } //success
  });
};

The output result also alerts showing the response from dbinsert.php: Output Showing json created

<?php
  // Unescape the string values in the JSON array
  $tableData = stripcslashes($_POST['pTableData']);

  // Decode the JSON array
  $tableData = json_decode($tableData,TRUE);

  // now $tableData can be accessed like a PHP array
  echo $tableData[1]['ritem'];
  echo $tableData[2]['ritem'];
  echo $tableData[3]['ritem'];

  $dat = $_POST['podat'];
  echo $dat;
?>

However the output is showing only date ($dat) and what i want to display array from that json. i need to insert data in database from that array thats why i want to check if array isdisplaying ok or not.

Here is the source code: https://www.fourfront.us/blog/store-html-table-data-to-javascript-array

  • 2
    It's not clear what the issue is. Your code only shows `$dat` because that's the only value you're echoing...? – Rory McCrossan Nov 03 '18 at 15:13
  • I want to decode and Display the data sent by other page in the form of json array. Means I want to display anything that is in the HTML table. I have intentionally coded DAT to show to check if php is okay – Debojit Deb Nov 03 '18 at 18:32
  • if you just want to check the data for debugging purposes then `var_dump($tableData);` will do the job quite happily. Or you could re-encode it as JSON again and output it that way. (And you could have researched that yourself in 20 seconds probably) – ADyson Nov 05 '18 at 09:26
  • Its showing in alert (means sending data to dbinsert) but i cant able to access that on dbinsert.php.. i mean if dbinsert is receiving that json or not i am not sure as i m unable to cast received data. if there any way to display the array data than i can retrieve the values from that array to insert in DataBase. dbinsert showing NULL against var_dump($tableData); means it is not getting anything?? – Debojit Deb Nov 05 '18 at 15:13

0 Answers0