POST/PUT Form Data format...

POST/PUT Form Data format...

clementibclementib Posts: 4Questions: 2Answers: 0

I have an issue. I'm using the editor to add and edit data from tables. When I go to post or put the data to my server, DataTables changes the Names of the inputs. For example, I may have an input called 'FirstName' but when the data posts, it shows up as 'data[FirstName]=Bryan'. It's placing my post data name in brackets and adding the word data to the beginning of each name. You can even see this functionality on the examples on this site. Is there a way to keep the name intact such as 'FirstName=Bryan'? Hopefully, I'm asking my question correctly. Thanks!

Answers

  • allanallan Posts: 61,451Questions: 1Answers: 10,055 Site admin
    edited September 2014

    Hi,

    The Editor manual describes how the client-side communicates with the server. The data[] syntax indicates that the data is being sent as an object - but many server-side environments will automatically decode into an object, but presumably not the one you are using!

    Either you could decode it, or use ajax.data to remap the data that Editor would send into something that you want - i.e. flatten the data object to the top level.

    Regards,
    Allan

  • clementibclementib Posts: 4Questions: 2Answers: 0
    edited September 2014

    Thanks for the response, allan. I think I'm really close to figuring this out. Here's what I have so far...

    edit: {
     type: 'PUT',
        url: '/api/data/_id_',
        data: function (d) {
            return $.extend({}, d, {
                FirstName : d.data.FirstName,
                LastName : d.data.LastName,
                PhoneNumber : d.data.PhoneNumber
            });
        }
    },
    

    When I do this, I get both the original data object and the new object. This works, but why is it sending both objects? The manual says that it should replace the original object. Here's what the string looks like...

    action=edit&data%5BFirstName%5D=Charde&data%5BLastName%5D=York&data%5BPhoneNumber%5D=(418)6623878&id=31&FirstName=Charde&LastName=York&PhoneNumber=(418)6623878
    
  • allanallan Posts: 61,451Questions: 1Answers: 10,055 Site admin

    The manual is correct, and I think the implementation might be wrong. I'm away from my main computer at the moment, but I'll check this when I get back.

    In the mean time, what you could do is:

    edit: {
     type: 'PUT',
        url: '/api/data/_id_',
        data: function (d) {
           $.extend(d, {
                FirstName : d.data.FirstName,
                LastName : d.data.LastName,
                PhoneNumber : d.data.PhoneNumber
            });
    
            delete d.data;
        }
    },
    

    So the original object will be used, but with the data property removed.

    Allan

  • allanallan Posts: 61,451Questions: 1Answers: 10,055 Site admin

    I've just checked this and it appears to be working as expected for me. If I do:

            ajax: {
                url: "../php/staff.php",
                data: function ( d ) {
                    return { a: 1 };
                }
            },
    

    then only a = 1 is sent to the server.

    Could you link me to an example page that shows the issue please? Also might be worth checking you are using the current release (1.3.3) if you aren't already.

    Allan

This discussion has been closed.