Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
137
rated 0 times [  141] [ 4]  / answers: 1 / hits: 6454  / 11 Years ago, wed, december 25, 2013, 12:00:00

What is the correct way of sending a HTML table to the PHP script via AJAX, please? My table has 10 rows and three four columns. The table is not too large but still it could be too much for one AJAX call.



So here are my questions:




  • Should the whole table content be sent via one AJAX request?

  • Should I send the table content per table rows - 10 rows would be 10 AJAX calls?

  • Should I encode the table values into JSON?



If you have any directions to correctly submitting tables to PHP script via AJAX, that would really help.



I use pure JavaScript and no JQuery.



Thank you in advance.






UPDATE - SOLUTION:



For those, who are interested in fully working solution, here is the full sotuion that works well for me.



JavaScript:



    // submit the table to the PHP script for processing
BlacklistTable.prototype.submit = function()
{
var that = this;

// get table ID
var elementTable = document.getElementById('copyrightBlacklistTable');

// create JSON object
var jObject = [];

// iterate through the table
// rows
for (var i = 0; i < elementTable.rows.length; i++)
{
// create array within the array - 2nd dimension
jObject[i] = [];

// columns within the row
for (var j = 0; j < elementTable.rows[i].cells.length; j++)
{
jObject[i][j] = elementTable.rows[i].cells[j].innerHTML;
}
}

var JSONObject = encodeURIComponent( JSON.stringify(jObject));

var url = PHP/updateBlacklist.php;
var requestData = &blacklistObject= + JSONObject;

var XMLHttpRequestObj = FileResort.Utils.createRequest();
XMLHttpRequestObj.open(POST, url, true);
XMLHttpRequestObj.setRequestHeader(Content-Type, application/x-www-form-urlencoded);
XMLHttpRequestObj.send(requestData);

// process return message
XMLHttpRequestObj.onreadystatechange = function (event)
{
if (XMLHttpRequestObj.readyState == 4 && XMLHttpRequestObj.status == 200)
{
alert(XMLHttpRequestObj.responseText);
var responseJSON = eval('(' + XMLHttpRequestObj.responseText + ')');

if (responseJSON.result == true)
{
console.log(Success processing Blacklist Object);
}
else
{
console.log(Error processing Blacklist Object);
}
}
}

};


PHP Script:



// debugging library
include 'ChromePhp.php';

// connect to the database
require_once 'mysqlConnect.php';

// get variable values stored in $_POST
$blacklistObjectJSON = $_POST['blacklistObject'];

//$data = json_decode($blacklistObjectJSON, true);
$data = json_decode($blacklistObjectJSON, false);

// testing accessing the imported table values
echo $data[0][0];

// close database connection
mysqli_close($dbc);

$array = array(result => true);
echo json_encode($array);

More From » php

 Answers
4

i think best way use JSON. you can do something like this



function getJson(){
var table = document.getElementById('mytable');
var tr = table.getElementsByTagName('tr');
var jObject = {}
for (var i = 0; i < tr.length; i++){
var td = tr[i].getElementsByTagName('td');

for (var j = 0; j < td.length; j++){
jObject['table_tr' + i + '_td_' + j] = td[j].innerHTML;
}
}
return jObject;
}


demo


[#49229] Tuesday, December 24, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kourtney

Total Points: 368
Total Questions: 103
Total Answers: 85

Location: Bonaire
Member since Sat, May 1, 2021
3 Years ago
kourtney questions
Sun, Oct 4, 20, 00:00, 4 Years ago
Tue, Oct 29, 19, 00:00, 5 Years ago
Thu, Apr 4, 19, 00:00, 5 Years ago
Fri, Mar 1, 19, 00:00, 5 Years ago
;