I am working on this table in which there will be 10 different projects (Jupiter, Uranus, Saturn, etc..) and in each row, there is a comment section for the user to add updates. As shown in the screenshots, each row has its own "add record" button but I want there to be one "submit" button at the bottom of the table to update multiple comment sections. Currently, only one textarea can be submitted.
Here is a snip from the table file:
echo "<h3> Welcome ". $username;
echo "<h3> Form Data </h3>";
echo "<table border='1'>";
echo "<tr>
<th>Project</th>
<th>%Completed</th>
<th>Comments</th>
</tr>";
echo "<tr>
<td>Jupiter</td>
<td>78%</td>
<td>";?> <form action = 'summary.php' method="post">
<textarea name="text1" rows="2" cols=15></textarea>
<input type="submit" name="value1" value="Add Record">
</form>
<?php "</td>
</tr>";
echo "<tr>
<td>Uranus</td>
<td>69%</td>
<td>";?> <form action = 'summary.php' method="post">
<textarea name="text2" rows="2" cols=15></textarea>
<input type="submit" name="value2" value="Add Record">
</form> <?php
echo "</table>";
Here is a snip from the summary file:
<?php
$PC1 = $_POST["text1"];
$PC2 = $_POST["text2"];
//Display in table
echo "Table updated by: " .$_SESSION['appusername'];
echo "<table border='1'>";
echo "<tr>
<th>Project</th>
<th>%Completed</th>
<th>Comments</th>
</tr>";
echo "<tr>
<td>Jupiter</td>
<td>78%</td>
<td>$PC1</td>
</tr>";
echo "<tr>
<td>Uranus</td>
<td>69%</td>
<td>$PC2</td>
</tr>";
echo "</table>";
?>
</body>
Any advice would be excellent
You can try something like this:
<form>
<table>
<tr>
<td>project 1</td>
<td></td>
<td><input name="projects[1][comment]"></td>
<td><button name="submit_id" value="1">Comment</button></td>
</tr>
<tr>
<td>project 2</td>
<td></td>
<td><input name="projects[2][comment]"></td>
<td><button name="submit_id" value="2">Comment</button></td>
</tr>
<table>
<button name="submit_id" value="all">Submit all</button>
</form>
Any submit button inside <form>
will send whole form to the server, but using submit_id
value you can make decision which fields you need to process with this request: comment of one particular project, or all comments.
Project 2 button:
«Submit all» button:
</div>
Write a javascript code to get all the values from the input. Add a class to the inputs like 'planets' and get them all by the javascript code $('.planets') and then iterate over the planets read the values to an array and send that to server.
$(function(){
$('#sender').on("click", function(){
var pl = $(".planets");
var data = [];
pl.each(function(){
data.push($(this).val());
});
serverData = JSON.stringify(data)
document.writeln(serverData)
});
});
pass serverData to server using ajax and do json decode to convert back to array and use it to store wherever you want.