Html and Controller communicating

Could help me create an html that queries my controller (domain_check) and shows an alert?
Below is what has already been done.
At the moment all the ways I’m doing, it’s crashing error.

Controller:

  def checkout_domain(%{"request" => request_params}) do
    System.cmd("whois", [Map.get(request_params, "domain")])
  end

Template html:


            <div class="input-group input-group-sm">
                <!-- <input type="text" class="form-control"> -->
                <label>Domain:&nbsp;&nbsp;</label>
                <%= text_input f, :domain, class: "form-control" %>
                <%= error_tag f, :domain %>
                  <span class="input-group-append">
                    <button type="button" class="btn btn-info btn-flat">Check Domain!</button>
                  </span>
            </div>

It will not work like this…

If You want to update the page after it has rendered, You will need liveview, or javascript.

It seems the controller is missing connection params and returns to conn. If you’re are using Phoenix, it has controller generation task, that can be utilized via mix.

Some good tutorial examples:

1 Like

Yeah,
I even thought about it, but I’m terrible at javascript …
I see that I will not have another exit :frowning:

I thought of something like this with Javascript
Correct me if I’m wrong.

Template

            <h1 id="myHeader"></h1>
            <div class="input-group input-group-sm">
                <!-- <input type="text" class="form-control"> -->
                <label>Domain:&nbsp;&nbsp;</label>
                <%= text_input f, :domain, class: "form-control" %>
                <%= error_tag f, :domain %>
                  <span class="input-group-append">
                    <button onclick="displayResult()" type="button" class="btn btn-info btn-flat">Check Domain!</button>
                  </span>
            </div>

<script>
function displayResult() {
  document.getElementById("myHeader").innerHTML = "<%= inspect MyprojectWeb.RequestController.checkout_domain(domain) %>";
}
</script>

Controller

  def checkout_domain(params) do
    System.cmd("whois", [params])
  end

Something like this…

You need to render in JSON the checkout_domain action, and use fetch… like this.

  • fetch send an ajax request to your JSON api and returns a promise
  • if it’s ok, update DOM with the result
  • if it’s not, display the error

BTW If You can do this, You will have a better understanding of how JSON Api works.