Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newbie issue - $_REQUEST don't populate #1508

Closed
Caumaker opened this issue Feb 6, 2017 · 8 comments
Closed

Newbie issue - $_REQUEST don't populate #1508

Caumaker opened this issue Feb 6, 2017 · 8 comments

Comments

@Caumaker
Copy link

Caumaker commented Feb 6, 2017

First of all, really thanks for W2UI! It's amazing!

I am a newbie to w2ui and I have serios problems with something really basic.

I am trying to get data from my server (LAMP) but I get always the same (Command "" is not recognized). I did everything that I know. I try for more then 15 hours and nothing.

image

I get data only when I assembly a array in PHP and encode to Json to pass with an ECO.
But I want to make it work with the default method to send request.

I tried to use the example that is in the folder " /server/php " and get the same issue. I try all examples that have server connection.

My $_REQUEST array do not receive data, if I send an var in the url. ...php?param=1 I get it into the users.php. But I do not receive $_REQUEST['cmd'] and the other vars.

Please some one help me. Thanks in advance.

My code:
users.php I did customization in server, user, pass...
`<?php

require("w2db.php");
require("w2lib.php");

$db = new dbConnection("mysql");
$db->connect("localhost", "xxx", "xxx", "tdr_DB", "3306");

switch ($_REQUEST['cmd']) {

case 'get':
    if (array_key_exists('recid', $_REQUEST)){  // if true , then is a 'get-record' only one record with recid
        $sql = "SELECT userid, fname, lname, email, login, password
                FROM users
                WHERE userid = ".$_REQUEST['recid'];
        $res = $w2grid->getRecord($sql);
    }
    else{        
        $sql  = "SELECT * FROM users       
                 WHERE ~search~ ORDER BY ~sort~";
        $res = $w2grid->getRecords($sql, null, $_REQUEST);
    }  
     $w2grid->outputJSON($res);
    break;

case 'delete':
    $res = $w2grid->deleteRecords("users", "userid", $_REQUEST);
    $w2grid->outputJSON($res);
    break;

//case 'get-record':
//    break;

case 'save':
    $res = $w2grid->saveRecord('users', 'userid', $_REQUEST);
    $w2grid->outputJSON($res);
    break;

default:
    $res = Array();
    $res['status']  = 'error';
    $res['message'] = 'Command "'.$_REQUEST['cmd'].'" is not recognized.';
    $res['postData']= $_REQUEST;
    $w2grid->outputJSON($res);
    break;

}
`

in index.php is:
`

<script type="text/javascript" src="../../libs/jquery/jquery-2.1.4.min.js"></script> <script type="text/javascript" src="../../dist/w2ui.min.js"></script>
<script> $(function () { // define and render grid $('#users').w2grid({ name : 'users', url : 'users.php', header : 'List of Users', show: { header : true, toolbar : true, footer : true, toolbarAdd : true, toolbarDelete : true }, columns: [ { field: 'fname', caption: 'First Name', size: '150px', searchable: true }, { field: 'lname', caption: 'Last Name', size: '150px', searchable: true }, { field: 'email', caption: 'Email', size: '100%', searchable: true }, { field: 'login', caption: 'Login', size: '150px', searchable: true } ], onAdd: function (event) { editUser(0); }, onDblClick: function (event) { editUser(event.recid); } });
// defined form https://api.myjson.com/bins/6ayrt
$().w2form({
    name     : 'user_edit',
    url     : 'users.php',
    style     : 'border: 0px; background-color: transparent;',
    formHTML:
        '<div class="w2ui-page page-0">'+
        '    <div class="w2ui-label">First Name:</div>'+
        '    <div class="w2ui-field">'+
        '        <input name="fname" type="text" size="35"/>'+
        '    </div>'+
        '    <div class="w2ui-label">Last Name:</div>'+
        '    <div class="w2ui-field">'+
        '        <input name="lname" type="text" size="35"/>'+
        '    </div>'+
        '    <div class="w2ui-label">Email:</div>'+
        '    <div class="w2ui-field">'+
        '        <input name="email" type="text" size="35"/>'+
        '    </div>'+
        '    <div class="w2ui-label">Login:</div>'+
        '    <div class="w2ui-field">'+
        '        <input name="login" type="text" size="25"/>'+
        '    </div>'+
        '    <div class="w2ui-label">Password:</div>'+
        '    <div class="w2ui-field">'+
        '        <input name="password" type="password" size="25"/>'+
        '    </div>'+
        '</div>'+
        '<div class="w2ui-buttons">'+
        '    <input type="button" value="Cancel" name="cancel">'+
        '    <input type="button" value="Save" name="save">'+
        '</div>',
    fields: [
        { name: 'fname', type: 'text', required: true },
        { name: 'lname', type: 'text', required: true },
        { name: 'email', type: 'email' },
        { name: 'login', type: 'text', required: true },
        { name: 'password', type: 'text', required: false },
    ],
    actions: {
        "save": function () {
            this.save(function (data) {
                if (data.status == 'success') {
                    w2ui['users'].reload();
                    $().w2popup('close');
                }
                // if error, it is already displayed by w2form
            });
        },
        "cancel": function () {
            $().w2popup('close');
        },
    }
});

});

function editUser(recid) {
$().w2popup('open', {
title : (recid == 0 ? 'Add User' : 'Edit User'),
body : '

',
style : 'padding: 15px 0px 0px 0px',
width : 500,
height : 300,
onOpen : function (event) {
event.onComplete = function () {
w2ui['user_edit'].clear();
w2ui['user_edit'].recid = recid;
$('#w2ui-popup #user_edit').w2render('user_edit');
}
}
});
}
</script>

`

