I have an Dynamic HTML table I am simply calling the id using JavaScript, looping through the data and capturing the cell values.
Question- How do I take this data POST it to test save.php page to insert the entire table into my MySQl db?
var table = document.getElementById('table'),
rows = table.getElementsByTagName('tr'),
i, j, cells, OS_d,roleApp_d,Men_d,vcpu_d,Val_d,Per_d,hw_d ;
for (i = 1, j = rows.length; i < j; ++i) {
cells = rows[i].getElementsByTagName('td');
if (!cells.length) {
continue;
}
OS_d = cells[1].firstChild.value;
roleApp_d = cells[2].firstChild.value;
vcpu_d = cells[3].firstChild.value;
Men_d = cells[4].firstChild.value;
Val_d = cells[5].firstChild.value;
Per_d = cells[6].firstChild.value;
hw_d = cells[7].firstChild.value;
}
--- Section where assistance is required
$.ajax({
type: "POST",
url: 'Save.php',
data: ({data:OS_d, roleApp_d, vcpu_d, Men_d, Val_d, Per_d, hw_d}),
success: function(data) {
alert(data);
}
});
PHP Side Save.php
<?php
$test = $_POST['data'];
echo $test;
?>
Just quick code, not tested it so your post data should be like this:
var table = document.getElementById('table'),
rows = table.getElementsByTagName('tr'),
i, j, cells, OS_d,roleApp_d,Men_d,vcpu_d,Val_d,Per_d,hw_d ;
var p_row = '[';
for (i = 1, j = rows.length; i < j; ++i) {
cells = rows[i].getElementsByTagName('td');
if (!cells.length) {
continue;
}
p_row += "{";
p_row +='"OS_d":"'+cells[1].firstChild.value+'",';
p_row +='"roleApp_d":"'+ cells[2].firstChild.value+'",';
p_row +='"vcpu_d":"'+ cells[3].firstChild.value+'",';
p_row +='"Men_d":"'+ cells[4].firstChild.value+'",';
p_row +='"Val_d":"'+ cells[5].firstChild.value+'",';
p_row +='"Per_d":"'+ cells[6].firstChild.value+'",';
p_row +='"hw_d":"'+ cells[7].firstChild.value+'"';
p_row += '},';
}
p_row = (p_row).replace(new RegExp("[,]+$"), "");
p_row += ']';
var formData = new FormData();
formData.append('data',p_row);
fetch("/save.php", {
method: "POST",
body: formData
}).then(function (response) {
console.log(response);
}).catch(function (error) {
console.error(error, "code: " + error.status);
});
I figured it out so I decided to share to the community. Thanks for the support.
$.ajax({
type: "POST",
url: 'test.php',
data: {p_row : p_row },
success: function(data){
alert(data);
}
});
}
On the test.php side
<?php
$data = false;
if(isset($_POST['p_row'])){
$data = $_POST['p_row'];
}
echo 'Received Data: ' . $data ;
//$conn->close();
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.