Reputation: 13
I wanted to change the Checkin time for every switch I change. My problem is that when I change any of those, I will only change the 1st row checkin time. If anyone could help with the problem I have.
Update: Wanted to change the checkin time, when I change the first row, the first row checkin time will be updated or given text and so on... 2nd switch changes the 2nd row checkin time ... 3rd.. and so on. Thanks.
<script>
$(document).ready(function () {
$('.checkin-button').click(function(e){
if ($(this).is(':checked')){
var famid = $(this).closest('tr').find('input[name="famno"]').val();
var a = this.checked ? 1 : 0;
$.ajax({
type: 'POST',
url: 'checkdep.php',
data: {famid: famid}
}).done(function(res){
console.log(res);
if (res == 1 ){
var date = //the date **wanted to pass date to TIMEEMP. Since I still dont know how to and how to give value to the var with dd/mm/yyyy HH:mm:ss a format.
$('timeemp').text/html(date) **confused here
}else{
alert('Check-in failed');
}
});
}else{
});
</script>
<html>
<tr>
<td><?php echo $no; ?></td>
<td><?php echo $company; ?></td>
<td><?php echo $branch; ?></td>
<td><a href="empmain.php?access=view&photos=<?php echo $photos;?>&lname=<?php echo $lname; ?>&fname=<?php echo $fname; ?>&empid=<?php echo $empid;?>&empno=<?php echo $empno;?>"><span class="glyphicon glyphicon-user" aria-hidden="true"></span><?php echo $empid;?></a></td>
<td><?php echo $depthead; ?></td>
<td id="timeemp"><?php echo $timecheck; ?></td>
<td>
<div class = "form-group">
<form id="empform" action="" method="post">
<?php
$userid='1';
if($userid == '1'){
if ($row['Status'] == ''){
echo'
<div class="form-group">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="">
<label>
<input type="checkbox" class="js-switch checkemp-button" />
<input name="emp" value = "'.$empno.'" type="hidden" />
</label>
</div>
</div>
</div>';
}
else{
echo '<div class="form-group">
<div class="col-md-9 col-sm-9 col-xs-12">
<div class="">
<label>
<input type="checkbox" class="js-switch checkemp-button" checked />
<input name="emp" value = "'.$empno.'" type="hidden" />
</label>
</div>
</div>
</div>';
}
}
else{
}
?>
</form>
</div>
</td>
</tr>
</html>
Upvotes: 1
Views: 76
Reputation: 151
If you want to use the current system time you can get it by "date = new Date()". But by default you will get the full date String. If you need to convert it into custom format you can follow any of the following ways
1) You can use moment.js => this will be helpful to get date in custom format and also you can convert based on the time zone as well
2) You can get date(as like the given format) using javascript by the following function
function getDateString(){
var currentDate = new Date();
var month = currentDate.getMonth() + 1;
if(month<=9)
month="0" +month;
var date = currentDate.getDate();
if(date<=9)
date="0" +date;
var hours = currentDate.getHours();
var meridian = "AM";
if(hours>=12){
hours= hours % 12;
meridian = "PM"
}
if(hours<=9){
hours = "0"+hours;
}
var minutes = currentDate.getMinutes();
if(minutes<=9)
minutes="0" +minutes;
var seconds = currentDate.getSeconds();
if(seconds<=9)
seconds="0" +seconds;
return month+"/"+date+"/"+currentDate.getFullYear()+" "+hours+":"+minutes+":"+seconds+ " "+meridian;
}
Upvotes: 0
Reputation: 151
Try the following updated code.
$('.checkin-button').click(function(e){
if ($(this).is(':checked')){
var famid = $(this).closest('tr').find('input[name="famno"]').val();
var a = this.checked ? 1 : 0;
var selectedRow = $(this).closest('tr'); // Updated
$.ajax({
type: 'POST',
url: 'checkdep.php',
data: {famid: famid}
}).done(function(res){
console.log(res);
if (res == 1 ){
var date = //the date **wanted to pass date to TIMEEMP. Since I still dont know how to and how to give value to the var with dd/mm/yyyy HH:mm:ss a format.
selectedRow.find('#timeemp').text(date); // Updated
}else{
alert('Check-in failed');
}
});
}else{}
});
Upvotes: 1