duanduanxi9441 2016-11-28 15:59
浏览 199

从表格单元更新数据库而不刷新页面

I want to make editable cell in table, then when hit 'enter' update database without refreshing whole page.

first of all I populate my table from sql query like:

//up there - no important things

echo'<table> 
        <tr>';                  
        while($row = mysql_fetch_assoc($result))
        {                       
            foreach($row as $key=>$value)
            {
               if(!($value == "id_p" || $value == "pass" || $value == "salt" || $value == "login"))
               {               
               echo'<th>'.$value.'</th>';
               }
            }
        } 
        echo '</tr>';

        if(mysql_num_rows($result2) > 0)
        {
            while($row2 = mysql_fetch_assoc($result2))
            {                           
                echo '<tr>';
                foreach($row2 as $key=>$value)
                {                           
                    if(!($key == "id_p" || $key == "pass" || $key == "salt" || $key == "login"))
                       echo'<td>'.$value.'</td>';
                }
                echo '</tr>';
            }
        }
        else
            echo '<tr><td colspan="9">Brak wyników.</td></tr>';

echo'</table>';

Then i change cell <td> with script (not my script) to

$(function(){
        $("td").click(function(event)
        {
            if($(this).children("input").length > 0)
                    return false;
            var tdObj = $(this);
            var preText = tdObj.html();
            var inputObj = $("<input type='text' />");
            tdObj.html("");
            inputObj.width(tdObj.width())
                .height(tdObj.height())
                .css({border:"0px",fontSize:"17px"})
                .val(preText)
                .appendTo(tdObj)
                .trigger("focus")
                .trigger("select");
            inputObj.keyup(function(event){
                if(13 == event.which) // if ENTER
                { 
                    var text = $(this).val();
                    tdObj.html(text);

 // SOMETHING HERE TO UPDATE DATABASE??

                }
                else if(27 == event.which) {  // if ESC
                        tdObj.html(preText);
                }
              });
                inputObj.click(function(){
                        return false;
                });
        });
});

And now i'm stuck. How can i send new data to update(without refreshing whole page)? I'm not good in js/ajax. i've created new page update_db.php

if(isset($_POST['post_name']))
{
   echo 'processing...';
  //mysql things...
}

but this is all i have.

  • 写回答

1条回答 默认 最新

  • dpvr49226 2016-11-28 16:09
    关注

    You can't do that without refreshing unless you use AJAX.

    if(13 == event.which) // if ENTER
    { 
        var text = $(this).val();
        tdObj.html(text);
        $.POST("update_db.php", { tdVal: text });
    }
    

    You can now get the tdVal in the $_POST of update_db.php

    Also I recommend using mysqli instead of mysql since its removed from php 7.

    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算