dongpu1315 2018-07-14 15:12
浏览 83
已采纳

为什么AJAX函数不能立即从DB中获取数据?

I have 2 AJAX scripts:

1st makes HTML-table's first cell editable and writes entered value directly into DB-table;

2nd fetches data from DB-table and shoes in HTML-table's second cell.

Sequence is this:

  1. User clicks on first HTML-table's cell, edit it. AJAX script writes it to DB.

  2. PHP-script adds this data to some variable and writes answer in DB.

  3. AJAX-script immediately fetches data from DB and shoes in second HTML-tables's cell.

Scripts are this: 1. Edit.js:

   function showEdit(editableObj) {
     $(editableObj).css("background","#FFF");
   } 

  function saveToDatabase(editableObj,column,id) {
    $(editableObj).css("background","#FFF url(loaderIcon.gif) no-repeat right");
  $.ajax({
    url: "days.php",
    type: "POST",
    data:'column='+column+'&editval='+editableObj.innerHTML+'&id='+id,
    success: function(data){
     $(editableObj).css("background","#FDFDFD");

      //$(editableObj).css("background","#FDFDFD");
     // $("#birthday-data").replaceWith(data); //The .replaceWith() method removes content from the DOM and inserts new content in its place with a single call
    }        
   });
}

2. Show.js:

$(document).ready(function() {           

  $.ajax({    // create an ajax request
    type: "POST",
    url: "days.php",             
    dataType: "text",   // expect html to be returned                
    success: function(response){                    
        $("#one").html(response); // get the element having id of "one" and put the response inside it
        //alert(response);

  }        

}

3. Table.php: // This is content part of PHP script with HTML-table

<td aria-label="First column" contenteditable="true" onBlur="saveToDatabase(this,'select1','<?php echo $show[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $show[$k]["select1"]; ?></td>
      <td id="one"><?php echo $value['day1']; ?></td>

The problem is that I have to refresh page to see my HTML-table 2 cell updated. Expect it be updated immediately after first cell's data edited. Think problem is in show.php, can't get it. Need some help!

  • 写回答

2条回答 默认 最新

  • duanshangying5102 2018-07-14 20:14
    关注
    function saveToDatabase(editableObj,column,id) {
        $(editableObj).css("background","#FFF url(loaderIcon.gif) no-repeat right");
      $.ajax({
        url: "days.php",
        type: "POST",
        data:'column='+column+'&editval='+editableObj.innerHTML+'&id='+id,
        success: function(data){
         $(editableObj).css("background","#FDFDFD");
    
          //$(editableObj).css("background","#FDFDFD");
         // $("#birthday-data").replaceWith(data); //The .replaceWith() method removes content from the DOM and inserts new content in its place with a single call
    
        $("#one").text(data);
        }        
       });
    }
    

    I believe you're just trying to get the response from days.php to show in the table cell... Well, this is how you do it. 'data' is the expected return from days.php, so days.php would echo out whatever you want #one table cell to show after 'column='+column+'&editval='+editableObj.innerHTML+'&id='+id is sent to that page... And then it would just put it right into the cell.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效