Reputation: 776
I am sending two unique ids into a php file via ajax. The thing is ajax is sending data to php successfully, though, insert query isn't working there .... Here is the JS script:
<!-- JS script for setting and getting task and employee id -->
<script type="text/javascript">
$('.alcbtn').click(function() {
var ts = $(this).data('task');
$('.allocatetaskmodal table').find('.alctask').attr('data-task', ts);
});
$('.alctask').click(function() {
var t = $(this);
var emp = $(this).data('emp');
var task = $(this).data('task');
$.post('tescript.php', { emp : emp , task : task }, function(value) {
console.log("data sent");
t.text('Allocated');
});
});
</script>
Here is the php script:
<?php
include 'connection.php';
$emp_id = $_POST['emp'];
$tsk_id = $_POST['task'];
$ins = "insert into taskempmapping (task_id,employee_id) values ($tsk_id,$emp_id)";
$run = mysqli_query($con,$ins);
?>
And here is the html:
<div class="modal fade allocatetaskmodal" id="allocatetsk" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header prj-gc">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title prj-gc" id="myModalLabel">Allocate Employee</h4>
</div>
<div class="modal-body prj-gc">
<table class="table table-bordered">
<thead>
<tr>
<th>Employee ID</th>
<th>Employee Name</th>
<th>Employee Designation</th>
<th>Pending Tasks</th> <!-- use count in query to fetch pending tasks -->
<th></th>
</tr>
</thead>
<tbody>
<!-- Php code to fetch all the records of employees whose designation is not PM or TL -->
<?php
$query = "select employee_id, employee_name, employee_designation from employee where employee_designation!= 'Project Manager' AND employee_designation!='Team Leader'";
$result = mysqli_query($con,$query);
while ($row = mysqli_fetch_array($result))
{
echo '<tr>
<td>'.$row['employee_id'].'</td>
<td>'.$row['employee_name'].'</td>
<td>'.$row['employee_designation'].'</td>
<td></td>
<td><a class="btn btn-info btn-xs alctask" data-emp="'.$row['employee_id'].'" href="#">Allocate</a></td>
</tr>';
}
?>
</tbody>
</table>
</div>
Please help me in this. Thank you!
Upvotes: 0
Views: 83
Reputation: 4065
UPDATE TO ANSWER
OK, new plan. I think we've established from our private chat that the data is not getting to the PHP file. Copy these three files into the same directory and see if you get the same results I do.
First up, index.php:
<?php
$result = [
[
'employee_id' => 1,
'employee_name' => 'bill',
'employee_designation' => 'webdesigner'
],
[
'employee_id' => 2,
'employee_name' => 'sam',
'employee_designation' => 'manager'
],
[
'employee_id' => 3,
'employee_name' => 'mike',
'employee_designation' => 'boss'
],
];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-xs-8">
<table class="table table-bordered">
<thead>
<tr>
<th>Employee ID</th>
<th>Employee Name</th>
<th>Employee Designation</th>
<th>Pending Tasks</th> <!-- use count in query to fetch pending tasks -->
<th></th>
</tr>
</thead>
<tbody>
<?php
foreach ($result as $row)
{
echo '<tr>
<td>'.$row['employee_id'].'</td>
<td>'.$row['employee_name'].'</td>
<td>'.$row['employee_designation'].'</td>
<td></td>
<td><a class="btn btn-info btn-xs alctask" data-emp="'.$row['employee_id'].'" href="#">Allocate</a></td>
</tr>';
}
?>
</tbody>
</table>
</div>
</div>
</div>
<!-- jQuery first, then Tether, then Bootstrap JS. -->
<script
src="https://code.jquery.com/jquery-3.1.1.js"
integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA="
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
<script src="script.js" type="text/javascript" charset="utf-8" async defer></script>
</body>
</html>
Next, script.js:
$(document).ready(function() {
$('.alctask').click(function() {
var t = $(this);
var emp = $(this).data('emp');
var task = 12;
console.log('emp in JS = ' + emp);
console.log('task in JS= ' + task)
$.post('tescript.php', { emp : emp , task : task }, function(data) {
console.log(data);
});
});
});
Finally, tescript.php:
<?php
$emp_id = $_REQUEST['emp'];
$tsk_id = $_REQUEST['task'];
echo 'emp_id in PHP = ' . $emp_id . PHP_EOL;
echo 'tsk_id in PHP = ' . $tsk_id . PHP_EOL;
?>
When I click 'Allocate' on the first employee, I get this in the console:
emp in JS = 1
task in JS= 12
emp_id in PHP = 1
tsk_id in PHP = 12
I didn't implement your way of getting the task_id so I just set it to 12 each time, but it doesn't matter for the sake of the example. Try that out, and see if you get similar results as me. Then, we can work through why your other script isn't getting similar answers.
Upvotes: 1
Reputation: 165
Try using '' and capital letters when inserting values into table
$ins = "INSERT INTO `taskempmapping` (task_id,employee_id) VALUES ('$tsk_id','$emp_id')";
$run = mysqli_query($con,$ins);
Upvotes: 0