Today, We want to share with you status active inactive php example.In this post we will show you Active And Inactive Users Concept Using Php And Ajax, hear for activate and deactivate concept using php and mysql ajax we will give you demo and example for implement.In this post, we will learn about Bootstrap Toggle Switch Update Database Field Using Ajax with an example.
how to set active and inactive in php using ajax?
Contents
Database
CREATE TABLE IF NOT EXISTS `product` ( `id` int(11) NOT NULL, `name` varchar(150) NOT NULL, `pcode` 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 & PHP Code
<?php $db= new mysqli('localhost','root','','mostlikers'); ?> <html> <head></head> <body> <table border="1"> <tr> <th>#</th> <th>name</th> <th>pcode</th> <th>Action</th> </tr> <?php $sql=$db->query("Select * from product"); foreach ($sql as $key => $product) { ?> <tr> <td><?php echo $product['id'] ?></td> <td><?php echo $product['name']; ?></td> <td><?php echo $product['pcode']; ?></td> <td><i data="<?php echo $product['id'];?>" class="status_checks btn <?php echo ($product['status'])? 'btn-success': 'btn-danger'?>"><?php echo ($product['status'])? 'Active' : 'Inactive'?> </i></td> </tr> <?php } ?> </table> </body> </html>
Ajax Code
<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); $product_id=$db->real_escape_string($id); $status=$db->real_escape_string($status); $sql=$db->query("UPDATE product SET status='$status' WHERE id='$id'"); echo 1; ?>
CSS CODE
.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; }
I hope you get an idea about Active inactive users in PHP MySQLi.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, donโt forget to share.
I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.