Remove button after being clicked - php

I have a page where the users can view their leave applications, I also have a Cancel button beside it which looks like this:
View Page
What I wanted to happen is that when I click on the cancel button, the status would change and the button would disappear.
I added a script that would make the button disappear and it worked, but it won't update the status.
So here's my form:
<div class="container">
<div class="page-header">
<h3>My Leaves</h3>
<div class="table-responsive">
<table class="table">
<tr>
<th>Employee Name</th>
<th>Phone</th>
<th>Email</th>
<th>From</th>
<th>To</th>
<th>Reason</th>
<th>Type</th>
<th>Status</th>
</tr>
<?php
include ('database.php');
$result = $database->prepare ("SELECT leaves.* FROM leaves INNER JOIN employee ON employee.id = leaves.user_id WHERE employee.username = '".$_SESSION["VALID_USER_ID"]."'");
$result ->execute();
for ($count=0; $row_message = $result ->fetch(); $count++){
?>
<tr>
<td><?php echo $row_message['firstname']." " .$row_message['lastname']; ?></td>
<td><?php echo $row_message['phone']; ?></td>
<td><?php echo $row_message['email']; ?></td>
<td><?php echo $row_message['fromdate']; ?></td>
<td><?php echo $row_message['todate']; ?></td>
<td><?php echo $row_message['reason']; ?></td>
<td><?php echo $row_message['type']; ?></td>
<td><?php echo $row_message['status']; ?></td>
<td>
<form method="post" action="update-leave-status-emp.php">
<input type="hidden" name="leaveno" value="<?php echo $row_message['leaveno']; ?>" />
<input type="submit" value="Cancelled" class="removebtn" name="cancelled"></input>
</form>
</td>
</tr>
<?php } ?>
</table>
<button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button>
</div>
</div>
</div>
</div>
</div>
And here's the script below:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".removebtn").click(function(){
$(this).remove();
});
});
</script>
PHP Code:
<?php
if(isset($_POST['cancelled']))
{
$msg = "Cancelled";
$status=$_POST['cancelled'];
}
$leaveno=$_POST['leaveno'];
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con, 'companydb');
$sql = "UPDATE leaves SET status = '$status' WHERE leaveno = '$leaveno'";
if(mysqli_query($con, $sql))
header("refresh:1; url=view-leave-emp.php?msg=$msg");
else
var_dump(mysqli_error($con));
?>

Related

adding values to a different column after button click

I have a page where users can view the tickets they sent, they can cancel it as well, here's what it looks like:
What I want to happen is that when I click on the "Closed: Cancelled" button. It would show "Closed: Cancelled" in the assignee column as well. Currently this is what I only get when I click the button:
Here's my form:
<div class="container">
<div class="page-header">
<h3>My Tickets</h3>
<div class="table-responsive">
<table class="table">
<tr>
<th>Employee Name</th>
<th>Time</th>
<th>Priority</th>
<th>Assignee</th>
<th>Subject</th>
<th>Problem</th>
<th>Status</th>
<th></th>
</tr>
<?php
include ('database.php');
$result = $database->prepare ("SELECT tickets.* FROM tickets INNER JOIN employee ON employee.id = tickets.employee_id WHERE employee.username = '".$_SESSION["VALID_USER_ID"]."'");
$result ->execute();
for ($count=0; $row_message = $result ->fetch(); $count++){
?>
<tr>
<td><?php echo $row_message['firstname']." ".$row_message['lastname']; ?></td>
<td><?php echo $row_message['time']; ?></td>
<td><?php echo $row_message['priority']; ?></td>
<td><?php echo $row_message['assignee']; ?></td>
<td><?php echo $row_message['subject']; ?></td>
<td><?php echo $row_message['problem']; ?></td>
<?php if ($row_message['status']) : ?>
<td><?php echo $row_message['status']."".$row_message['assignee'];?></td>
<?php else : ?>
<td>
<form method="post" action="update-ticket-status-emp.php">
<input type="hidden" name="ticketno" value="<?php echo $row_message['ticketno']; ?>" />
<input type="submit" name="closedcan" value="Closed: Cancelled"></input>
</form>
</td>
<?php endif ; ?>
</tr>
<?php } ?>
</table>
<button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button>
</div>
</div>
</div>
</div>
</div>
And here's the exec code:
<?php
if(isset($_POST['closedcan']))
{
$msg = "ClosedCan";
$status = $_POST['closedcan'];
$assignee = $_POST['closedcan'];
}
$ticketno=$_POST['ticketno'];
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con, 'companydb');
$sql = "UPDATE tickets SET status = '$status' WHERE ticketno = '$ticketno'";
if(mysqli_query($con, $sql))
header("refresh:1; url=view-tickets-emp.php?msg=$msg");
else
var_dump(mysqli_error($con));
?>
PS: I know mysql is deprecated, I will change it eventually when I figure this out.
Modified the query in the exec code:
$sql = "UPDATE tickets SET status = '$status', assignee = '$assignee' WHERE ticketno = '$ticketno'";

Removing button after updating database values

I have a page where users can view the leave applications they submitted and they can also cancel the application, here's what it looks like:
I wanted to get rid of the cancel button after it was cancelled or after it was clicked. So I added a remove button script in the execution code. Apparently, it was wrong. Everything worked fine until I added the script in the execution code.
Here's my form:
HTML+PHP
<div class="container">
<div class="page-header">
<h3>My Leaves</h3>
<div class="table-responsive">
<table class="table">
<tr>
<th>Employee Name</th>
<th>Phone</th>
<th>Email</th>
<th>From</th>
<th>To</th>
<th>Reason</th>
<th>Type</th>
<th>Status</th>
</tr>
<?php
include ('database.php');
$result = $database->prepare ("SELECT leaves.* FROM leaves INNER JOIN employee ON employee.id = leaves.user_id WHERE employee.username = '".$_SESSION["VALID_USER_ID"]."'");
$result ->execute();
for ($count=0; $row_message = $result ->fetch(); $count++){
?>
<tr>
<td><?php echo $row_message['firstname']." " .$row_message['lastname']; ?></td>
<td><?php echo $row_message['phone']; ?></td>
<td><?php echo $row_message['email']; ?></td>
<td><?php echo $row_message['fromdate']; ?></td>
<td><?php echo $row_message['todate']; ?></td>
<td><?php echo $row_message['reason']; ?></td>
<td><?php echo $row_message['type']; ?></td>
<td><?php echo $row_message['status']; ?></td>
<td>
<form method="post" action="update-leave-status-emp.php">
<input type="hidden" name="leaveno" value="<?php echo $row_message['leaveno']; ?>" />
<input type="submit" value="Cancelled" class="removebtn" name="cancelled"></input>
</form>
</td>
</tr>
<?php }?>
</table>
<a href="employee_panel.php">
<button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button>
</a>
</div>
</div>
</div>
Execution Code:
<?php
if(isset($_POST['cancelled'])){
$msg = "Cancelled";
$status=$_POST['cancelled'];
echo "<script>
$(document).ready(function(){
$(".removebtn").click(function(){
$(this).remove();
});
});
</script>";
}
$leaveno=$_POST['leaveno'];
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con, 'companydb');
$sql = "UPDATE leaves SET status = '$status' WHERE leaveno = '$leaveno'";
if(mysqli_query($con, $sql))header("refresh:1; url=view-leave-emp.php?msg=$msg");
else var_dump(mysqli_error($con));
?>
PS: I know my codes are vulnerable to injections, I will change it eventually, for now, I have to figure this out.
Enclosed cancelled button in an if statement that if the record is marked cancelled the button won't be shown anymore.
You can try this updated code:
<div class="container">
<div class="page-header">
<h3>My Leaves</h3>
<div class="table-responsive">
<table class="table">
<tr>
<th>Employee Name</th>
<th>Phone</th>
<th>Email</th>
<th>From</th>
<th>To</th>
<th>Reason</th>
<th>Type</th>
<th>Status</th>
</tr>
<?php
include ('database.php');
$result = $database->prepare ("SELECT leaves.* FROM leaves INNER JOIN employee ON employee.id = leaves.user_id WHERE employee.username = '".$_SESSION["VALID_USER_ID"]."'");
$result ->execute();
for ($count=0; $row_message = $result ->fetch(); $count++){
?>
<tr>
<td><?php echo $row_message['firstname']." " .$row_message['lastname']; ?></td>
<td><?php echo $row_message['phone']; ?></td>
<td><?php echo $row_message['email']; ?></td>
<td><?php echo $row_message['fromdate']; ?></td>
<td><?php echo $row_message['todate']; ?></td>
<td><?php echo $row_message['reason']; ?></td>
<td><?php echo $row_message['type']; ?></td>
<td><?php echo $row_message['status']; ?></td>
<td>
<form method="post" action="update-leave-status-emp.php">
<input type="hidden" name="leaveno" value="<?php echo $row_message['leaveno']; ?>" />
<?php if( $row_message['status'] != "Cancelled" ) {
<input type="submit" value="Cancelled" class="removebtn" name="cancelled"></input>
<?php } ?>
</form>
</td>
</tr>
<?php }?>
</table>
<a href="employee_panel.php">
<button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button>
</a>
</div>
</div>
</div>

Trying to update column values by submit button

I'm trying to update status on my leaves table by pressing the approve/reject button. Here's my table schema:
So far this is what i've done:
<?php
if(isset($_POST['approved']))
{
echo "Approved";
$status=$_POST['approved'];
}
if(isset($_POST['rejected']))
{
echo "Rejected";
$status=$_POST['rejected'];
}
?>
<!-- Begin page content -->
<div class="container">
<div class="page-header">
<h3>Employee Leaves</h3>
<div class="table-responsive">
<table class="table">
<tr>
<th>Employee Name</th>
<th>Phone</th>
<th>Email</th>
<th>From</th>
<th>To</th>
<th>Reason</th>
<th>Status</th>
<th>---</th>
</tr>
<?php
include ('database.php');
$result = $database->prepare ("SELECT * FROM leaves order by id DESC");
$result ->execute();
for ($count=0; $row_message = $result ->fetch(); $count++){
?>
<tr>
<td><?php echo $row_message['full_name']; ?></td>
<td><?php echo $row_message['phone']; ?></td>
<td><?php echo $row_message['email']; ?></td>
<td><?php echo $row_message['fromdate']; ?></td>
<td><?php echo $row_message['todate']; ?></td>
<td><?php echo $row_message['reason']; ?></td>
<td><?php echo $row_message['status']; ?></td>
<td>
<form method="post" action="update.php">
<input type="hidden" value="<?php echo $row_message['id']; ?>" />
<input type="submit" value="Approved" name="approved"></input>
<input type="submit" value="Rejected" name="rejected"></input>
</form>
</td>
</tr>
<?php } ?>
</table>
<button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button>
</div>
</div>
</div>
</div>
</div>
and here's my update.php
<?php
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con, 'leaves');
$sql = "UPDATE leaves SET status = :status WHERE id = :id";
if(mysqli_query($con, $sql))
header("refresh:1; url=view-leave.php");
else
echo "Error";
?>
I'm getting the "error" when I click on approve/reject. I think the problem is with the update.php. I'm not sure what it is tho or where.
Try this : update.php
<?php
if(isset($_POST['approved']))
{
$msg = "Approved";
$status=$_POST['approved'];
}
if(isset($_POST['rejected']))
{
$msg = "Rejected";
$status=$_POST['rejected'];
}
$id=$_POST['id'];
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con, 'leaves');
$sql = "UPDATE newusers SET username = '$status' WHERE id = '$id'";
if(mysqli_query($con, $sql))
header("refresh:1; url=index.php?msg=$msg");
else
var_dump(mysqli_error($con));
?>
and view-leave.php
<?php
if(isset($_GET['msg']))
{
echo $_GET['msg'];
}
?>
<!-- Begin page content -->
<div class="container">
<div class="page-header">
<h3>Employee Leaves</h3>
<div class="table-responsive">
<table class="table">
<tr>
<th>Employee Name</th>
<th>Phone</th>
<th>Email</th>
<th>From</th>
<th>To</th>
<th>Reason</th>
<th>Status</th>
<th>---</th>
</tr>
<?php
include ('database.php');
$result = $database->prepare ("SELECT * FROM leaves order by id DESC");
$result ->execute();
for ($count=0; $row_message = $result ->fetch(); $count++){
?>
<tr>
<td><?php echo $row_message['full_name']; ?></td>
<td><?php echo $row_message['phone']; ?></td>
<td><?php echo $row_message['email']; ?></td>
<td><?php echo $row_message['fromdate']; ?></td>
<td><?php echo $row_message['todate']; ?></td>
<td><?php echo $row_message['reason']; ?></td>
<td><?php echo $row_message['status']; ?></td>
<td>
<form method="post" action="update.php">
<input type="hidden" name="id" value="<?php echo $row_message['id']; ?>" />
<input type="submit" value="Approved" name="approved"></input>
<input type="submit" value="Rejected" name="rejected"></input>
</form>
</td>
</tr>
<?php } ?>
</table>
<button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button>
</div>
</div>
</div>
</div>
</div>
You have to write database name here:
mysqli_select_db($con, 'dbname');
Did you try to declare a default value for the $msg=''; and $status = '';?

How to display multiple rows from another table on the same line when fetching data into table using php

I have two tables in mysql, one table holds pics been snapped from webcam with image descriptions and another table which holds the user's registration details.
The image below will explain better.
This is the photos table with a track_id of the user
This is the user registration table with a unique id
On the the photos table the user details appeared twice with different description of the image uploaded
here is the result of the fetched details from both tables
My objective is to make the pictures appear side by side but when I fetched from database it was duplicating the information due to the fact that the user appeared twice on the photos table. And when I used group by it only displayed one picture.
Here is my source code
<?php require_once('queries.php'); ?>
<?php include("header.php"); ?>
<?php include("head.php"); ?>
<?php include("sidenavs.php"); ?>
<?php include("topnavs.php"); ?>
<div class="wrapper">
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 col-md-12">
<h4 class="form-signin-heading">Users Details.</h4>
<hr />
<span class="glyphicon glyphicon-pencil"></span> Add User
<hr />
<div class="content-loader">
<table cellspacing="0" width="100%" id="example" class="table display table-striped table-hover table-responsive">
<thead>
<tr>
<th>#ID</th>
<th>Name</th>
<th>dob</th>
<th>gender</th>
<th>bvn</th>
<th>business name</th>
<th>contact address</th>
<th>Town/City</th>
<th>lga</th>
<th>State</th>
<th>phone 1</th>
<th>phone 2</th>
<th>email</th>
<th>products & services rendered</th>
<th>sub society name</th>
<th>position</th>
<th>mem id no</th>
<th>next kin name</th>
<th>relationship</th>
<th>phone of next kin</th>
<th>payment status</th>
<th>photo</th>
<th>signature</th>
<th>date registered</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
require_once 'db/dbconfig.php';
$i =1;
$stmt = $db_con->prepare("SELECT * FROM users ORDER BY id DESC");
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
$idm = $row['id'];
$stmts = $db_con->prepare("SELECT * FROM fotos where track_id ='$idm' group by $idm");
$stmts->execute();
$rows=$stmts->fetch(PDO::FETCH_ASSOC);
?>
<tr>
<td><?php echo $i; $i++;?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['dob'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['bvn'];?></td>
<td><?php echo $row['business_name'];?></td>
<td><?php echo $row['contact_addr'];?></td>
<td><?php echo $row['Town_City'];?></td>
<td><?php echo $row['lga'];?></td>
<td><?php echo $row['State'];?></td>
<td><?php echo $row['phone_1']; ?></td>
<td><?php echo $row['phone_2']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['products_services_rendered'];?></td>
<td><?php echo $row['sub_society_name']; ?></td>
<td><?php echo $row['position']; ?></td>
<td><?php echo $row['mem_id_no'];?></td>
<td><?php echo $row['next_kin_name'];?></td>
<td><?php echo $row['relationship'];?></td>
<td><?php echo $row['phone_of_next_kin'];?></td>
<td><?php if($row['payment_status'] == 'Paid'){echo '<span class="alert-success" style="padding:3px; ">Paid</span>';}else{echo '<span class="alert-warning" style="padding:3px; ">Unpaid</span>';}?></td>
<td><img src="fotos/<?php if($rows['des'] == 'Photo'){echo $rows['id_foto'];}?>" width="50" height="50"></td>
<td><img src="fotos/<?php if($rows['des'] == 'Signature'){echo $rows['id_foto'];} ?>" width="50" height="50"></td>
<td><?php echo $row['date_registered'];?></td>
<td class="td-actions text-right">
<!--<a id="<?php echo $row['id']; ?>" class="edit-link btn btn-primary btn-simple btn-xs" rel="tooltip" href="#" title="Edit User"><i class="material-icons">edit</i></a>-->
<a id="<?php echo $row['id']; ?>" class="delete-link btn-danger btn-simple btn-xs" rel="tooltip" href="#" title="Delete">
<span class="glyphicon glyphicon-trash"></span>
</a>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<?php include("footer2.php"); ?>

Jquery mouse over event

I want to show a hidden div when I mouse over a link on my table cell. The event only fires up on the first row. This is what i did:
$(document).ready(function(){
$("#show_div").mouseover(function() {
$("#hello").css('visibility', 'visible');
});
$("#hello").mouseover(function() {
$("#hello").css('visibility', 'visible');
});
$("#hello").mouseout(function() {
$("#hello").css('visibility', 'hidden');
});
});
<form action="" method="post">
<table class="table table-bordered table-hover">
<div id="bulkOptionContainer" class="col-xs-4">
<select class="form-control" name="bulk_options" id="">
<option value="">Select Option</option>
<option value="publish">Publish</option>
<option value="draft">Draft</option>
<option value="delete">Delete</option>
</select>
</div>
<div class="col-xs-4">
<input class="btn btn-success" type="submit" name="submit" value="Apply"/>
<a class="btn btn-primary" href="posts.php?source=add_post">Add New</a>
</div>
<thead>
<tr>
<th><input type="checkbox" name="checkbox[]" id="selectAllCheckBoxes"></th>
<th>Id</th>
<th>Author</th>
<th scope="col">Title</th>
<th>Category</th>
<th>Status</th>
<th>Image</th>
<th>Tags</th>
<th>Comments</th>
<th>Date</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php $query = "SELECT * FROM post ORDER BY post_id DESC";
$result_set = mysqli_query($link, $query);
confirm_query($result_set);
while ($row = mysqli_fetch_assoc($result_set)):?>
<tr>
<td><label><input type="checkbox" class="checkBoxes" name="checkBoxArray[]"
value="<?php echo $row['post_id']; ?>"></label></td>
<td><?php echo $row['post_id'] ?></td>
<td><?php echo $row['post_author'] ?></td>
<td width="100%">
<!-- link to over-->
<a id="show_div"
href="../post.php?p_id=<?php echo $row['post_id'] ?>"><?php echo $row['post_title'] ?></a>
<!-- div to become visible on hover-->
<div id="hello" style="visibility:hidden;">
<p>post</p>
</div>
</td>
<td><?php echo sel_cat_byId($row['post_category_id']) ?></td>
<td><?php echo $row['post_status'] ?></td>
<td><img width="100" class="img-responsive" src="../images/<?php echo $row['post_image'] ?>"></td>
<td><?php echo $row['post_tag'] ?></td>
<td><?php echo $row['post_comment_count']; ?></td>
<td><?php echo $row['post_date'] ?></td>
<td><a class="btn btn-danger" href="posts.php?delete=<?php echo $row['post_id']; ?> "
onclick="alert('Are You Sure?')">Delete</a></td>
</td>
<td><a class="btn btn-success" href="posts.php?source=edit_post&edit=<?php echo $row['post_id']; ?>">Edit</a>
</td>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</form>
****the event only fires up on the first row.
Thanks
First replace your id with class and then try this
$(".show_div").mouseover(function() {
$(this).next(".hello").css('visibility', 'visible');
});
$(".show_div").mouseout(function() {
$(this).next(".hello").css('visibility', 'hidden');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<a class="show_div"
href="#">abc</a>
<!-- div to become visible on hover-->
<div class="hello" style="visibility:hidden;">
<p>post</p>
</div>

Categories