Getting value from an input (liveview)

I am currently starting to use “liveview” in my app, but I have a problem getting the value of a form entry.
I can’t find the reason why I can’t.
Could you help me identify where the problem is?

Liveview

defmodule MyappWeb.LiveRequest do
    use Phoenix.LiveView
  
    def render(assigns) do
      ~L"""

      <h1 id="myHeader"><%= @deploy_step %></h1>
                
      <div class="input-group input-group-sm">
          <label>Domínio:&nbsp;&nbsp;</label>
          <input type="text" name="params" list="matches" placeholder="Domínio..." class="form-control">
            <span class="input-group-append">
              <button phx-click="checkdomain" type="button" class="btn btn-info btn-flat">Checar Domínio!</button>
            </span>
      </div>
      """
    end
 
    def mount(_session, socket) do
        {:ok, assign(socket, params: nil, deploy_step: "Aguardando para busca!")}
    end

      def handle_event(checkdomain", %{"params" => params}, socket) do
        url = "https://myapi.com/v2/test/list/full/#{params}"
        response = HTTPoison.get!(url)    
        registrobr = response.body |> Poison.decode!()
        status = registrobr["fqdn"]

        {:noreply, assign(socket, params: params, deploy_step: status)}
      end

  end

Always returns me null

Input needs to be in the form tag and you need to use phx-submit event

1 Like

as sreyansjain said, you need to use a form with phx-submit, for example:

    def render(assigns) do
      ~L"""

      <h1 id="myHeader"><%= @deploy_step %></h1>
                
      <div class="input-group input-group-sm">
        <form phx-submit="checkdomain">
          <label>Domínio:&nbsp;&nbsp;</label>
          <input type="text" name="params" list="matches" placeholder="Domínio..." class="form-control">
          <span class="input-group-append">
            <button type="submit" class="btn btn-info btn-flat">Checar Domínio!</button>
          </span>
        </form>
      </div>
      """
    end

note, I also changed the button to type="submit". Cheers :slight_smile:

3 Likes

It really was that.
Thank you very much