XMLHttpRequest to Post HTML Form

后端 未结 6 1999
遇见更好的自我
遇见更好的自我 2020-12-01 04:50

Current Setup

I have an HTML form like so.

相关标签:
6条回答
  • 2020-12-01 05:12

    The ComFreek's answer is correct but a complete example is missing.

    Therefore I have wrote an extremely simplified working snippet:

    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge, chrome=1"/>
    
    <script>
    "use strict";
    function submitForm(oFormElement)
    {
      var xhr = new XMLHttpRequest();
      xhr.onload = function(){ alert(xhr.responseText); }
      xhr.open(oFormElement.method, oFormElement.getAttribute("action"));
      xhr.send(new FormData(oFormElement));
      return false;
    }
    </script>
    </head>
    
    <body>
    <form method="POST"
          action="post-handler.php"
          onsubmit="return submitForm(this);" >
       <input type="text"   value="previousValue" name="name"/>
       <input type="submit" value="Update"/>
    </form>
    </body>
    </html>
    

    This snippet is basic and cannot use GET. I have been inspired from the excellent Mozilla Documentation. Have a deeper read of this MDN documentation to do more. See also this answer using formAction.

    0 讨论(0)
  • 2020-12-01 05:12

    With pure Javascript, you just want something like:

    var val = document.getElementById("inputFieldID").value;
    

    You want to compose a data object that has key-value pairs, kind of like

    name=John&lastName=Smith&age=3
    

    Then send it with request.send("name=John&lastName=Smith&age=3");

    0 讨论(0)
  • 2020-12-01 05:14

    I have had this problem too, I think.

    I have a input element with a button. The onclick method of the button uses XMLHTTPRequest to POST a request to the server, all coded in the JavaScript.

    When I wrapped the input and the button in a form the form's action property was used. The button was not type=submit which form my reading of HTML standard (https://html.spec.whatwg.org/#attributes-for-form-submission) it should be.

    But I solved it by overriding the form.onsubmit method like so:

    form.onsubmit = function(E){return false;}
    

    I was using FireFox developer edition and chromium 38.0.2125.111 Ubuntu 14.04 (290379) (64-bit).

    0 讨论(0)
  • 2020-12-01 05:21
    function postt(){
        var http = new XMLHttpRequest();
        var y = document.getElementById("user").value;
        var z = document.getElementById("pass").value;
        var postdata= "username=y&password=z"; //Probably need the escape method for values here, like you did
    
        http.open("POST", "chat.php", true);
    
        //Send the proper header information along with the request
        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        http.setRequestHeader("Content-length", postdata.length);
    
        http.onreadystatechange = function() {//Call a function when the state changes.
            if(http.readyState == 4 && http.status == 200) {
                alert(http.responseText);
            }
        }
        http.send(postdata);
    }
    

    how can I post the values of y and z here from the form

    0 讨论(0)
  • 2020-12-01 05:29

    The POST string format is the following:

    name=value&name2=value2&name3=value3
    


    So you have to grab all names, their values and put them into that format. You can either iterate all input elements or get specific ones by calling document.getElementById().

    Warning: You have to use encodeURIComponent() for all names and especially for the values so that possible & contained in the strings do not break the format.

    Example:

    var input = document.getElementById("my-input-id");
    var inputData = encodeURIComponent(input.value);
    
    request.send("action=dosomething&" + input.name + "=" + inputData);
    

    Another far simpler option would be to use FormData objects. Such an object can hold name and value pairs.

    Luckily, we can construct a FormData object from an existing form and we can send it it directly to XMLHttpRequest's method send():

    var formData = new FormData( document.getElementById("my-form-id") );
    xhr.send(formData);
    
    0 讨论(0)
  • 2020-12-01 05:30

    By the way I have used the following code to submit form in ajax request.

     $('form[id=demo-form]').submit(function (event) {
    
        if (request) {
            request.abort();
        }
        // setup some local variables
        var $form = $(this);
    
        // let's select and cache all the fields
        var $inputs = $form.find("input, select, button, textarea");
    
    
        // serialize the data in the form
        var serializedData = $form.serialize();
    
    
        // fire off the request to specific url
    
        var request = $.ajax({
            url : "URL TO POST FORM",
            type: "post",
            data: serializedData
        });
        // callback handler that will be called on success
        request.done(function (response, textStatus, jqXHR){
    
    
        });
    
        // callback handler that will be called on failure
        request.fail(function (jqXHR, textStatus, errorThrown){
    
        });
    
        // callback handler that will be called regardless
        // if the request failed or succeeded
        request.always(function () {
            // reenable the inputs
    
        });
    
        // prevent default posting of form
        event.preventDefault();
    });
    
    0 讨论(0)
提交回复
热议问题