phpAjax 例子

Posted 2240199847

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了phpAjax 例子相关的知识,希望对你有一定的参考价值。

PHP

<?php
$action
= $_GET[action]; switch ($action) { case init_data_list: init_data_list(); break; case add_row: add_row(); break; case del_row: del_row(); break; case edit_row: edit_row(); break; } // 列表 function init_data_list(){ $data = array(); $query = query_sql(select * from `et_data` order by `id` asc); while($row = $query->fetch_assoc()){ $data[] = $row; } echo json_encode($data); } //删除 function del_row(){ $dataid = $_POST[dataid]; $sql = "DELETE FROM `et_data` WHERE `id` = " . $dataid; if(query_sql($sql)){ echo "ok"; } else { echo "db error ..."; } } //添加 function add_row(){ $sql = INSERT INTO `et_data` ( `c_a`,`c_b`,`c_c`,`c_d`,`c_e`,`c_f`,`c_g`,`c_h`) VALUES(; for($i=0; $i<8; $i++){ $sql .= \‘ . $_POST[col_ .$i] . \‘, ; } $sql = trim($sql,", "); $sql .= ")"; if($res = query_sql($sql,"SELECT LAST_INSERT_ID() as LD")){ $d = $res->fetch_assoc(); echo $d[LD]; } else { echo "db error ..."; } } //修改 function edit_row(){ $sql = UPDATE `et_data` SET ; $id = $_POST[id]; unset($_POST[id]); for($i = 0 ; $i < 8 ; $i++){ $sql .= `c_ . chr(97+$i) . ` = \‘ . $_POST[col_ . $i] . \‘ , ; } $sql = trim($sql,", "); $sql .= WHERE `id` = . $id ; if(query_sql($sql)){ echo "ok"; } else { echo "db error ..."; } } // 初始化数据库 function query_sql(){ $mysqli = new mysqli("127.0.0.1", "root", "root", "etable"); $sqls = func_get_args(); foreach($sqls as $s){ $query = $mysqli->query($s); } $mysqli->close(); return $query; }

 

JS

$(function(){

    var g_table = $(‘table.data‘);
    var init_data_url = "data.php?action=init_data_list";

    $.get(init_data_url, function(data){
        var row_items = $.parseJSON(data);
        for (var i = 0, j = row_items.length; i < j; i++) {
            var data_dom = create_row(row_items[i]);
            g_table.append(data_dom);
        }
    });

    //删除事件
    function delHandler(){
        if (confirm(‘确定要删除吗?‘)) {
            var data_id = $(this).attr("dataid");
            var meButton = $(this);
            $.post("data.php?action=del_row",{dataid:data_id},function(res){
                    if("ok" == res)    {
                        $(meButton).parent().parent().remove();
                    } else {
                        alert("删除失败...");    
                    }
            });
        }
    }

    //修改事件
    function editHandler(){
        var data_id = $(this).attr("dataid");
        var meButton = $(this);
        var meRow = $(this).parent().parent();//没有事件

        var editRow = $("<tr></tr>");

        for (var i = 0; i < 8; i++) {
            var editTd = $("<td><input class=‘textField‘ type=‘text‘ /></td>");
            var v = meRow.find(‘td:eq(‘+ i +‘)‘).html();
            editTd.find(‘input‘).val(v);
            editRow.append(editTd);
        }

        var opt_td = $("<td></td>");
        var saveButton = $(‘<a class="opLink" href="javascript:">确认&nbsp;</a>‘);
        saveButton.click(function(){
            var currentRow = $(this).parent().parent();
            var input_fields = currentRow.find("input");
            var post_fields = {};
            for( var i = 0 , j = input_fields.length; i < j; i++){
                    post_fields[‘col_‘ + i]     = input_fields[i].value;
            }
            post_fields[‘id‘] = data_id;
            $.post(‘data.php?action=edit_row‘,post_fields,function(res){
                if("ok" == res){
                    var newUpdateRow = create_row(post_fields);        
                    currentRow.replaceWith(newUpdateRow);
                } else {
                    alert("数据更新失败...");    
                }
            });
        });


        var canceButton = $(‘<a class="opLink" href="javascript:">&nbsp;取消</a>‘);
        canceButton.click(function(){
            var currentRow = $(this).parent().parent();
            meRow.find(‘a:eq(0)‘).click(delHandler);
            meRow.find(‘a:eq(1)‘).click(editHandler);
            currentRow.replaceWith(meRow);//需重新绑定事件
        })

        opt_td.append(saveButton);
        opt_td.append(canceButton);

        editRow.append(opt_td);
        meRow.replaceWith(editRow);
    }

    //列表
    function create_row(data_item){
        var row_obj = $("<tr></tr>");
        for(var k in data_item){
            if ("id" != k) {
                var col_td = $("<td></td>");
                col_td.html(data_item[k]);
                row_obj.append(col_td);
            }
        }

        var delButton = $(‘<a class="opLink" href="javascript:">删除&nbsp;</a>‘);
        delButton.attr("dataid", data_item[‘id‘]);
        delButton.click(delHandler);


        var editButton = $(‘<a class="opLink" href="javascript:">编辑&nbsp;</a>‘);
        editButton.attr("dataid", data_item[‘id‘]);
        editButton.click(editHandler);

        var opt_td = $(‘<td></td>‘);
        opt_td.append(delButton);
        opt_td.append(editButton);
        row_obj.append(opt_td);
        return row_obj;
    }

    // 添加
    $("#addBtn").click(function(){
        var addRow = $("<tr></tr>");

        for (var i = 0; i < 8; i++) {
            var col_td = $("<td><input type=‘text‘ class=‘textField‘ /></td>")
            addRow.append(col_td);
        }

        var col_opt = $("<td/></td>");

        var confirmBtn = $(‘<a class="opLink" href="javascript:">确认&nbsp;</a>‘);
        confirmBtn.click(function(){
            var currentRow = $(this).parent().parent();
            var input_fields= currentRow.find(‘input‘);
            var post_fields = {};
            for(var i = 0 , j = input_fields.length; i< j ; i++){
                    post_fields[‘col_‘ + i]    = input_fields[i].value;
            }
            $.post("data.php?action=add_row", post_fields, function(res){
                if( 0 < res){
                    post_fields[‘id‘] = res;
                    var postAddRow = create_row(post_fields);
                    currentRow.replaceWith(postAddRow);
                } else {
                    alert("插入失败...");    
                }
            })
        });

        var cancelBtn = $(‘<a class="opLink" href="javascript:">&nbsp;取消</a>‘);
        cancelBtn.click(function(){
            $(this).parent().parent().remove();
        });

        col_opt.append(confirmBtn);
        col_opt.append(cancelBtn);

        addRow.append(col_opt);
        g_table.append(addRow);
    });

});

 


以上是关于phpAjax 例子的主要内容,如果未能解决你的问题,请参考以下文章

分享几个实用的代码片段(附代码例子)

分享几个实用的代码片段(附代码例子)

PHPAjax跨域解决方案 jsonpcors

前端小白之每天学习记录----phpajax

如何创建片段以重复变量编号中的代码行

为啥这个代码片段说包含错误?