Accept/Decline Buttons - how to send value to the controller as a param?

Hi guys,

I’ve been trying to create a form with 2 buttons - Accept and Decline for the last couple of hours but without any success so I hope someone can help me out here.

I’ve tried to create it with form that has 2 submit buttons

<.form let={_f} for={@changeset} action={Routes.campaign_path(@conn, :update, @campaign)}>
        <%= submit "Accept" %>
        <%= submit "Decline" %>
    </.form>

and just with buttons

<%= button("Accept", to: Routes.campaign_path(@conn, :update, @campaign), method: :put %>
<%= button("Decline", to: Routes.campaign_path(@conn, :update, @campaign, method: :put %>

The user can click only one of those buttons and that should trigger the update function inside controller. The question I have is, how do I pass the “Accept” or “Decline” value to the controller as the param from those buttons?

Only inputs with names are sent to the backend. Submit buttons don’t have names by default, you’ll need to provide them on your own. You can either use different names to differenciate or use the same name and different values.

1 Like

I tried adding it but it adds name as a key inside the param map. Is there a way to add it as a value under some key like status so I would get %{“status” => “Accept”}

<%= submit "Accept", name: "Accept" %>

name is a map key

%{"Accept" => "", "_csrf_token" => "TztCHiEBOjVSMAEiI3ZYD0A6fQ9YFh4nxxsAuPCR9YDXg36Wrr5V9IGf", "_method" => "put", "id" => "619"}

I would like to get something like this

%{"status" => "Accept", "_csrf_token" => "TztCHiEBOjVSMAEiI3ZYD0A6fQ9YFh4nxxsAuPCR9YDXg36Wrr5V9IGf", "_method" => "put", "id" => "619"}

Just solved this so if anyone has a similar problem, here’s the solution;
As @LostKobrakai suggested, name has to be added which creates a map key and to add a key value form value has to be added too.

<%= submit "Accept", name: "status", value: "Accept" %>