Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AlanGreyjoy's avatar

Any way to stop replacing . with _ during post or just pass files during ajax

I was wondering if there was a way to stop replacing . with _ during a post.

If i send my data through ajax as data: {blah.1.foo_bar:'hey'}, The periods are kept. But any other way, it seems laravel is auto replacing the periods with underscores :(

Or better yet, if I could just set files: during my data, that would be awesome. But I get an illegal invocation when I do this.

$(document).on('click', '.btnUpdate', function () {
            let _token = $('input[name="_token"]').val();
            let templateID = $('#template_id').val();
            let accountID = $('#acc_id').val();
            let form = $('#updateForm').serializeArray();
            let files = $('#wallpaper_upload\.url').prop('files')[0];

            $.ajax({
                type: 'post',
                url: '{{route('apps.phonemanager.autoprovision.update')}}',
                data: {
                    _token:_token,
                    templateID: templateID,
                    accountID: accountID,
                    form: form,
                    files: files,
                },
                success: function (response) {
                    toastr.success(response);
                },
                error: function (response) {
                    toastr.error(response.responseJSON);
                }
            });
        });

Unfortunately, leaving the dots in the parameter names is required for this area.

0 likes
5 replies
lostdreamer_nl's avatar

Uploading files via javascript cannot simply be done by putting it in the POST data, you'll either have to (base64) encode it before sending it (and decode on the other side) or use a FormData object (preferred way)

https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects

About the dot notation, it's PHP itself doing that, not laravel:

http://www.php.net/manual/en/language.variables.external.php

"Dots and spaces in variable names are converted to underscores. For example <input name="a.b" /> becomes $_REQUEST["a_b"]. "

AlanGreyjoy's avatar

FormData converts all the dots to underscores. I am still trying other ways.

And thanks on the clarification of the dot notation.

AlanGreyjoy's avatar

Can you pass new params in FormData?

Like, fd.append('foo', 'bar') ?

Then i could pass all the form options as an json string and still get the file uploads.

lostdreamer_nl's avatar

@alangreyjoy check the link I gave with it's documentation ;) it's on the top:

var formData = new FormData();

formData.append("username", "Groucho");
formData.append("accountnum", 123456); // number 123456 is immediately converted to a string "123456"

// HTML file input, chosen by user
formData.append("userfile", fileInputElement.files[0]);

// JavaScript file-like object
var content = '<a id="a"><b id="b">hey!</b></a>'; // the body of the new file...
var blob = new Blob([content], { type: "text/xml"});

formData.append("webmasterfile", blob);

var request = new XMLHttpRequest();
request.open("POST", "http://foo.com/submitform.php");
request.send(formData);

You can mix normal fields and files, just like in a normal form (just make a blob of the file first if it wasn't yet)

Going down the dot notation is going to be very hard to maintain because if the other side is programmed in PHP, you are still going to lose those dots in favor of underscores.

You could always create a separate Request class for you app, that simply takes the request input, and renames the keys back into dot notation.

Or perhaps, post a deeply nested javascript object (which turns into a PHP array after post) which you can read using laravel's dot notation, so posting:

formData.append("testinput", {
    test: {
        user: {
            name: 'Lost',
            email: [email protected]
        }
    }
});

can be read in php as:


$data = array_dot($request->get('testinput'));

$name = $data['test.user.name'];
$email = $data['test.user.email'];

AlanGreyjoy's avatar

Yea thanks!!

I forgot to post back, I did in fact read the link. All is well now.

Sent the form options as json strings and now everything is moving full steam.

Thanks!!

Please or to participate in this conversation.