Today i have posted Active and inactive users script. This concept mostly using E-commerce website administrator access to check product available or not, delivery status, user block and activate like that similar concept related on this concept. Here i am using php with ajax let see the code.
Database
CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL,
`name` varchar(150) NOT NULL,
`email` varchar(150) NOT NULL,
`address` varchar(100) NOT NULL,
`status` enum('0','1') NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
Html
'btn-success': 'btn-danger' - Checking class
'Active' : 'Inactive' - Checking Active or inactive value
data="<?php echo $user['id'];?>" - User id to pass javascript valuetat
$db= Database connection.
<?php $db= new mysqli('localhost','root','','mostlikers'); ?>
<html>
<head></head>
<body>
<table border="1">
<tr>
<th>#</th>
<th>name</th>
<th>email</th>
<th>Action</th>
</tr>
<?php $sql=$db->query("Select * from user");
foreach ($sql as $key => $user) {
?>
<tr>
<td><?php echo $user['id'] ?></td>
<td><?php echo $user['name']; ?></td>
<td><?php echo $user['email']; ?></td>
<td><i data="<?php echo $user['id'];?>" class="status_checks btn
<?php echo ($user['status'])?
'btn-success': 'btn-danger'?>"><?php echo ($user['status'])? 'Active' : 'Inactive'?>
</i></td>
</tr>
<?php } ?>
</table>
</body>
</html>
Ajax
$(this).hasClass- Class suceess (Active) or (Inactive).
var msg - Status message.
<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).on('click','.status_checks',function(){
var status = ($(this).hasClass("btn-success")) ? '0' : '1';
var msg = (status=='0')? 'Deactivate' : 'Activate';
if(confirm("Are you sure to "+ msg)){
var current_element = $(this);
url = "ajax.php";
$.ajax({
type:"POST",
url: url,
data: {id:$(current_element).attr('data'),status:status},
success: function(data)
{
location.reload();
}
});
}
});
</script>
Ajax.php
<?php $db= new mysqli('localhost','root','','mostlikers');
extract($_POST);
$user_id=$db->real_escape_string($id);
$status=$db->real_escape_string($status);
$sql=$db->query("UPDATE user SET status='$status' WHERE id='$id'");
echo 1;
?>
CSS
.btn-success {
background-color: #65B688;
border-color: #65B688;
}
.btn-danger {
color: #fff;
background-color: #d9534f;
border-color: #d43f3a;
}
.btn {
color: white;
display: inline-block;
margin-bottom: 0;
font-weight: 400;
text-align: center;
vertical-align: middle;
cursor: pointer;
background-image: none;
border: 1px solid transparent;
white-space: nowrap;
padding: 6px 12px;
font-size: 14px;
line-height: 1.42857143;
border-radius: 4px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
Database
CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL,
`name` varchar(150) NOT NULL,
`email` varchar(150) NOT NULL,
`address` varchar(100) NOT NULL,
`status` enum('0','1') NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
Html
'btn-success': 'btn-danger' - Checking class
'Active' : 'Inactive' - Checking Active or inactive value
data="<?php echo $user['id'];?>" - User id to pass javascript valuetat
$db= Database connection.
<?php $db= new mysqli('localhost','root','','mostlikers'); ?>
<html>
<head></head>
<body>
<table border="1">
<tr>
<th>#</th>
<th>name</th>
<th>email</th>
<th>Action</th>
</tr>
<?php $sql=$db->query("Select * from user");
foreach ($sql as $key => $user) {
?>
<tr>
<td><?php echo $user['id'] ?></td>
<td><?php echo $user['name']; ?></td>
<td><?php echo $user['email']; ?></td>
<td><i data="<?php echo $user['id'];?>" class="status_checks btn
<?php echo ($user['status'])?
'btn-success': 'btn-danger'?>"><?php echo ($user['status'])? 'Active' : 'Inactive'?>
</i></td>
</tr>
<?php } ?>
</table>
</body>
</html>
Ajax
$(this).hasClass- Class suceess (Active) or (Inactive).
var msg - Status message.
<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).on('click','.status_checks',function(){
var status = ($(this).hasClass("btn-success")) ? '0' : '1';
var msg = (status=='0')? 'Deactivate' : 'Activate';
if(confirm("Are you sure to "+ msg)){
var current_element = $(this);
url = "ajax.php";
$.ajax({
type:"POST",
url: url,
data: {id:$(current_element).attr('data'),status:status},
success: function(data)
{
location.reload();
}
});
}
});
</script>
Ajax.php
<?php $db= new mysqli('localhost','root','','mostlikers');
extract($_POST);
$user_id=$db->real_escape_string($id);
$status=$db->real_escape_string($status);
$sql=$db->query("UPDATE user SET status='$status' WHERE id='$id'");
echo 1;
?>
CSS
.btn-success {
background-color: #65B688;
border-color: #65B688;
}
.btn-danger {
color: #fff;
background-color: #d9534f;
border-color: #d43f3a;
}
.btn {
color: white;
display: inline-block;
margin-bottom: 0;
font-weight: 400;
text-align: center;
vertical-align: middle;
cursor: pointer;
background-image: none;
border: 1px solid transparent;
white-space: nowrap;
padding: 6px 12px;
font-size: 14px;
line-height: 1.42857143;
border-radius: 4px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
Nice work
ReplyDeletethank you, brother.
ReplyDeleteI just want to let you know that I just check out your site and I find it very interesting and informative..
ReplyDeletephp Script