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

AlexDFCH's avatar

URL got changed after I send a POST request to somewher else!

Hi,

In my form on VILT stack, I have a watcher for country selection, when I choose a country, it gets provinces of its:

//On Place/Edit page:
watch(country_id, throttle((newCountry_id) => {
    router.visit(route('admin.places.partial_data.get_provinces'), {
        method: 'post',
        data: {page_path:'Places/Edit', country_id: newCountry_id},
        only: ['provinces'],
        replace: true,
        preserveState: true,
        preserveScroll: true,
    })

}, 500))

and back-end:

//Controller:
public function getProvinces(Request $request)
    {
        $validated = $request->validate([
            'country_id' => 'required|max:36',
            'page_path' => [
                'required',
                Rule::in(['Places/Edit', 'Places/Create']),
            ],
        ]);

        $provinces = Place::provinces($validated['country_id'])->get();

        return Inertia::render('Admin/'.$validated['page_path'], [
            'provinces' => $provinces,
        ]);
}
//Route:
Route::prefix('partial_data')->name('partial_data.')->group(function () {
            Route::post('/get_provinces', [PlaceController::class, 'getProvinces'])->name('get_provinces');
        });

I don't want repeat my self and make two getProvinces method for Create part and Edit part so I send page_path and send back response to where it comes.

It works fine. only problem is when I choose country my URL change to "/admin/places/partial_data/get_provinces". page is same and works correctly but url changed.

Actually, when we send data to a url with POST for getting some data back, Why URL should change?! It's not make sence!

what I should to do for preventing changing url?

Second question:

When you want make just one method to send some data to two different pages, are you agree to get partial data like that? How you code in same situation?

Thank you.

0 likes
4 replies
tisuchi's avatar

@alexdfch Can you try replacing router.visit with the following?

Inertia.post(route('admin.places.partial_data.get_provinces'), {
    page_path: 'Places/Edit',
    country_id: newCountry_id,
}, {
    only: ['provinces'],
    replace: true,
    preserveState: true,
    preserveScroll: true,
    onSuccess: (page) => {
        this.$page.props.provinces = page.props.provinces;
    }
});
AlexDFCH's avatar

@tisuchi Thank you for your help, I did it but it doesn't fixed the problem.

URL still get changed.

AlexDFCH's avatar

Someone asks same question 2 years ago and no one answer that. https://stackoverflow.com/questions/65396586/

I think maybe my question is wrong. Maybe we should not use Inertia like that.

Is it ok if we send POST request to other route? or it's a Inertia bug?

If we should get all our data from same url so we should write a method for any page and it's against DRY.

What I'm missing? what is my misunderstanding?

I would be happy if you share your opinion about this with me.

Thank you.

Please or to participate in this conversation.