Run controller function on button press in Phoenix?

I have the following template:

<h1>Confirmation instructions</h1>

<button onpress={Routes.phone_confirmation_path(@conn, :create)}>Send Code</button>
<.form id="code-form" let={f} for={:user} action={Routes.phone_confirmation_path(@conn, :update)}>
  <div>
    <%= label f, :code%>
    <%= text_input f, :code, required: true, id: "code" %>
    <%= submit "Submit Code"%>
  </div>
</.form>

<script>
  async function sendCode() {
    await fetch('/')
  }
</script>

And I want to run the referenced function when the user presses the button outside the form. This current configuration isn’t working, any ideas as to how to achieve this?

You can submit forms from an input outside of the form by setting the form attribute.

I am assuming you want this button to invoke sendCode() js function - onpress is not html5 event handler.

You should use onclick :

<button onclick="sendCode()">Send Code</button>