0

I'm studying JavaScript (without jQuery) and I try to get the response of a form submit.

Here is a (little modified) sample form example given on w3schools;

<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <h1>JavaScript FORMS Example #3</h1>
        <br>
        <h3>validating form</h3>
        <form
            name="myForm" action="http://www.w3schools.com/js/demo_form.asp"
            onsubmit="return validateForm()" method="post">
            Name: <input type="text" name="fname" required>
            <input type="submit" value="Submit Form">
        </form>
        <br>
        <script>
        function validateForm() {
            var x = document.forms["myForm"]["fname"].value;

            if(x == null || x =="") {
                alert("Name must be filled out");
                return false;
            }
        }
        </script>
    </body>
</html>

What I am trying to do is to get the response of the submited form. The response can be a simple text, XML or JSON formatted. (Sure if the response is plain text, I can parse it with JSON.parse() method)

I have searched/sweeped SO but all the answers regarding to the "getting response of the form submit" or similiar questions using jQuery, however, I am searching for an exact pure JavaScript solution.

Edit: The focus of the question is not making an AJAX call without jQuery. I'm simply asking for a possible simple solution if there is any.

Levent Divilioglu
  • 9,139
  • 5
  • 49
  • 96
  • 1
    [`XMLHttpRequest`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) or [`window.fetch()`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) ( – Andreas Jan 13 '16 at 09:03
  • 1
    Possible duplicate of [How to make an AJAX call without jQuery?](http://stackoverflow.com/questions/8567114/how-to-make-an-ajax-call-without-jquery) – Gerald Schneider Jan 13 '16 at 09:06
  • @Gerald Schneider : I don't think so, I'm not asking making an AJAX call without jQuery, the focus tag of the question is not AJAX but HTML forms. – Levent Divilioglu Jan 13 '16 at 09:10
  • 2
    what you want to do is the very definition of AJAX. – Gerald Schneider Jan 13 '16 at 09:14
  • @Gerald Schneider : If so, are you saying that it is impossible to get the form submit response without AJAX. Can you also give some links? – Levent Divilioglu Jan 13 '16 at 09:16
  • 3
    The process of submitting a form without reloading the page is generally called AJAX. So I'd say yes, it is impossible to do so without it. The linked question shows how to do that with pure JavaScript. – Gerald Schneider Jan 13 '16 at 09:24
  • 1
    The definition of Ajax is **Asynchronous JavaScript and XML**. Ajax pretty much covers any call to a server from javascript, which submitting a form and getting a response is. You can get a form to submit without Javascript and just use whatever server side language you want to process it. – Craicerjack Jan 13 '16 at 09:33

0 Answers0