dongza5150 2017-04-03 13:36
浏览 44

使用ajax jquery进行内联/就地编辑

I trying to do in place editing on a table column with ajax jquery. I'am returning the table with ajax jquery like this:

Form to find table:

<div class="input-group">
    <span class="input-group-addon">Term</span>
     <!-- I passed the tables name as values for a specific period -->
        <select name="term" class="form-control" id="term">
           <option value="">Select Period</option>
           <option value="period_one">1st Period</option>
           <option value="period_two">2nd Period</option>
           <option value="period_three">3rd Period</option>
           <option value="period_four">4th Period</option>
           <option value="period_five">5th Period</option>
           <option value="period_six">6th Period</option>
           <option value="first_exam">First Semester Exam</option>
           <option value="second_exam">Second Semester Exam</option>
         </select>
  </div>

JQuery Ajax script that sends post value:

<script>


$(document).ready(function() {
    $('#term').on('change', function() {
    var term = $('#term').val();
    if (term != '') {
      $.ajax({
          url:"findGrades.php",
          method:"post",
          data:{"term":term},
          dataType:"text",
          success:function(data){
            $("#result").html(data);
          //after the result have been displayed the datatable function is called
          $('#dataTable').DataTable();
        }
      });
    } else {
      $("#result").html('');
    }
   }); 
 });

Codes from my findGrades.php

$term = mysqli_escape_string($connection, $_POST['term']);

$output .= '<td contenteditable="true" onBlur="saveToDatabase(this,'.$term.','.$row["score"].','.$row['id'].')">'.$row["score"].'</td>';

Based upon the result that was return(a table) I want to be able to edit a column in that table, which is the column I've shown here. That's why I added a contenteditable attribute on it.

Ajax is bringing the right result for the table, but when I tried to update I'am getting this error from my js console:

Uncaught ReferenceError: period_one is not defined at HTMLTableCellElement.onblur (gradesTry:1)

'period_one' is the value of one of the select element and also a term/ table

Here is how my complete script files looks:

<script>
    function saveToDatabase(editableObj,term,column,id) {
        $.ajax({
           url: "saveedit.php",
           method: "POST",
data:'&editval='+editableObj.innerHTML+'&term='+term+'&column='+column+'&id='+id,
           dataType:"text",
           success: function(data){
            $('#result').html(data);
           }        
       });
     }

$(document).ready(function() {
    $('#term').on('change', function() {
    var term = $('#term').val();
    if (term != '') {
      $.ajax({
          url:"findGrades.php",
          method:"post",
          data:{"term":term},
          dataType:"text",
          success:function(data){
            $("#result").html(data);
          //after the result have been displayed the datatable function is called
          $('#dataTable').DataTable();
        }
      });
    } else {
      $("#result").html('');
    }
   }); 
 });
</script>

Contents of saveedit.php

$column = $_POST["column"];
$value = $_POST['editval'];
$id = $_POST['id'];
$term = $_POST['term'];

$query = "UPDATE $term SET $column = '{$value}' WHERE id = $id";
$result = mysqli_query($connection, $query);
if ($result && mysqli_affected_rows($connection) >= 0){
        echo "updated successfully";
} else {
    die("Database query failed. ". mysqli_error($connection));
}

I'm very sure that I'm not doing things the right way. Will appreciate if you can point me in the right direction on how to do things correctly. Thanks.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog