Hi everybody.
I want to send my javascript and dynamic table to my database as a text variable.
I explained whatever I did:
- first of all I used jquery to get the values
- 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';
});
$('#tbTableValues').html(TableData);
}
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>"));
$.ajax({
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);
var_dump($tableData);
}
echo processJSONArray();
Is there any suggestion?
Thanks in advance