I created the table in mysql. I am able to get data with my functions..
image

Sorry this basic question, but I did everything that I know.

@Caumaker Caumaker changed the title Newbie issue - $_REQUEST don't populate GRID Newbie issue - $_REQUEST don't populate Feb 6, 2017
@Caumaker
Copy link
Author

Caumaker commented Feb 6, 2017

I keep trying...
When I set different datatype.... the issue change..
I inserted the line w2utils.settings.dataType = 'HTTP'; before the Grid
image


image

@Caumaker
Copy link
Author

Caumaker commented Feb 6, 2017

When I try using a jquery ajax function .. using datatype: json, I am able to send POST and populate the $_REQUEST. But with the default Grid function not.

image

results:
image

@Caumaker
Copy link
Author

Caumaker commented Feb 7, 2017

Hi there, I keep trying solve this issue.
Now I am able to populate the $_REQUEST, doing the follow:
keep original the index.php
image

And decoding the HTTPJSON received:
$params = json_decode($_REQUEST['request'], true);
image

Results:
image

I saw all exemples that I found and nobody appear to being so (complex/uggly) as my solution. Maybe I don't know some php.ini settings that must be enable to allow the normal method work.
Please, If someone knows why I am not able to use normal way, say me.
Thanks

@mpf82
Copy link
Collaborator

mpf82 commented Feb 7, 2017

My guess: the default data type for server communication has changed in w2ui 1.5 and all the examples are probably still for w2ui 1.4

Default dataType in 1.4: HTTP
Default dataType in 1.5: HTTPJSON

You can change the data type as follows. Valid values are HTTP, HTTPJSON, RESTFULL, RESTFULLJSON, JSON (case sensitive).

w2utils.settings.dataType = 'JSON';

For more information on the differences in 1.4 / 1.5 you should visit this thread: #1351

@mrg2001
Copy link
Collaborator

mrg2001 commented Feb 11, 2017

Please, very please, write short bug description. Respect to other people time is appreciated :). @mpf82 response seems to be sound. Reopen the ticket if it did not help.

@mrg2001 mrg2001 closed this as completed Feb 11, 2017
@AlexVDem
Copy link

AlexVDem commented Jun 16, 2020

I have the same issue, I always got "Command "" is not recognized." Seems to me w2grid() do not returns $_REQUEST with "get", "get-records" or something like that. Do you have any solution?

@mpf82
Copy link
Collaborator

mpf82 commented Jun 17, 2020

@AlexKovynev If you're using the latest master then "get" will no longer be sent to the server for a "get" request and in addition "cmd" has been renamed to "action" for "delete"/"save" commands.

That means, if the data does not contain an "action" attribute, you should handle it as "get".

If you're using 1.5 RC1 then see my answer above.

You could always inspect your browser's network tab in the dev console to see what exactly gets sent to your server, or just inspect all the data that your server receives.

@AlexVDem
Copy link

@AlexKovynev If you're using the latest master then "get" will no longer be sent to the server for a "get" request and in addition "cmd" has been renamed to "action" for "delete"/"save" commands.

That means, if the data does not contain an "action" attribute, you should handle it as "get".

If you're using 1.5 RC1 then see my answer above.

You could always inspect your browser's network tab in the dev console to see what exactly gets sent to your server, or just inspect all the data that your server receives.

Thanks for reply. I noticed, than new version make a new problems. :) I've set
w2utils.settings.dataType = 'JSON';
and
url: {
get: 'users.php',
save: 'users.php',
remove: 'users.php'
},
and now I get correct JSON string to my users.php (like {"cmd":"get","selected":[],"limit":5,"offset":0}).
But I have another issue: users.php (in fact - getRecords() from w2lib.php) returns strange data array looks like:
ERROR: Server communication failed.
EXPECTED: {status: "success", total: 5, records: Array(1)}
OR: {status: "error", message: "error message"}
RECEIVED: ---Array
---Array
(
[0] => Array
(
[0] => 18
)

)
---Array
(
[0] => Array
(
[0] => 1
[1] => Name1
[2] => LastName1
[3] => name1@mail.net
[4] => Name1
[5] => 1234567
)

[1] => Array
    (
        [0] => 2
        [1] => Name2
        [2] => LastName2
        [3] => name2@mail.net
        [4] => Name2
        [5] => 11223
    )

[2] => Array
    (
        [0] => 3
        [1] => Name3
        [2] => LastName3
        [3] => name3@mail.net
        [4] => Name3
        [5] => 11223
    )

)
As result I got error 500 (Internal Server Error) and "AJAX error. See console for more details." Seems to me, I always receive wrong array and json_encode($data) can't encode it in JSON string for w2grid().
Buy the way, I can use "add record" button, and add records to MySQL and it works almost correct (do not close popup window after Save).
Do you have any idea, what's wrong with getrecords()? I will appreciate for help. Thanks.
PS My code in attached file.

w2grid.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants