When I update one of the user, It will update some of user's timein too.
It's updating but I don't know why the other user too.
If I edit the time out it will change the timeout too of the some users. Sorry for my bad english.
logs.php
<tbody>
<?php
$user_query=mysqli_query($dbcon,"SELECT * FROM user_logs_tbl where create_date >= date(now())")or die(mysqli_error($dbcon));
//$user_query=mysqli_query($dbcon,"SELECT * FROM user_logs_tbl")or die(mysqli_error($dbcon));
while($row=mysqli_fetch_array($user_query)){
$ids = isset($_row['id']) ? $_row['id'] : '';
$_SESSION=$id=$row['user_id'];
$user_query2=mysqli_query($dbcon,"SELECT * FROM users_tbl where id='$id'")or die(mysqli_error($dbcon));
$row2= mysqli_fetch_array($user_query2);?>
<tr class="del<?php echo $ids ?>">
<td>
<?php echo $row['id']; ?>
</td>
<td>
<?php echo $row2['firstname']." ".$row2['middlename']." ".$row2['lastname']; ?>
</td>
<td>
<?php $course= $row['create_date'];
if($course == '') {
echo "N/A";
} else {
echo $course;
}
?>
</td>
<td>
<?php echo $tayming=$row['time_in']; ?>
</td>
<td>
<?php $yr_lvl=$row['time_out'];
if($yr_lvl == '') {
?>
<a rel="tooltip" title="Timeout" id="<?php echo $id; ?>" href="#timeout<?php echo $id; ?>" data-toggle="modal" class="btn btn-info"><i class="icon-minus icon-large"></i> Manual Time-Out</a>
<?php include('modal_time_user.php');
} else {
echo $yr_lvl;
}
?>
</td>
<td>
<?php
$d=mktime(9, 06);
$nice=date("h:i", $d);
if($tayming >= $nice) {
echo "You're Late! Bro!";
} else {
echo "Very Good!";
}
?>
</td>
<td>
<a rel="tooltip" title="Edit" id="<?php echo $id; ?>" href="#edit<?php echo $id; ?>" data-toggle="modal" class="btn btn-success"><i class="icon-pencil icon-large"></i> Edit</a>
<?php include('modal_edit_time.php'); ?>
</td>
<?php include('toolttip_edit_delete.php'); ?>
</tr>
<?php } ?>
</tbody>
this is the modal where if you click the button edit
modal_edit_time.php
<div id="edit<?php echo $id; ?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-body">
<div class="alert alert-info"><strong>Manual Time-In</strong></div>
<form class="form-horizontal" method="post">
<div class="control-group">
<label class="control-label" style="margin-left:29%;" for="inputEmail">Employee:</label>
<div class="controls">
<?php $wiw=mysqli_query($dbcon,"SELECT * FROM users_tbl where id='$id'");
$row2= mysqli_fetch_array($wiw);?>
<input type="hidden" id="inputEmail" name="id" value="<?php echo $row['user_id']; ?>" required>
<input type="text" name="userid" value="<?php echo $row2['firstname']." ".$row2['middlename']." ". $row2['lastname'];?>" readonly>
</div>
</div>
<div class="control-group">
<label class="control-label" style="margin-left:29%;" for="inputEmail">Date:</label>
<div class="controls">
<input type="date" id="inputEmail" value="<?php echo $row['create_date'];?>" name="create_date" placeholder="Date" required>
</div>
</div>
<div class="control-group">
<label class="control-label" style="margin-left:29%;" for="inputEmail">Time-In:</label>
<div class="controls">
<input type="time" id="inputEmail" value="<?php echo $row['time_in'];?>" name="timein" placeholder="Time" required>
</div>
</div>
<div class="control-group">
<label class="control-label" style="margin-left:29%;" for="inputEmail">Time-Out:</label>
<div class="controls">
<input type="time" id="inputEmail" value="<?php echo $row['time_out'];?>" name="timeout" placeholder="Time" required>
</div>
</div>
<div class="control-group">
<div class="controls">
<button name="edit" type="submit" class="btn btn-success"><i class="icon-save icon-large"></i> Save</button>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true"><i class="icon-remove icon-large"></i> Close</button>
</div>
</div>
<?php echo $id;
if (isset($_POST['edit'])){
$userid=$_POST['id'];
$timein=$_POST['timein'];
$timeout=$_POST['timeout'];
$create_date=$_POST['create_date'];
mysqli_query($dbcon,"UPDATE user_logs_tbl set time_in='$timein',time_out = '$timeout',create_date ='$create_date' where id='$id'")or die(mysqli_error($dbcon));
$yourURL="logs.php";
echo ("<script>location.href='$yourURL'</script>");
}
?>
Web interface:
Database :
Related
This is my code to display the table and fetch the data from the database. When I click the Edit button, the modal is not showing, what is wrong with my code? Please help me. The first set of code is the index.php file and second is the update-product.php file which is the file for modal.
<?php
require_once "config.php";
//$i = 1;
$query = mysqli_query($link, "SELECT * FROM `product_stock`") or die(mysqli_error());
while($row = mysqli_fetch_array($query)){
?>
<tr>
<th scope="row" class="text-center"><?php echo $row["product_id"]; ?></th>
<td class="text-center"><?php echo $row["product_name"]; ?></td>
<td class="text-center"><?php echo $row["price"]; ?></td>
<td class="text-center"><?php echo $row["category"]; ?></td>
<td class="text-center"><?php echo $row["supplier_name"]; ?></td>
<td class="text-center"><?php echo $row["qty_stock"]; ?></td>
<td class="text-center"><img src="images/<?php echo $row["product_img"]; ?>" width = 200 title="<?php echo $row['product_img']; ?>"></td>
<td class="text-center"><button class="btn btn-info" data-toggle="modal" type="button" data-target="#update_modal<?php echo $row["product_id"]?>"><span class="glyphicon glyphicon-edit"></span> Edit</button></td>
</tr>
<?php
include "update-product.php";
}
?>
<div class="modal fade" id="update_modal<?php echo $row["product_id"]?>" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST" action="do-update-product.php">
<div class="modal-header">
<h3 class="modal-title">Update Product</h3>
</div>
<div class="modal-body">
<div class="col-md-2"></div>
<div class="col-md-8">
<div class="form-group">
<label>Product ID</label>
<input type="text" name="prodid" value="<?php echo $row["product_id"]?>" class="form-control" required="required"/>
</div>
<div class="form-group">
<label>Product Name</label>
<input type="hidden" name="prodid" value="<?php echo $row["product_id"]?>"/>
<input type="text" name="prodname" value="<?php echo $row["product_name"]?>" class="form-control" required="required"/>
</div>
<div class="form-group">
<label>Lastname</label>
<input type="text" name="price" value="<?php echo $row["price"]?>" class="form-control" required="required" />
</div>
<div class="form-group">
<label>Address</label>
<input type="text" name="qty_stock" value="<?php echo $row["qty_stock"]?>" class="form-control" required="required"/>
</div>
</div>
</div>
<div style="clear:both;"></div>
<div class="modal-footer">
<button name="update" class="btn btn-warning"><span class="glyphicon glyphicon-edit"></span> Update</button>
<button class="btn btn-danger" type="button" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
</div>
</div>
</form>
</div>
</div>
</div>
if you use Bootstrap 4
<button class="btn btn-info" data-toggle="modal" type="button" data-target="#update_modal<?php echo $row["product_id"]?>"><span class="glyphicon glyphicon-edit"></span> Edit</button>
if you use Bootstrap 5
<button class="btn btn-info" data-bs-toggle="modal" type="button" data-bs-target="#update_modal<?php echo $row["product_id"]?>"><span class="glyphicon glyphicon-edit"></span> Edit</button>
i need to pass form values to bootstrap model form hidden fields on form button click.
basically the button is form button that holds the value of pos_id from database and a hidden text field that holds pro_id from database and when use click "Apply" button a registration form in model is opened where i need to take input from user and also want to store hidden fields value to database along with user inputs
here is my code of form that i am submitting.
<?php
echo "<div class='mt-3'>
<table class='table table-striped'>
<thead>
<tr>
<th>Apply For</th>
<th>Description</th>
<th>Project Name</th>
<th>Type</th>
<th>Last Date</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>".$row['post_name'],"</td>
<td>".$row['post_desc'],"</td>
<td>".$row['proj_name'],"</td>
<td>".$row['proj_type'],"</td>
<td>".$row['last_date'],"</td>
<td>
<form id='post_apply' method='post' action=''>
<input type='hidden' value='".$row['proj_id'],"'","></input>
<button data-toggle='modal' data-target='#applymodal' type='button' name='apply_btn' value='".$row['post_id'],"'"," class='btn btn-success border float-right '>Apply</button>
</form>
</td>
</tr>
</tbody>
</table>
</div>";
}
}
else {
echo "<p class='alert alert-danger text-center'>No Posts Found</p>";
exit;
}
mysqli_close($conn);
and model code is as:
<!-- The Modal -->
<div class="modal fade" id="applymodal" role="dialog">
<div class="modal-dialog " style="max-width:600px;">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header bg-danger ">
<h1 class="modal-title text-center" color="white">New Registration</h1>
<button type="button" class="btn btn-danger" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div id="modalmain" class="modal-body">
<form class="form" action="signup.php" method="post" enctype="multipart/form-data">
<fieldset>
<!-- display form error messages -->
<?php include(INCLUDE_PATH . "/layouts/messages.php") ?>
<div class="form-group <?php echo isset($errors['firstname']) ? 'has-error' : '' ?>">
<label class="control-label">Firs Name</label>
<input type="text" name="first_name" id="first_name" class="form-control form-control-sm">
<?php if (isset($errors['firstname'])): ?>
<span class="help-block"><?php echo $errors['firstname'] ?></span>
<?php endif; ?>
<?php echo $row['post_id'];?>
</div>
<div class="form-group <?php echo isset($errors['last_name']) ? 'has-error' : '' ?>">
<label class="control-label">Last Name</label>
<input type="text" name="last_name" id="last_name" class="form-control form-control-sm">
<?php if (isset($errors['last_name'])): ?>
<span class="help-block"><?php echo $errors['last_name'] ?></span>
<?php endif; ?>
</div>
<div class="form-group <?php echo isset($errors['mobile']) ? 'has-error' : '' ?>">
<label class="control-label">Mobile</label>
<input type="text" name="mobile" id="mobile" class="form-control form-control-sm" data-inputmask=" 'mask' : '0399-9999999'" placeholder="Mobile">
<?php if (isset($errors['mobile'])): ?>
<span class="help-block"><?php echo $errors['mobile'] ?></span>
<?php endif; ?>
</div>
<div class="form-group <?php echo isset($errors['username']) ? 'has-error' : '' ?>">
<label class="control-label">CNIC Number</label>
<input type="text" name="username" id="username" class="form-control form-control-sm" data-inputmask="'mask': '99999-9999999-9'" placeholder="14 Digits CNIC Number" required="yes">
<?php if (isset($errors['username'])): ?>
<span class="help-block"><?php echo $errors['username'] ?></span>
<?php endif; ?>
</div>
<div class="form-group <?php echo isset($errors['email']) ? 'has-error' : '' ?>">
<label class="control-label">Email</label>
<input type="text" name="email" id="email" class="form-control form-control-sm" placeholder="Email Address">
<?php if (isset($errors['email'])): ?>
<span class="help-block"><?php echo $errors['email'] ?></span>
<?php endif; ?>
</div>
<div class="form-group <?php echo isset($errors['password']) ? 'has-error' : '' ?>">
<label class="control-label">Password</label>
<input type="password" name="password" id="password" class="form-control form-control-sm">
<?php if (isset($errors['password'])): ?>
<span class="help-block"><?php echo $errors['password'] ?></span>
<?php endif; ?>
</div>
<div class="form-group <?php echo isset($errors['passwordConf']) ? 'has-error' : '' ?>">
<label class="control-label">Password confirmation</label>
<input type="password" name="passwordConf" class="form-control form-control-sm">
<?php if (isset($errors['passwordConf'])): ?>
<span class="help-block"><?php echo $errors['passwordConf'] ?></span>
<?php endif; ?>
</div>
<input type="hidden" value="$_POST['proj_id'];"></input>
<input type="hidden" value="$_POST['post_id'];"></input>
<div class="form-group">
<button type="submit" name="signup_btn" class="btn btn-block btn-success ">Create Account</button>
</div>
</fieldset>
</form>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<p class="text-start">Aready have an account? Sign in</p>
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
#salar zaidi: Thank you soo much for help.
This solution worked for me:
$("#post_apply").click(function() {
var proj = $(this).find("#proj_id").val();
var post = $(this).find("#apply_btn").val();
var postn = $(this).find("#post_name").val();
$("#field_proj_id").val(proj)
$("#field_post_id").val(post)
$("#field_post_n").val(postn)
});
Using php, I want to make the Edit button in register.php to display the form which shows fields that have the already existing user's data and then admin can update the data. I'm about to go crazy because of this thing it's been the third day in row not moving to the second step! I've watched many YT tutorials and explored many solutions here and in google as well, but couldn't actually figure out how to fix mine! Please if you know where my mistake is and what should I edit, do tell me and I will be more than happy to fix it.
These are my files:
register.php
<?php
session_start();
include('includes/header.php');
include('includes/navbar.php');
?>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#addadminprofile">
Add Admin Profile
</button>
<!-- Modal -->
<div class="modal fade" id="addadminprofile" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Add Admin Data</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action="code.php" method="POST">
<div class="modal-body">
<div class="form-group">
<label>Username </label>
<input type="text" name="username" class="form-control" required>
</div>
<div class="form-group">
<label>Full name </label>
<input type="text" name="full_name" class="form-control" required>
</div>
<div class="form-group">
<label>Email </label>
<input type="email" name="email" class="form-control" required>
</div>
<div class="form-group">
<label>Password </label>
<input type="password" name="password" class="form-control" required>
</div>
<div class="form-group">
<label>Confirm password </label>
<input type="password" name="cpassword" class="form-control" required>
</div>
<div class="form-group">
<label>Gender </label>
<input type="radio" name="gender" tabindex="1" value="Male">
<span> Male</span>
<input type="radio" name="gender" tabindex="2" value="Female">
<span> Female</span>
</div>
<div class="form-group">
<label>Age </label>
<input type="text" name="age" class="form-control" required>
</div>
<div class="form-group">
<label>Phone number </label>
<input type="text" name="phone_number" class="form-control" required>
</div>
<div class="form-group">
<label>Work durationr </label>
<input type="text" name="work_duration" class="form-control" required>
</div>
<div class="form-group">
<label >Food Category:</label>
<select name="food_category" class="form-control" required>
<option value="hot_cold_dishes">Hot/Cold Dishes</option>
<option value="desserts">Desserts</option>
<option value="drinks">Drinks</option>
</select>
</div>
<div class="form-group">
<label>IG Account </label>
<input type="text" name="ig_account" class="form-control" required>
</div>
<div class="form-group">
<label >State:</label>
<select name="state" class="form-control" required>
<option value="1">First</option>
<option value="2">Second</option>
<option value="3n">Third</option>
</select>
</div>
<div class="form-group">
<label>Postcode </label>
<input type="text" name="postcode" class="form-control" required>
</div>
<div class="form-group">
<label>City </label>
<input type="text" name="city" class="form-control" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="Submit" name="submit" class="btn btn-primary">Register</button>
</div>
</form>
</div>
</div>
</div>
<?php
/*
if(isset($_SESSION['success']) && $_SESSION['success'] != ''){
echo '<h2>'.$_SESSION['success'].'</h2>';
unset ($_SESSION['success']);
}
if(isset($_SESSION['status']) && $_SESSION['status'] != ''){
echo '<script>alert('.$_SESSION['status'].')</script>';
unset ($_SESSION['status']);
}
*/
?>
<div class="card shadow">
<div class="card-body">
<div class="table-responsive">
<?php
$connection = mysqli_connect("localhost", "root", "", "finalproject") or die ("<script>alert('Connection Failed.')</script>");
$sql = "SELECT * FROM users";
$result = mysqli_query($connection, $sql);
?>
<table class="table table-borderd" id="dataTable" width="30%" cellspecing="0">
<thead>
<tr>
<th>Username</th>
<th>Full Name</th>
<th>Email</th>
<th>Password</th>
<th>Age</th>
<th>Gender</th>
<th>Phone Number</th>
<th>Work Duration</th>
<th>Food Category</th>
<th>IG Account</th>
<th>State</th>
<th>Postcode</th>
<th>City</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result)){
?>
<tr>
<td><?php echo $row['username']?></td>
<td><?php echo $row['full_name']?></td>
<td><?php echo $row['email']?></td>
<td><?php echo $row['password']?></td>
<td><?php echo $row['phone_number']?></td>
<td><?php echo $row['work_duration']?></td>
<td><?php echo $row['food_category']?></td>
<td><?php echo $row['ig_account']?></td>
<td><?php echo $row['state']?></td>
<td><?php echo $row['postcode']?></td>
<td><?php echo $row['city']?></td>
<td><?php echo $row['gender']?></td>
<td><?php echo $row['age']?></td>
<td>
<form action="first_register.php" method="POST">
<input type="hidden" name="edit_username" value="<?php echo $row['username'];?>">
<button type="submit" name="edit_button" class="btn btn-success">Edit</button>
</form>
</td>
<td>
<button type="submit" class="btn btn-danger">Delete</button>
</td>
</tr>
<?php
}
}
else{
echo "No record found";
}
?>
</tbody>
</table>
</div>
</div>
<?php
include('includes/script.php');
include('includes/footer.php');
?>
first_register.php
<?php
$connection = mysqli_connect("localhost", "root", "", "finalproject")or die ("<script>alert('Connection Failed.')</script>");
session_start();
include('includes/header.php');
include('includes/navbar.php');
?>
<div class="container-fluid">
<!-- DataTales Example -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary"> EDIT Admin Profile </h6>
</div>
<div class="card-body">
<?php
if(isset($_POST['edit_button']))
{
$username = $_POST['username'];
$query = "SELECT * FROM register WHERE username='$username' ";
$query_run = mysqli_query($connection, $query);
foreach($query_run as $row)
{
?>
<form action="code.php" method="POST">
<input type="hidden" name="edit_username" value="<?php echo $row['username'] ?>">
<div class="form-group">
<label> Full name </label>
<input type="text" name="edit_full_name" value="<?php echo $row['full_name'] ?>" class="form-control">
</div>
<div class="form-group">
<label>Email</label>
<input type="email" name="edit_email" value="<?php echo $row['email'] ?>" class="form-control"
placeholder="Enter Email">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="edit_password" value="<?php echo $row['password'] ?>"
class="form-control" placeholder="Enter Password">
</div>
<div class="form-group">
<label>Gender</label>
<input type="radio" name="edit_gender" value="Male" class="form-control">
<span> Male</span>
<input type="radio" name="edit_gender" value="Female" class="form-control">
<span> Female</span>
</div>
<div class="form-group">
<label>Age</label>
<input type="text" name="edit_age" value="<?php echo $row['age'] ?>"
class="form-control">
</div>
<div class="form-group">
<label>Phone Number</label>
<input type="text" name="edit_phone_number" value="<?php echo $row['phone_number'] ?>"
class="form-control">
</div>
<div class="form-group">
<label>Work Duration</label>
<input type="text" name="edit_work_duration" value="<?php echo $row['work_duration'] ?>"
class="form-control">
</div>
<div class="form-group">
<label>Food Category</label>
<select name="food_category" class="form-control" required>
<option value="hot_cold_dishes">Hot/Cold Dishes</option>
<option value="desserts">Desserts</option>
<option value="drinks">Drinks</option>
</select>
</div>
</div>
<div class="form-group">
<label>IG Account</label>
<input type="text" name="edit_ig_account" value="<?php echo $row['ig_account'] ?>"
class="form-control">
</div>
<div class="form-group">
<label>State</label>
<select name="state" class="form-control" required>
<option value="1">First</option>
<option value="2">Second</option>
<option value="3n">Third</option>
</select>
</div>
<div class="form-group">
<label>Postcode</label>
<input type="text" name="edit_postcode" value="<?php echo $row['postcode'] ?>"
class="form-control">
</div>
<div class="form-group">
<label>City</label>
<input type="text" name="edit_city" value="<?php echo $row['city'] ?>"
class="form-control">
</div>
<form action="edit_register.php" method="POST">
<input type="hidden" name="edit_username" value="<?php echo $row['username'];?>">
<button type="submit" name="edit_button" class="btn btn-success">Edit</button>
</form>
</form>
<?php
}
}
?>
<?php
include('includes/script.php');
include('includes/footer.php');
?>
edit_register.php
<?php
$connection = mysqli_connect("localhost", "root", "", "finalproject")or die ("<script>alert('Connection Failed.')</script>");
session_start();
include('includes/header.php');
include('includes/navbar.php');
?>
<div class="card shadow">
<div class="card-header">
<h6 class="font-weight-bold text-primary">EDIT Profile </h6>
</div>
<div class="card_body">
<?php
if(isset($_POST['updatebtn']))
{
$username2 = $_POST['edit_username'];
$full_name2 = $_POST['edit_full_name'];
$email2 = $_POST['edit_email'];
$password2 = $_POST['edit_password'];
$cpassword2 = $_POST['cpassword'];
$phone_number2 = $_POST['edit_phone_number'];
$work_duration2 = $_POST['edit_work_duration'];
$food_category2 = $_POST['edit_food_category'];
$ig_account2 = $_POST['edit_ig_account'];
$state2 = $_POST["edit_state"];
$postcode2 = $_POST['edit_postcode'];
$city2 = $_POST['edit_city'];
$gender2 = $_POST['edit_gender'];
$age2 = $_POST['edit_age'];
$query = "UPDATE users SET username='$username2', full_name='$full_name2', email='$email2', password='$password2', phone_number='$phone_number2', word_duration='$work_duration2', food_category='$food_category2',
ig_account='$ig_account2', state='$state2', postcode='$postcode2', city='$city2', gender='$gender2', age='$age2' WHERE username='$username2'";
$query_run = mysqli_query($connection, $query);
if($query_run)
{
$_SESSION['status'] = "Your Data is Updated";
$_SESSION['status_code'] = "success";
header('Location: register.php');
}
else
{
$_SESSION['status'] = "Your Data is NOT Updated";
$_SESSION['status_code'] = "error";
header('Location: register.php');
}
?>
<?php
}
?>
</div>
<?php
include('includes/script.php');
include('includes/footer.php');
?>
At register.php, you have this line this -
<input type="hidden" name="edit_username" value="<?php echo $row['username'];?>">
So at first_register.php, you should have this line like this instead -
$username = $_POST['username']; to $username = $_POST['edit_username'];
and this line like this instead -
<form action="code.php" method="POST"> to <form action="edit_register.php" method="POST">
i could see you have a form inside a another form like this
<form action="edit_register.php" method="POST">
<input type="hidden" name="edit_username" value="<?php echo $row['username'];?>">
<button type="submit" name="edit_button" class="btn btn-success">Edit</button>
</form>
//remove the form tag to this only since you have input for edit_username aready at the top of your form
<button type="submit" name="edit_button" class="btn btn-success">Edit</button>
//and finally at edit_register.php change
if(isset($_POST['updatebtn']))
to if(isset($_POST['edit_button']))
I guess this should help achieve the goal. good luck
I need to select a record in the modal form. The HTML code is,
<table class="table justify-content-center" style="background-color:white">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<thead style="background-color:green;color:white;font-weight:bold">
<tr>
<th>Employee Name</th>
<th>Employee ID</th>
<th>Designation</th>
<th>Phone</th>
<th>Actions</th>
</tr>
</thead>
<?php
while ($row = $result->fetch_assoc()): ?>
<tr>
<td><?php echo $row['employeename']; ?></td>
<td><?php echo $row['employeeno']; ?></td>
<td><?php echo $row['designation']; ?></td>
<td><?php echo $row['contactnumber']; ?></td>
<td>
<button name="edit" class="editbtn"> <a href="#editEmployeeModal" data-toggle="modal" data-dismiss="modal" style="color: brown" >Edit</a> </button>
<button name="delete" class="btn btn-info " >Delete </button>
</td>
</tr>
<?php endwhile; ?>
</table>
and the HTML for modal form is,
<!-- Edit Modal HTML -->
<div id="editEmployeeModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<form action="employeeprocess.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<div class="modal-header">
<h4 class="modal-title">Edit Employee</h4>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body">
<div class="form-group">
<label>Employee Number</label>
<input type="text" class="form-control" value="<?php $employeenum ?>" name="employeenum" required>
</div>
<div class="form-group">
<label>Employee Name</label>
<input type="text" class="form-control" name="employeename" value="<?php $employeename ?>" required>
</div>
<div class="form-group">
<label>Job Location</label>
<input type="text" class="form-control" name="joblocation" value="<?php $joblocation ?>" required>
</div>
<div class="form-group">
<label>DateofJoin</label>
<input type="date" class="form-control" name="dateofjoin" value="<?php $dateofjoin ?>" required>
</div>
<div class="form-group">
<label>Contact Number</label>
<input type="text" class="form-control" name="contactnumber" value="<?php $contactnumber ?>" required>
</div>
<div class="form-group">
<label>Designation</label>
<input type="text" class="form-control" name="designation" value="<?php $designation ?>" required>
</div>
<div class="form-group">
<label>Status</label>
<input type="text" class="form-control" name="status" value="<?php $status ?>" required>
</div>
<div class="form-group">
<label>Photo</label>
<input type="file" class="form-control" name="myfile" required>
</div>
</div>
<div class="modal-footer">
<input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
<input type="submit" class="btn btn-success" name="edit" value="Update">
</div>
</form>
</div>
</div>
</div>
employeeprocess.php
if (isset($_GET['delete'])){
$id = $_GET['delete'];
$mysqli->query("DELETE FROM employees WHERE id=$id") or die($mysqli->error());
$_SESSION['message'] = "Record has been deleted!";
$_SESSION['msg_type'] = "danger";
header("location: employeelist.php");
}
if (isset($_GET['edit'])){
$id = $_GET['edit'];
$update = true;
$result = $mysqli->query("SELECT * FROM employees WHERE id=$id") or die($mysqli->error());
if (count($result)==1){
$row = $result->fetch_array();
$employeenum = $row['employeenum'];
$employeename = $row['employeename'];
$joblocation = $row['joblocation'];
$dateofjoin = $row['dateofjoin'];
$contactnumber = $row['contactnumber'];
$designation = $row['designation'];
$status = $row['status'];
}
}
Javascript is
<script>
$(".editbtn").click(function () {
$('#edit').val($(this).data('id'));
});
</script>
I need to pass the selected record to the editEmployeeModal and set the variable edit for modal dialog to display the selected record. The modal is not working, if I put the statement href="#editEmployeeModal?edit= <?php echo $row['id']; ?>" So, Can you suggest me to this problem?
You can set a data attribute as id of your row in the for loop and bind an onClick event to a js function to update the form id field:
<button name="edit" class="btn btn-info"> <a href="#editEmployeeModal" data-toggle="modal" data-dismiss="modal" style="color: brown" data-rowid="<?=$row['id']; ?>" onClick="updateId()" >Edit</a> </button>
JavaScript
function updateId(){
let id = this.getAttribute('data-rowid');
document.getElementsByName("id")[0].value = id;
}
You could alternatively pass the id to the updateId function as a parameter instead of setting the data attribute.
I am wondering why the code only works on the last row of the message reply, I can't reply on the latest message but I can reply on the last message, the messages are descending.
Please take a look at messages.php:
<form action="read_message.php" method="post">
<div class="pull-right">
<button class="btn btn-info" name="read"><i class="icon-check"></i> Read</button>
Check All <input type="checkbox" name="selectAll" id="checkAll" />
<script>
$("#checkAll").click(function () {
$('input:checkbox').not(this).prop('checked', this.checked);
});
</script>
</div>
<ul class="nav nav-pills">
<li class="active"><i class="icon-envelope-alt"></i>inbox</li>
<li class=""><i class="icon-envelope-alt"></i>Send messages</li>
</ul>
<?php
$query_announcement = mysql_query("select * from message_received
LEFT JOIN user ON user.user_id = message_received.user_id
where message_received.receiver_id = '$session_id' order by date_sent DESC
")or die(mysql_error());
$count_my_message = mysql_num_rows($query_announcement);
if ($count_my_message != '0') {
while ($row = mysql_fetch_array($query_announcement)) {
$id = $row['message_id'];
$id_2 = $row['message_id'];
$fn = $row['firstname'];
$ln = $row['lastname'];
$status = $row['message_status'];
$sender = $row['user_id'];
$sender_name = $fn . ' ' . $ln;
$receiver = $row['receiver_id'];
?><div class="alert alert-info">
<div class="post" id="del<?php echo $id; ?>">
<div class="message_content">
<?php echo $row['content']; ?>
</div>
<div class="pull-right">
<?php if ($status == 'read') {
} else {
?>
<input id="" class="" name="selector[]" type="checkbox" value="<?php echo $id; ?>">
<?php } ?>
</div>
<hr>
Send by: <strong><?php echo $fn . ' ' . $ln; ?></strong>
<i class="icon-calendar"></i> <? php echo $row['date_sent']; ?>
<div class="pull- right">
<a class="btn btn-link" href="#reply<?php echo $id; ?>" data-toggle="modal" ><i class="icon-reply"></i> Reply </a>
</div>
<div class="pull-right">
<a class="btn btn-link" href="#<?php echo $id; ?>" data-toggle="modal" ><i class="icon-remove"></i> Remove </a>
<?php include("remove_inbox_message_modal.php"); ?>
<?php include("reply_inbox_message_modal_user.php"); ?>
</div>
</div>
</div>
<?php }
} else { ?>
<div class="alert alert-info"><i class="icon- info-sign"></i> No Message Inbox</div>
<?php } ?>
</form>
NOW THIS is the function for read_message.php, I can't see any error, please help me.
<?php
if (isset($_POST['reply'])) {
$sender = $_POST['sender'];
$receiver = $_POST['receiver'];
$my_message = $_POST['my_message'];
mysql_query("insert into message_received (user_id,receiver_id,content,date_sent) values('$receiver','$sender','$my_message',NOW())")or die(mysql_error());
mysql_query("insert into message_sent (receiver_id,content,date_sent,user_id) values('$sender','$my_message',NOW(),'$receiver')")or die(mysql_error());
echo "<script> alert('Your message has been sent $my_message') </script>";
echo " <script>location.replace('messages.php')</script>";
?>
<script>
alert('Message Sent');
window.location = "messages.php";
</script>
<?php
}
?>
now this is the reply modal codes,
<!-- Modal -->
<div id="reply<?php echo $id; ?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria- hidden="true">x</button>
<h3 id="myModalLabel">Reply</h3>
</div>
<div class="modal-body">
<center>
<div class="control-group">
<label class="control-label" for="inputEmail">To:</label>
<div class="controls">
<input type="hidden" name="sender" id="inputEmail" value="<?php echo $sender; ?>" readonly>
<input type="hidden" name="receiver" id="inputEmail" value="<?php echo $receiver; ?>" readonly>
<input type="hidden" name="uid" id="inputEmail" value="<?php echo $uid; ?>" readonly>
<input type="hidden" name="my_name" value="<?php echo $reciever_name; ?>" readonly>
<input type="text" name="name_of_sender" id="inputEmail" value="<?php echo $sender_name; ?>" readonly>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputPassword">Content:</label>
<div class="controls">
<textarea name='my_message'></textarea>
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" name="reply" id="<?php echo $id; ?>" class="btn btn-success reply"><i class="icon-reply"></i> Reply</button>
</div>
</div>
</center>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true"><i class="icon-remove icon-large"></i> Close</button>
<button id="<?php echo $id; ?>" class="btn btn-danger remove" data- dismiss="modal" aria-hidden="true"><i class="icon-check icon-large"></i> Yes </button>
</div>
when I reply to a message the message is sent but it has no content but data from sender, date and time is saved in the database, but in the lowest row, it all functions well? please help me. thank you
You have it your message modal window in one form.
So your solution works only if you have only one message. If you have more messages when you send your response send all responses for each message (its one form) and each response content have same name.
For this reason your server obtains empty $_POST['my_message']
You need have for each response own form and it will works. like this:
<form action="read_message.php" method="post">
<!-- Modal -->
<div id="reply<?php echo $id; ?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria- hidden="true">x</button>
<h3 id="myModalLabel">Reply</h3>
</div>
<div class="modal-body">
<center>
<div class="control-group">
<label class="control-label" for="inputEmail">To:</label>
<div class="controls">
<input type="hidden" name="sender" id="inputEmail" value="<?php echo $sender; ?>" readonly>
<input type="hidden" name="receiver" id="inputEmail" value="<?php echo $receiver; ?>" readonly>
<input type="hidden" name="uid" id="inputEmail" value="<?php echo $uid; ?>" readonly>
<input type="hidden" name="my_name" value="<?php echo $reciever_name; ?>" readonly>
<input type="text" name="name_of_sender" id="inputEmail" value="<?php echo $sender_name; ?>" readonly>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputPassword">Content:</label>
<div class="controls">
<textarea name='my_message'></textarea>
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" name="reply" id="<?php echo $id; ?>" class="btn btn-success reply"><i class="icon-reply"></i> Reply</button>
</div>
</div>
</center>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true"><i class="icon-remove icon-large"></i> Close</button>
<button id="<?php echo $id; ?>" class="btn btn-danger remove" data- dismiss="modal" aria-hidden="true"><i class="icon-check icon-large"></i> Yes </button>
</div>
</form>
PHP variables are not parsed in single quotes. You need to update your SQL queries to use double quotes, or no quotes, with your PHP variables.
insert into message_sent (receiver_id,content,date_sent,user_id) values("$sender","$my_message",NOW(),"$receiver")
That is why only the date is saving.
Clear the messages in your database, update the queries (multiple) and try again. Then, report back with an update and I'll take another look at it.