I'm trying to create a PHP form to a nonprofit organization and would like to send an email via SMTP using PHPMailer.
My problem is that the form contains a dynamic table, where I want to store/send data to a .php file via Ajax and JSON, but it doesn't fill the array with values (even if I type $(tr).find('td:eq(0)').val()
. The Ajax data also contains two addition values.
Also if I try to json_decode on the code below, the PHP breaks.
- I tried getting the data by variable, and it works.
- I tried to collect them in a single string which was converted to JSON, it didn't work.(The PHP doesn't send anything nor error feedbacks)
- Right now I'm trying to create multiple arrays from rows. But like 2. it doesn't work
The HTML body:
<html>....
<input id="name" class="form-control" required>
<input type="email" id="email"class="form-control" required>
<table id="dynamic_field">
<tr>
<td>
<textarea id="value1" class="form-control" required></textarea></td>
<td>
<textarea id="value2" class="form-control" required></textarea></td>
.....
</tr></table>
<input type="button" onclick="sendEmail()" value="Send &arr" class="btn btn-primary">
The Script:
function sendEmail() {
var name = $("#name");
var email = $("#email");
var value1 = $("#value1");
var value2 = $("#value2");
....
var TableData;
TableData = JSON.stringify(storeTblValues());
$.ajax({
url: 'sendEmail.php',
method: 'POST',
dataType: 'json',
data: {
name: name.val(),
email: email.val(),
value1: value1.val(),
value2: value2.val(),
TableData: TableData
}, success:...
function storeTblValues(){
var TableData = new Array();
$("#dynamic_field tr").each(function (row, tr) {
TableData[row] = {"value1": $(tr).find('td:eq(0)').text() , "value2": $(tr).find('td:eq(1)').text() , ......}
;});return TableData;
;}
I expect the output of the array TableData
to be [{"value1":"Whatever in Value 1","value2":"Whatever in Value 2 ",....}]
, but what I read from this string is [{"value1":"
","value2":"
",...}]