0

I have two buttons(Login and logout). When logout is active, if user clicks again on the logout button then shouldn't do insertion or updation. The same for login button also.

Now, it's inserting when user clicks on the active button. I need to avoid that.

Thanks in advance.

Code:

<script>
function insertDateTime(obj, id)
{
$("[id^='"+obj.id+"']").css('background', '#FFFFFF').css('font-weight', 'normal').css('font-size', '14px').css('color', 'grey');
$(obj).css('background', '#D4B357').css('font-weight', 'bold').css('font-size', '15px').css('color', 'black');

if (window.XMLHttpRequest) {
    xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

var params = "id="+id;

xmlhttp.open("POST","insertdate.php",true);

xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
/*    xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");*/
xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
        //alert(xmlhttp.responseText);
    }
}        
xmlhttp.send(params);
}
</script>

getuser.php

<table>
<tr>
   <td>
       <button id="btnSave" <?php if($entry=='1'){?> style=" background:#D4B357; font-weight:bold; font-size:15px; color:black;outline:0px;"<?php }?> onclick="insertDateTime(this,'<?php echo $q;?>');"> Login</button>
   </td>
   <td>
       <button id="btnSave" <?php if($entry=='0'){?> style=" background:#D4B357; font-weight:bold; font-size:15px;color:black;outline:0px;"<?php }?> onclick="insertDateTime(this,'<?php echo $q;?>');"> Logout</button>
   </td>
 </tr>
 </table>

insertdate.php date_default_timezone_set("Asia/Kolkata"); $date=date("Y-m-d", time());?>

<?php $time=date("H:i:s",time());
include_once "database.php";
$q = $_POST['id'];

$sql_check="select * from entries where user_id='$q' order by id desc";/*last row*/
$res_check=mysql_query($sql_check);
$row=mysql_fetch_array($res_check);
$logout_date=$row['logout_date'];
$num=mysql_num_rows($res_check);
$id=$row['id'];

if($logout_date != "" || $num=='0'){
$sql1="insert into entries(user_id,login_date,login_time,logout_date,logout_time)values('$q','$date','$time','','')";
$res1=mysql_query($sql1);
$sql="update users set entry='1' where id='$q'";
$res=mysql_query($sql);
}
else{
$sql="update entries set logout_date='$date', logout_time='$time' where id='$id'";
$res=mysql_query($sql);
$sql1="update users set entry='0' where id='$q'";
$res1=mysql_query($sql1);
}
  • 1
    **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Sep 19 '14 at 06:40

1 Answers1

0

You can stop the user from clicking the buttons second and consecutive times by disabling the buttons after the first click using a simple JavaScript code.

Once the action is complete, you can enable the button back (or hide it as they are for login/logout).

  • Hi Daniel, thanks for the reply. I'm not good in Javascript. It will be very helpful if you can provide me the code for that. –  Sep 19 '14 at 06:53