Stop inserting if user clicks on the active button - php

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);
}

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).

Related

Enable and disable button without page load

I have a form with a dropdown box, login button, and logout button. My requirement is to insert date and time when user click on the login or logout button after username selection from dropdown. Also, when user clicks on login button, it has to enable by changing the background color without page load. Same functionality required for logout option also.
Now, I can select the user's name from the dropdown which is coming from database. I stucked on the insertion part which i need without page load.
I need to insert data without page load once click on the login button.
Any help would be appreciable. Thanks in advance.
Code:
index.php
<?php
include_once "database.php";
?>
<html>
<head>
<script>
function showUser(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<?php
$sql_user="SELECT * FROM users";
$result_user=mysql_query($sql_user);
?>
<form>
<select name="users" onchange="showUser(this.value)" style="width: 210px;height: 36px; font-size:17px;">
<option style="height:30px;" value="">Select a person:</option>
<?php while($row_user=mysql_fetch_array($result_user)){?><option style="height:30px;" value="<?php echo $row_user['id'];?>"><?php echo $row_user['username'];?></option><?php }?>
</select>
</form>
<br>
<div id="txtHint"></div>
</body>
</html>
getuser.php
<style>
button{
width: 100px;
height: 38px;
background:white;
color:black;
}
</style>
<?php
$q = intval($_GET['q']);
include_once "database.php";
$sql="SELECT * FROM users WHERE id = '$q'";
$result = mysql_query($sql);
$row=mysql_fetch_array($result);
$entry=$row['entry'];
?>
<form method="post" name="">
<table>
<tr><td>
<button id="btnSave" <?php if($entry=='0'){?> style=" background:#6DAAD4;"<?php }?>> Login</button>
</td><td>
<button <?php if($entry=='1'){?> style=" background:#6DAAD4;"<?php }?>> Logout</button>
</td></tr>
</table>
</form>
in you getuser.php remove form tag and add function 'insertDateTime()' to buttons click event like:
<table>
<tr>
<td>
<button id="btnSave" onclick="insertDateTime(this,'<?php echo $q;?>');"> Login</button>
</td>
<td>
<button id="btnSave" onclick="insertDateTime(this,'<?php echo $q;?>')"> Logout</button>
</td>
</tr>
</table>
Add one function to your index.php
<script src="http://code.jquery.com/jquery-1.11.1.min.js"> </script> //better to download and us locally
<script>
function insertDateTime(obj, id)
{
$("[id^='"+obj.id+"']").css('background', '#FFFFFF');
$(obj).css('background', '#6DAAD4');
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>
and write your code in insertdate.php to insert date and time in database. You can get your id using $_POST['id']; in insertdate.php.
Bind onclick = "func()" in button
inside func method, change the color this way :
document.getElementById("formID").style.backgroundColor="lightblue";

how to open multiple textboxes on a click using ajax

<table>
<tbody id="eb_body">
<tr>
<td>Booking Bills:</td>
<td><input type="text" id="e_brochure0" name="e_brochure0" placeholder="From Bill No........" required/></td>
</tr>
<tr>
<td>
<a onClick="add_brochures()" style="text-decoration:none !important;"><span style="cursor:pointer; font-size:10px; ">+Add more bill nos.</span></a>
</td>
</tr>
<tr>
<td>
<input type="submit" value="save" name="save" />
</td>
</tr>
</tbody>
</table>
there is an anchor tag on that click i am calling a function as below..
<script type="text/javascript">
var b=1;
function add_brochures()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("zone").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","five.php",true);
xmlhttp.send();
b=b+1;
}
</script>
with this i am directing the page to five.php where i am displaying a select box....
code for five.php is this...
<?php
include 'dbcon.php';
$sql="select * from tb_zone";
$query=mysql_query($sql);
?>
<tr>
<td>
<select>
<?php
while($row=mysql_fetch_array($query))
{
?>
<option>
<?php echo $row["zone_name"]; ?>
</option>
<?php } ?>
</select>
</td>
</tr>
when i click on anchor tag it is opening one selectbox but bot more....what i want is that when i click on anchor tag then a selectbox appears...if i click on it 5 times then i should open selectbox 5 times....right now it is opening just 1 selectbox....please help
Change:
document.getElementById("zone").innerHTML=xmlhttp.responseText;
to:
document.getElementById("zone").innerHTML += xmlhttp.responseText;
+= will concatenate the response to the existing HTML, which will add a new row to the table.

PHP Ajax with a form

I have a form that let user to choose where them come from..
I have list out the country list by select box..
and i success to do it onchange and use ajax to read out all the state list..
actually i have two php file.. one is the main one that contain all the code include the form.. another one is generate state list.. my ajax code and form are in the main php
this is the one that generate state list...
$cID = $_GET["country"];
$sql = "SELECT * FROM States WHERE CountryID=$cID";
$result = $db->get_results($sql);
if (isset($result)) {
foreach ( $result as $states ) {
?>
<input type="checkbox" name="chkState[]" value="<?=$states->StateID?>" /> <?=$states->StateName?><br />
<?php
}
}
this is part of my code that read out..
but my problem is...
where i submit the form..
i only get the variable of country.. i cannot take the data of state that use ajax to generate out...
sample of my form
<form action="" name="place">
<select name="cboCountry" class="drop" onchange="showState(this.value)">
//some php code the list out the country
</select>
<div id="txtHint">
</div>
<input type="submit" name="submit" value="submit" />
</form>
and here my ajax code
<script>
function showState(str)
{
alert('xxx');
var xmlhttp;
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","state_list.php?country="+str,true);
xmlhttp.send();
}
</script>
so when i submit the from with a save button... i only get the date of country and state are missing.. any body can help me? or having any way to do what i want?

Perform xmlhttprequest and afterwards direct to new url location

Im new to this ajax and javascript thing. What I want is first to popup a confirm box, if true then perform xmlhttprequest. Afterwards I want to redirect the user to a new page. Here is what I've got.
<script type="text/javascript">
function showUser(str)
{
var r=confirm("Delete this customer?");
if (r==true) {
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","sletkunde.php?q="+str,true);
xmlhttp.send();
}
}
</script>
<form onclick="showUser(id.value); return false;" >
<input type="hidden" value="<?echo $id;?>" name="id">
<input type="button" value="tryk" name="knap">
</form>
<br>
<div id="txtHint"><b>something here</b></div>
And here is the sletkunde.php
<?
$q=$_GET["q"];
$slet = mysql_query("DELETE FROM Kunder WHERE KundeID = '".$q."'");
echo "hey"; // Just to see if it works
?>
The confirm box works, the mysql_query works, the echo "hey"; works. But how can I get it to redirect to kunder.php right after all of this (automatically).
Use location.href = "http://www.mysite.com/kunder.php" in your onreadystatechange function.

How can I send form information from a dynamically populated div #printClass into another div #dbmsg?

I am very new to php mysql ajax jquery programming and am having a problem loading addlabform's formdata from a div into a mysql database, therefore if my code is longwinded please forgive. I have php file teacher.php that has a div #printClass that was appended to with a form of checkbox values from getclass.php. I would like to take the checkbox values and pass them to a php file addlab.php that will load in another div #dbmsg with a message. I have tried using many different methods and have settled on trying to work with onclick and submit but I cannot pick up the values of the form in the first generated div #printClass. This is my html:
<div id="dbmsg"></div>This is
<div id="printClass">Labid: 100Course: 11
<br>
<form name="addlabform">
<input name="classid" value="11" type="hidden">
<input onclick="submitit()" value="SubmitIt" type="submit">
<input onclick="additnow()" value="Add Labs" type="button">
<table border="1">
<tbody>
<tr>
<th>Lastname</th>
<th>Firstname</th>
<th>Taken</th>
<th>Date</th>
</tr>
<tr>
<td>lastname1</td>
<td>firstname1</td>
<td align="center">
<input name="labdone" value="2191" id="2191" type="checkbox">
</td>
<td>
<i>Not Done</i>
</td>
</tr>
<tr>
<td>lastname2</td>
<td>firstname2</td>
<td align="center">
<input name="labdone" value="2042" id="2042" type="checkbox">
</td>
<td>
<i>Not Done</i>
</td>
</tr>
</table>
<script src="jquery-1.7.1.min.js" type="text/javascript"></script>
<script>$("#printClass").ready(additnow())</script>
<script>$("#printClass").ready(submitit())</script>
<script>
$(document).ready(function() {
$("#addlabform").submit(function () {
$.post("addlab.php", $("#addlabform").serialize(), function (data, textStatus) {
$("#dbmsg").append(data);
});
return false;
});
});
</script>
</div>
I have 3 functions get_check_values(), addit() and submitit(). get_check_values() is intended to get the values for the checkbox values, which does not work error console says that document.printClass is undefined. addit() works in loading the information into the div #dbmsg but I cant get the values from #printClass so until get_check_values works it is stuck. submitit() reloads the original php file with the checked values in the $_REQUEST. I would prefer to use submitit() because it would make it unnecessary to add all the input values to the addit() xmlhttp.open line. Here are the functions:
<script type="text/javascript">
function get_check_values()
{
var c_value = "";
for (var i=0; i < document.printClass.addlabform.labdone.length; i++)
{
if (document.printClass.addlabform.labdone[i].checked) {
c_value = c_value + document.printClass.addlabform.labdone[i].value;
alert(c_value);
return false;
}
}
}
function submitit() {
$(document).ready(function() {
$("#addlabform").submit(function () {
$.post("addlab.php", $("#addlabform").serialize(), function (data, textStatus) {
$("#dbmsg").append(data);
});
return false;
});
});
}
function additnow() {
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("dbmsg").innerHTML=xmlhttp.responseText;
}
}
formvals = "tid=$tid";
var classid=encodeURIComponent(document.getElementById("printClass").value);
xmlhttp.open("GET","addlab.php?+tid=$tid&classid="+classid+formvals,true);
xmlhttp.send();
}
Any help would be greatly appreciated as I have been working on this for days.
I suggest just using an ordinary form submit:
Your problem then becomes purely a PHP problem.
If there's some reason reloading the entire page is no good, you can use an iframe to ensure that only the form is reloaded (it'll still be the entire form, though):
<iframe src="addlab.php"></iframe>
Be sure to check $_POST['submitbutton'] in your PHP to check which submit button was clicked.

Categories