new_newB1e
new_newB1e

Reputation: 155

Symfony controller response to not refresh the page

I have a Symfony app where the admin role have buttons that can activate/deactivate users.

public function activeActionSchedule(Request $request, $id , User $user)
    {
        $em = $this->getDoctrine()->getEntityManager();

        if($user->getIsActive() == 1){
            $user->setIsActive(0);
        } else {
            $user->setIsActive(1);
        }

        $em->persist($user);
        $em->flush();

        return $this->redirectToRoute('view', array('id' => $user->getLoanId()->getId()));
    }

When I click that button it goes to this route, and after execution it refreshes completely the page (redirect). It's here a way how to not return nothing that will refresh/redirect to another page, so it will work just like Ajax call? If it's not possible and it's necessary to use Ajax, I think anyway I need to modify the response, how?

Upvotes: 0

Views: 2951

Answers (2)

vichu
vichu

Reputation: 353

you can just send an ajax request on clicking the button as

    $.ajax({
        type: "POST",
        url: "/user",
        data: {id: id},
        success: function (result) {
             alert(result.data);
             alert(result.msg);
        }
    });

and you can get the parameters send as post in controller by

    $post_data = $request->request->all();

this will return an array of parameters

and in controller you can send response like

        $json_data = array(
            'data' => 100,
            'msg' => "Success"
        );
        $response = new Response(json_encode($json_data));
        $response->headers->set('Content-Type', 'application/json');
        return $response;

Upvotes: 1

t-n-y
t-n-y

Reputation: 1209

You are using PHP and html, so it is not possible. If you really don't want to reload the page, you have to use Ajax, like you said

Upvotes: 2

Related Questions