Automatically Add current date to table when button clicked

You will need JavaScript for this. I have built you a little Tinker available here to show you the code as it is live (JS side): . Note that I have modified your mark-up slightly: the ID of your courses (if you have any) is now as the rel property of every row of your table.

$(function() { $("#myresults").each(function() { $(this).find("tbody > tr").each(function() { // Loops through the rows... if ($(this).attr("rel")) { // Checks if truthy var ID = $(this).attr("rel"); // This is the ID of your course var cellSwitch = $(this).find("td > input[type='checkbox']").first(); // This is your checkbox { $.ajax({ // AJAX request is being done here url: "", data: { "search": "test", "id": ID, "completed": $(this).is(":checked") }, success: function(d) { alert("I just pinged the server!"); } }); }); } }); }); });

Please don’t spam the server I am pinging too much. Every time you do a request, it feeds back to Amazon. :stuck_out_tongue:

So, the explanation for the client-side. I’m using jQuery to grease the way a bit and not have to lug an entire XMLHttpRequest definition around. If you’re not using it yet - strongly recommended for abstraction purposes.

On DOMLoad, the script goes to your table (selector $("#mytable") picks up the element by ID mytable). It then iterates through the table rows (selector .find(“tbody > tr”)), and if the row has a rel parameter, it knows that it will have a checkbox. If it does have one, it binds a click event to it.

This click event will fire an AJAX request to a pre-defined URL with the ID of the checkbox you’ve just checked. This will allow you to update your DB. You should point the url parameter to what you’d like to ping. If it succeeds, it will alert.

The PHP code should be something similar to below:

// Check connection
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();

if (empty($_POST[‘ID’])) {
$updateID = (int)$_POST[‘ID’]; // Sanitization by typecasting.

$sql = “UPDATE Assessment SET Completed = IF(Completed = ‘Completed’, ‘’, ‘Completed’) WHERE ID = “.$updateID.” LIMIT 1”;

if (!mysqli_query($con,$sql))
die('Error: ’ . mysqli_error());
This will update the row with the selected ID. If it is “Completed”, it will set it to “”, and vice versa, thanks to the IF clause in MySQL.

(The last query is slightly erroneous but I can’t update the post any longer. If you want to set the date instead, use NOW())

Sponsor our Newsletter | Privacy Policy | Terms of Service