How to send a table html as a serialize array to database?

Hi everybody.
I want to send my javascript and dynamic table to my database as a text variable.
I explained whatever I did:

  1. first of all I used jquery to get the values
  2. Then I stored HTML table values in a java script array
    3)I converted javascript array to JSON format
    4)I sent JSON array to a php script by JQuery AJAX
    But, I don’t know how to send it to database?
    Here is my code:
        function readTblValues()
            var TableData = '';

            $('#tbTableValues').val('');    // clear textbox
            $('#tblDowntimes tr').each(function (row, tr) {
                TableData = TableData
                        + $(tr).find('td:eq(1)').text() + ' '  // Task No.
                        + $(tr).find('td:eq(2)').text() + ' '  // Date
                        + $(tr).find('td:eq(3)').text() + ' '  // Description
                        + $(tr).find('td:eq(4)').text() + ' '  // Task
						+ $(tr).find('td:eq(5)').text() + ' '  // Task
                        + '\n';

        function storeAndShowTableValues()
            var TableData;
            TableData = storeTblValues();
            $('#tbTableValuesArray').html('<br>JS Array: <br>' + print_r(TableData));
        function storeTblValues()
            var TableData = new Array();

            $('#tblDowntimes tr').each(function (row, tr) {
                TableData[row] = {
                    "DOWNTIME": $(tr).find('td:eq(1)').text()
                    , "equipment": $(tr).find('td:eq(2)').text()
                    , "startdowntime": $(tr).find('td:eq(3)').text()
                    , "finishdowntime": $(tr).find('td:eq(4)').text()
					, "description": $(tr).find('td:eq(5)').text()
            TableData.shift();  // first row will be empty - so remove
            return TableData;

        function convertArrayToJSON()
            var TableData;
            TableData = $.toJSON(storeTblValues());
            $('#tbConvertToJSON').html('<br>JSON array: <br>' + TableData.replace(/},/g, "},<br>"));

        function sendTblDataToServer()
            var TableData;
            TableData = $.toJSON(storeTblValues());
            $('#tbSendTblDataToServer').val('JSON array to send to server: <br<br>' + TableData.replace(/},/g, "},<br>"));

                type: "POST",
                url: "test.php",
                data: "pTableData=" + TableData,    // post TableData to server script

                success: function (msg) {
                    // return value stored in msg variable 
                    $('#tbServerResponse').html('Server Response:<br><br><pre>' + msg + '</pre>');

        function print_r(arr, level) {
            var dumped_text = "";
            if (!level)
                level = 0;

            //The padding given at the beginning of the line.
            var level_padding = "";
            for (var j = 0; j < level + 1; j++)
                level_padding += "    ";

            if (typeof (arr) === 'object') { //Array/Hashes/Objects 
                for (var item in arr) {
                    var value = arr[item];

                    if (typeof (value) === 'object') { //If it is an array,
                        dumped_text += level_padding + "'" + item + "' \n";
                        dumped_text += print_r(value, level + 1);
                    } else {
                        dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
            } else { //Stings/Chars/Numbers etc.
                dumped_text = "===>" + arr + "<===(" + typeof (arr) + ")";
            return dumped_text;

and Here is my php code:

        function processJSONArray()
    $tableData = stripcslashes($_POST['pTableData']);
    $tableData = json_decode($tableData);
echo  processJSONArray();

Is there any suggestion?
Thanks in advance

You need to create a connection to your database then loop over your $tableData, performing an insert for each row. PDO is the preferred method for working with databases in PHP. This tutorial gives a good run through; have a go and come back here if you have any specific questions. It’s scarily long, but you only need the first dozen sections to get going.

1 Like

I don’t understand why.

A table shows tabular data that already exists, so there is no need to serialize or store it, because it already does, exist.

So why do you need to serialize it?

1 Like

Thanks dear experts for taking the time for the post.
To be honest , I want to send it to database for my targeted reports. That time, I have to left join downtime with another requirements. It should be serialized in database for my next targeted.
Also, I don’t understand why this error appears?
‘Undefined variable: tableData’
however, with aforementioned code, I manage to get the array in client side, but when I use this code that error is appeared (Undefined variable: tableData)
echo $tableData;
Thanks for your time and consideration

No it shouldn’t. You are heading in the wrong direction.

So, what is the solution to send it to database?

As I mentioned, you have the data, so you don’t need to serialize anything, you just have to query for it.

1 Like

Thanks for your reply,
Yes, I have the data, but in client side.
I want to send it to database. When I want to call it by php, this error is appeared for me
Undefined variable: tableData
I don’t know, why server can send it for client but, again, by php I can not call it. So, my query is not run.
How to solve it?
Thanks for your time

Does the user make changes to it after the database sends it to the client?

1 Like

Thanks again for your consideration
It has been solved

Sponsor our Newsletter | Privacy Policy | Terms of Service