I created a HTML Table using JavaScript dynamically.
So, the number of rows are not fixed. If I click add row button, it will genarate one new row. The following HTML and JavaScript code will generate dynamic table.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('[data-toggle="tooltip"]').tooltip();
var actions = $("table td:last-child").html();
// Append table with add row form on add new button click
$(".add_new").click(function(){
var index = $("table tbody tr:last-child").index();
var row = '<tr>' +
'<td><input type="text" name="fname" class="form-control" ></td>' +
'<td><input type="text" name="lname" class="form-control" ></td>' +
'</tr>';
$("table").append(row);
$('[data-toggle="tooltip"]').tooltip();
});
// Add row on add button click
$(document).on("click", ".add", function(){
var empty = false;
var input = $(this).parents("tr").find('input[type="text"]');
input.each(function(){
if(!$(this).val()){
$(this).addClass("error");
empty = true;
} else{
$(this).removeClass("error");
}
});
$(this).parents("tr").find(".error").first().focus();
if(!empty){
input.each(function(){
$(this).parent("td").html($(this).val());
});
}
});
// Delete row on delete button click
$(document).on("click", ".delete", function(){
$(this).parents("tr").remove();
$(".add_new").removeAttr("disabled");
});
});
</script>
</head>
<body>
<form action="display_table.php" id="my_form" name="my_form" method="post" >
<table id='userTable' name='userTable'>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="text" name="fname"></td>
<td><input type="text" name="lname"></td>
</tr>
</tbody>
</table>
<input type="button" id="add_new" name="add_new" class="add_new" value="Add New Row" >
<input type="submit" name="save" value="Submit">
</form>
</body>
</html>
Now, If I click the submit button, then the Table Data should be sent to the display_table.php page.
display_table.php
<?php
$user_table=$_POST['userTable'];
echo $user_table;
#Next task is to process the table and store into MySQL Database
?>
If the table data is received to display_table.php then I'll process the data to store into MySQL Database.
I need help to send the HTML-JavaScript Table to display_table.php
$_POST['userTable']won't exist since there's not form element with that name. Tables aren't posted since it isn't a valid form element. You need to access the input fields:$_POST['fname']etc. Also, if you add multiple inputs with the same name, you need to pass them as arrays by naming them:name="fname[]"(notice the[]after the name). Then$_POST['fname']will be an array with all the inputs with that name.<td><input type="text" name="fname[]"></td>and if I change the PHP code like this<?php $fname=$_POST['fname']; ?>Then in PHP code the variable$fnameis an array ?