weixin_33674437 2014-11-23 03:35 采纳率: 0%
浏览 18

JTable Ajax发布不起作用

I am using the example code straight from the JTable website for PHP. I cannot get values from the AJAX POST.

The javascript in my JTableSimple.php:

$(document).ready(function () {

        //Prepare jTable
        $('#PeopleTableContainer').jtable({
            title: 'Table of people',
            actions: {
                listAction: 'PersonActions.php?action=list',
                createAction: 'PersonActions.php?action=create',
                updateAction: 'PersonActions.php?action=update',
                deleteAction: 'PersonActions.php?action=delete'
            },
            fields: {
                PersonId: {
                    key: true,
                    create: false,
                    edit: false,
                    list: false
                },
                Name: {
                    title: 'Author Name',
                    width: '40%'
                },
                Age: {
                    title: 'Age',
                    width: '20%'
                },
                RecordDate: {
                    title: 'Record date',
                    width: '30%',
                    type: 'date',
                    create: false,
                    edit: false
                }
            }
        });

        //Load person list from server
        $('#PeopleTableContainer').jtable('load');

    });

PersonActions.php (Just showing for list component):

require_once 'config.php';
// connecting to mysql
$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
// selecting database
mysql_select_db(DB_DATABASE);
//Getting records (listAction) 
if($_GET["action"] == "list")
{
    //Get Name
    $Name = $_POST['Name'];
    //SQL query
    $result = mysql_query("select * from user_data WHERE Name=$Name");
    //Add selected records into an array
    $rows = array();
    while($row = mysql_fetch_array($result))
    {
        $rows[] = $row;
    }

    //Return result to jTable
    $jTableResult = array();
    $jTableResult['Result'] = 'OK';
    $jTableResult['Records'] = $rows;
    print json_encode($jTableResult);
}

I am using XAMPP. I have so far tried:

  • Using var_dump($_POST) which returns nothing.
  • Re-installing XAMPP
  • Re-installing all the header files for JTable, JQuery, Jquery UI and all the css files.
  • Using GET instead of POST but this doesn't work either.

When I open 'PersonActions.php?action=list' I receive a notice that:

Notice: Undefined index: Name in C:\xampp\htdocs\PersonActions.php on line xx

Which I assume means that $_POST['Name'] doesn't exist?

  • 写回答

1条回答 默认 最新

  • from.. 2014-11-24 04:47
    关注

    I had the same problem. Try adding this before the actions component in your table properties:

    ajaxSettings: {
            type: 'POST', 
            data: {'VariableName': 'VariableValue'},
            url: './PersonActions.php'
            },
    

    This occurs as the action 'list' does not POST any data through AJAX methods to your PersonActions.php. You have to do it manually.

    For the other actions such as 'update' and 'delete' you don't need to make any additional changes, just use $_POST['Fieldname'] as shown in the JTable example code.

    ALSO: I would recommend installing Firebug. It's really useful for viewing POST data and provides an additional tool for debugging.

    EDIT #1 For master/child with PHP: If you want to add a child table, add the following code as an additional field in your master table. I have named the field 'ChildTableButton'.

               fields: {
                    ChildTableButton: {
                            title: '',
                            width: '3%',
                            sorting: false,
                            edit: false,
                            create: false,
                            display: function (ChildData) {
                                //Image that will open child table
                                var $img = $('<img src="./img/more.png"/>');
                                //Open child table when user clicks the image
                                $img.click(function () {
                                    $('#PeopleTableContainer').jtable('openChildTable',
                                            $img.closest('tr'),
                                            {
                                                title: 'Title',
                                                actions: {
                                                    listAction: './tableactions.php?action=listchild',
                                                   updateAction:'./tableactions.php?action=updatechild',
                                                    deleteAction: './tableactions.php?action=deletechild'
                                                    },
                                                fields: {
                                                    Field1: {
                                                        title: 'Field 1 child table',
                                                        key: true,
                                                        create: false,
                                                        edit: false,
                                                        width: '50%'
                                                    },
                                                    Field2: {
                                                        title: 'Field 2 child table',
                                                        width: '50%'
                                                    }
                                                }
                                            }, function (data) { //opened handler
                                                data.childTable.jtable('load');
                                            });
                                });
                                //Return image to show on the person row
                                return $img;
                            }
                        },
    

    You can then deal with PHP side by checking if action='listchild' etc, in the same way as your master table.

    Hope this helps!

    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题