Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send simple HTTP request with HTML submit button

Tags:

I have added a submit button to the index.html page that resides on my server side

<form method="get" action="/start">
    <h5>Start Ad-Placer!</h5>
    <input type="submit" value="Start">
</form>

I need the button to simply send a http request to http://127.0.0.1:8484/get/start to start some process. Then once the process is done after a few seconds, I need to simply display an alert message with the response message, acknowledging it is done.

How can I do that with minimum efforts (without using JQuery or other libraries).

like image 328
Mary Avatar asked Aug 15 '17 16:08

Mary


People also ask

Can you use HTML to send HTTP requests?

Client/server architecture The server answers the request using the same protocol. An HTML form on a web page is nothing more than a convenient user-friendly way to configure an HTTP request to send data to a server. This enables the user to provide information to be delivered in the HTTP request.

How can create Submit button in HTML?

The <input type="submit"> defines a submit button which submits all form values to a form-handler. The form-handler is typically a server page with a script for processing the input data. The form-handler is specified in the form's action attribute.

Can a button submit without a form?

Submit buttons don't submit if they are not in a form. Not sure why you think form's can't be in a table? (As you already wrote in the title:) The button has no form associated to .... - So asking the question contains the answer: Add the form which is yet missing.

Can you use a button to submit a form?

Using submit buttons. <input type="submit"> buttons are used to submit forms. If you want to create a custom button and then customize the behavior using JavaScript, you need to use <input type="button"> , or better still, a <button> element.


1 Answers

If you try something like this you can send a HTTP request and then alert a response. Just change https://google.com to your URL.

<h5>Start Ad-Placer!</h5>
<input type="submit" value="Start" onclick="submit()">

<script type="text/javascript">
    function submit() {
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                alert(xhr.response);
            }
        }
        xhr.open('get', 'https://google.com', true);
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
        xhr.send();
    }

</script>
like image 151
thepi Avatar answered Oct 11 '22 14:10

thepi