通过ajax接收数据,而不是将数据插入mysql数据库

时间:2017-02-05 18:29:09

标签: php jquery html mysql ajax

我通过ajax将两个唯一ID发送到php文件中。事情是ajax正在成功向php发送数据,但插入查询在那里不起作用.... 这是JS脚本:

<!-- 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> 

这是php脚本:

<?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);
?>

这是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">&times;</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>

请帮帮我。谢谢!

2 个答案:

答案 0 :(得分:1)

更新回答

好的,新计划。我认为我们已经通过私人聊天建立了数据没有到达PHP文件。将这三个文件复制到同一目录中,看看我是否得到了相同的结果。

首先,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>

接下来,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);
        });
    });
});

最后,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;

?>

当我点击“分配”时在第一个员工身上,我在控制台中得到了这个:

emp in JS = 1
task in JS= 12
emp_id in PHP = 1
tsk_id in PHP = 12

我没有实现获取task_id的方式,所以我每次都将它设置为12,但是为了这个例子它并不重要。试试看,看看你是否得到与我类似的结果。然后,我们可以解决为什么你的其他脚本没有得到类似的答案。

答案 1 :(得分:0)

在将值插入表

时尝试使用''和大写字母
$ins = "INSERT INTO `taskempmapping` (task_id,employee_id) VALUES ('$tsk_id','$emp_id')";
$run = mysqli_query($con,$ins);