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

lara120618's avatar

View as response from post request

Is it a good practice to receive a view as a response of a post request?

0 likes
9 replies
jlrdw's avatar

Some code please. I usually use get for that, a post / put I am updating data, i.e.,

    public function petUpdate(Request $request)
    {

        if (!ChkAuth::chklog('user')) {  // use your RBAC logic
             abort(403);
        }
        $request->validate([
            'species' => 'required'
        ]);
        $petid = $request->input('petid');
        $species = $request->input('species');
        $postdata = [
            'species' => $species
        ];
        DB::table('dc_pets')
                ->where('petid', $petid)
                ->update($postdata);
        return Response::json(['success' => 'all okay']);
        
    }

Where return Response::json(['success' => 'all okay']); is displayed in a hidden DIV. For a quick example, it's not hidden.

                
              success: function (data) {
                    var message = "data updated";
                    alertWithoutNotice(message);
                   $('#msg').append('<div>' + data.success + '</div>');
                    
                },
                error: function (data, ajaxOptions, thrownError) {
                   if (data.status === 422) {
                        $.each(data.responseJSON.errors, function (key, value) {
                            $('#msg').append('<div>' + value + '</div>');
                        });
                    }

                    if (status === 403 || status === 500) {
                        $('#msg').text("Not Auth");
                    }
                    //window.location.href = '<?//= DIR . "indexbl" ?>';
                }

msg is a div with that id. It was just a quick example I did a while back.

Also after success or fail (error), have a means to return to edit page. Or if success redirect in JS.

Many options ways to do this stuff.

tykus's avatar
tykus
Best Answer
Level 104

Of course, there is no reason you could not return a view as the result of a POST request - but, what would the view be, e.g. a success page; or m,aybe the newly created resource (assuming your POST request was creating a new record)? In the case of the latter, returning a redirect to the show route/action would be typical.

If you are doing something like a search, then you might be as well sending the form with the GET method, allowing it to be bookmarked.

lara120618's avatar

@jlrdw It's not for a complex bit of logic likes yours. It's more like to filter down results.

lara120618's avatar

@tykus Good point. I never thought of it that way. This is for filtering data but I always thought that the URL looked like a mess that's why I thought of using a post request instead.

tykus's avatar

Yeah, for filters (like search), I would suggest a GET request, so that the filters are in the query string. A form can make a GET request.

In many cases, I would attempt to reuse the index action if I am attempting to filter the collection of resource.

If you have an advanced search with a lot of parameters, then I could understand your retiscence, but would try to look at the benefits rather than the perceived ugliness...

lara120618's avatar

@tykus Also, do I list all available traits of a data collection like for instance dimensions like height, width or do I just show suggestions of estimated values of the said trait.

tykus's avatar

Ranges would be an option for some traits like the ones you are suggesting

lara120618's avatar

What about a drop down of the dimensions? E.x. 3x4x5, 6x7x8

tykus's avatar

Sure, it amounts to the same thing...

Please or to participate in this conversation.