Questions Re: multiple INSERT statements to server PHP I have some questions regarding coding of multiple INSERT statements going to my server.
I have some questions regarding coding of multiple INSERT statements going to my server.
My code is based on an example I found in fiddle. Here is the code and
my questions in bold.
I'm trying to learn how this all works so any hints, explanations will be appreciated!!
Code below will run.
PHP Code:
- <?php
// Fri May 28, 2021 14:30
// multiAdd.php
// to serve multiInsert.html
set_include_path( '../include' );
error_reporting (E_ALL ^ E_NOTICE);
print_r($_POST);
echo json_encode($_POST); // Returns Json data - OK
?>
HTML Code:
- <!DOCTYPE html>
<!-- multiInsert.html -->
<!-- Adapted from http://jsfiddle.net/sperske/8e5ae/10/ -->
<html>
<head><title>testing multi insert function multiInsert.html 28May2021</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="../jquery/jquery-2.1.4.js"></script>
<style>
tbody#template {
display: none;
}
body {
font-family: roman, 'times new roman', times, serif;
font-size: 14pt;
margin: auto;
text-align: center;
background-color: #6795c8;
}
input {
font-size: 12pt;
color: blue;
}
</style>
</head> <!--=================== End <head> ===================== -->
<body>
<table id="tabledata">
<thead>
<th>Id</th>
<th>Vendor</th>
<th>OrderNo</th>
<th>DrugNo</th>
<th>Description</th>
<th>Cost</th>
<th>Refills</th>
</thead>
<tbody id="input"></tbody>
<tbody id="template">
<tr>
<td>
<input name="Id" type="text" />
</td>
<td>
<input name="Vendor" type="text" />
</td>
<td>
<input name="OrderNo" type="text" />
</td>
<td>
<input name="DrugNo" type="text" />
</td>
<td>
<input name="Description" type="text" />
</td>
<td>
<input name="Cost" type="text" />
</td>
<td>
<input name="Refills" type="text" />
</td>
</tr>
</tbody>
</table>
<button id="ActionAddRow">Add Row</button>
<button id="ActionSubmit">Submit</button>
<script type="text/javascript">
$(function () {
var addInputRow = function () {
$('#input').append($('#template').html());
};
addInputRow();
$('#ActionAddRow').on('click', addInputRow);
$('#ActionSubmit').on('click', function () {
var mydata = $('#input tr').map(function () {
var values = {};
$('input', $(this)).each(function () {
if (this.type === 'checkbox') { // org fiddle code
values[this.name] = this.checked; // ditto
} else {
values[this.name] = this.value;
}
});
return values; // to what ? - is this for ".get() ?"
})
.get(); //What does this do - with no parameters - does not work without this , however!
$.post('multiAdd.php', {
mydata,
//json: JSON.stringify(data), Original fiddle code
//delay: 1 ditto
})
- This does not work - how to fix ?? Use .get() above $.ajax??
/* $.ajax({
type: "POST",
url: "multiAdd.php",
data: mdata,
dataType: "json",
}) // ajax
*/
.done(function (response) {
alert("POST success");
console.log(response);
});
});
});
</script>
</body>
</html>