我正在尝试使用ExtJS创建一个简单的表单,并将值发送到数据库

rseugnpd  于 2022-09-26  发布在  其他
关注(0)|答案(2)|浏览(194)

我正在尝试使用ExtJS创建一个简单的表单并将值发送到数据库,但它没有将我输入的值插入表单。我不知道为什么它没有将数据输入到数据库。
PS:我正在使用Extjs 5
我的Extjs表格:

Ext.define('Grid.view.main.Form', {
        extend: 'Ext.form.Panel',
        xtype:'form',
        title: 'User Form',
        id:'myformpanel',
        height: 120,
        width: '100%',
        autoScroll:true,
        defaults: {
            xtype: 'textfield',
            labelAlign: 'top',
            padding: 10
        },
        layout: {
            type: 'hbox'
        },

        items: [{

                fieldLabel: 'TEST1',
                name: 'Test1'
            },{
                fieldLabel: 'TEST2',
                name: 'Test2'
            },{
                fieldLabel: 'TEST3',
                name: 'Test3'
            },{
                fieldLabel: 'TEST4',
                name: 'Test4'
            },{
                fieldLabel: 'TEST5',
                name: 'Test5'
            },{
                fieldLabel: 'TEST6',
                name: 'Test6'
            },{
                fieldLabel: 'TEST7',
                name: 'Test7'
            },{
                fieldLabel: 'TEST8',
                name: 'Test8'
            },{
                fieldLabel: 'TEST9',
                name: 'Test9'
            },{
                fieldLabel: 'TEST10',
                name: 'Test10'
            },{
                fieldLabel: 'TEST11',
                name: 'Test11'
            },{
                fieldLabel: 'TEST12',
                name: 'Test12'
            },

        ],

         buttons: [{
            text: 'login',
            name:'submit',

        handler: function(){
            Ext.Ajax.request({
        url: 'data/testform.php',
        method: 'POST',
        params: Ext.getCmp('myformpanel').getForm().getFieldValues(),
success: function(response){       
    Ext.Msg.alert('success ' + response.status);
},
failure: function(response){
    Ext.Msg.alert('server-side failure with status code ' + response.status);
}
});
                }
          });
        }
    }]

testform.php

<?php

require_once('database_connection.php');

if(isset($_POST['submit'])){

    $Test1=$_POST['Test1'];
        $Test2=$_POST['Test2'];
        $Test3=$_POST['Test3'];
        $Test4=$_POST['Test4'];
        $Test5=$_POST['Test5'];
        $Test6=$_POST['Test6'];
        $Test7=$_POST['Test7'];
        $Test8=$_POST['Test8'];
        $Test9=$_POST['Test9'];
        $Test10=$_POST['Tes10t'];
        $Test11=$_POST['Test11'];
        $Test12=$_POST['Test12'];

        $query="INSERT INTO testexcel (Test1,Test2,Test3,Test4,Test5,Test6,Test7,Test8,Test9,Test10,Test11,Test12) VALUES ('$Test1','$Test2','$Test3','$Test4','$Test5','$Test6','$Test7','$Test8','$Test9','$Test10','$Test11','$Test12')";
}

?>
evrscar2

evrscar21#

我猜你的请求正文是空的,不是吗?尝试将表单数据插入Ajax请求的params:配置中。http://docs.sencha.com/extjs/4.2.0/#!/api/Ext.Ajax接口

Ext.Ajax.request({
    url: 'data/testform.php',
    method: 'POST',
    params: this.ownerCt.ownerCt.getForm().getFieldValues(),
    success: function(response){       
        Ext.Msg.alert('success ' + response.status);
    },
    failure: function(response){
        Ext.Msg.alert('server-side failure with status code ' + response.status);
    }
});

this.ownerCt.ownerCt.getForm()并不是访问表单的最佳方式,尽管我在您的示例中找不到其他方式来访问它。如果您更喜欢,可以给表单一个id,并使用Ext.getCmp(id)。这看起来像params: Ext.getCmp('myformpanel123').getForm().getFieldValues()

bvuwiixz

bvuwiixz2#

感谢@Hown_先生的帮助……我知道为什么不将数据插入数据库……因为我的数据库配置错误,所以这是我的代码
//我的extjs表单面板

Ext.define('Grid.view.main.Form', {
    extend: 'Ext.form.Panel',
    xtype:'form',

    title: 'User Form',
    id:'myformpanel',
    height: 120,
    width: '100%',
    autoScroll:true,
    defaults: {
        xtype: 'textfield',
        labelAlign: 'top',
        padding: 10
    },
    layout: {
        type: 'hbox'
    },

    items: [
        {
            fieldLabel: 'TEST1',
            name: 'Test1'

        },{
            fieldLabel: 'TEST2',
            name: 'Test2'
                    },{
            fieldLabel: 'TEST3',
            name: 'Test3'
                   },{
            fieldLabel: 'TEST4',
            name: 'Test4'
                    },{
            fieldLabel: 'TEST5',
            name: 'Test5'
                    },{
            fieldLabel: 'TEST6',
            name: 'Test6'
                    },{
            fieldLabel: 'TEST7',
            name: 'Test7'
                    },{
            fieldLabel: 'TEST8',
            name: 'Test8'
                    },{
            fieldLabel: 'TEST9',
            name: 'Test9'
                    },{
            fieldLabel: 'TEST10',
            name: 'Test10'

        },{
            fieldLabel: 'TEST11',
            name: 'Test11'

        },{
            fieldLabel: 'TEST12',
            name: 'Test12'

        },

    ],

    buttons: [{

        text: 'login',
        name:'submit',

    handler: function()
    {

        Ext.Ajax.request({
            url: 'data/testform.php',
            method: 'POST',
            params: Ext.getCmp('myformpanel').getForm().getFieldValues(),
                success: function(response){       
                 Ext.Msg.alert('success ' + response.status);
    },
                failure: function(response){
                Ext.Msg.alert('server-side failure with status code ' + response.status);
    }
    });

    }]

});

//我的测试表单.php

<?php

require_once('database_connection.php');

    $Test1= isset($_POST['Test1']) ? $_POST['Test1'] : NULL;
    $Test2= isset($_POST['Test2']) ? $_POST['Test2'] : NULL;
    $Test3= isset($_POST['Test3']) ? $_POST['Test3'] : NULL;
    $Test4= isset($_POST['Test4']) ? $_POST['Test4'] : NULL;
    $Test5= isset($_POST['Test5']) ? $_POST['Test5'] : NULL;
    $Test6= isset($_POST['Test6']) ? $_POST['Test6'] : NULL;
    $Test7= isset($_POST['Test7']) ? $_POST['Test7'] : NULL;
    $Test8= isset($_POST['Test8']) ? $_POST['Test8'] : NULL;
    $Test9= isset($_POST['Test9']) ? $_POST['Test9'] : NULL;
    $Test10= isset($_POST['Test10']) ? $_POST['Test10'] : NULL;
    $Test11= isset($_POST['Test11']) ? $_POST['Test11'] : NULL;
    $Test12= isset($_POST['Test12']) ? $_POST['Test12'] : NULL;

        $query="INSERT INTO testexcel (Test1,Test2,Test3,Test4,Test5,Test6,Test7,Test8,Test9,Test10,Test11,Test12) VALUES ('$Test1','$Test2','$Test3','$Test4','$Test5','$Test6','$Test7','$Test8','$Test9','$Test10','$Test11','$Test12')";
        mysql_query($query,$con);

?>

希望这段代码能帮助像我这样的初学者。。。。
谢谢

相关问题