skip to Main Content

I’m totally a newbie on frontend development and just learning about jQuery.

I’m confused about "submit a HTML form with jQuery ajax". Here is a simple example:

<form class="form" action="" method="post">
    <input type="text" name="name" id="name" >
    <textarea name="text" id="message" placeholder="Write something to us"> </textarea>
    <input type="button" onclick="return formSubmit();" value="Send">
</form>

<script>
    function formSubmit(){
        var name = document.getElementById("name").value;
        var message = document.getElementById("message").value;
        var dataString = 'name='+ name + '&message=' + message;
        jQuery.ajax({
            url: "submit.php",
            data: dataString,
            type: "PUT",
            success: function(data){
                $("#myForm").html(data);
            },
            error: function (){}
        });
    return true;
    }
</script>

As you see, when we click the button Send, the function formSubmit() will be invoked and the jQuery.ajax will do its job. It will send a http request with the type PUT.

But there is an attribute in the HTML form method="post", now I’m confused. When we click the button, what will actually happen? The jQuery.ajax will send a request in the js function but what will the method="post" do? Or method="post" can be ignored? If it does nothing, can we make the attribute method empty: <form class="form" action="" method="">?

Additional

Now I realise that the type of the buttion in this example is button, instead of submit. If I change the type into submit and click it, what will happen? Will it send two http requests, a post request comes from the form and a put request comes from the jQuery.ajax?

4

Answers


  1. This example is badly designed and confusing.

    The formSubmit function will only be called when the button is clicked. When the button is clicked, that function will run, and nothing else will happen; since the input is not a submit button, the form will not attempt to submit. All network activity will result from the jQuery.ajax inside the function. In this case, the <form> and the method="post" are ignored completely, since the form doesn’t get submitted – the method used will be the one in the .ajax function, which is PUT.

    But the form is still submittable, if the user presses enter while focused inside the <input type="text". If they do that, then the formSubmit function will not be called (since the button wasn’t clicked), and the user’s browser will send the name and the message as form data to the server, on the current page – and yes, as a POST. (Is the current page this code is on submit.php? If not, that may be a mistake. Perhaps the person who wrote the code completely overlooked the possibility of the form being submitable without using the button.)

    To make the example less confusing, I’d change the button to a submit button, and add a submit handler to the form instead of a click listener to the button. Then have the submit handler return false or call e.preventDefault so that all network activity goes through the .ajax call.

    Login or Signup to reply.
  2. The method attribute in HTML Form is used to send the form data when you’re sending it without the help of an Ajax Request or any other scripting language.

    When you use a scripting language like JS and Jquery, you have the chance to send the data via an AJAX request. Inside the AJAX request, you can define the method again. So, it won’t rely on the HTML Form’s method attribute.

    Few resources you can follow:

    Login or Signup to reply.
  3. The form tag gives support by giving several inbuilt default actions, but since you have defined a submit button and made a function to be executed onclick manually, so the form tag will only confuse the structure. So it’s better to remove the form tag completely and you code will look like

        <input type="text" name="name" id="name" >
        <textarea name="text" id="message" placeholder="Write something to us"> </textarea>
        <input type="button" onclick="return formSubmit();" value="Send">
    
    
    <script>
        function formSubmit(){
            var name = document.getElementById("name").value;
            var message = document.getElementById("message").value;
            var dataString = 'name='+ name + '&message=' + message;
            jQuery.ajax({
                url: "submit.php",
                data: dataString,
                type: "PUT",
                success: function(data){
                    $("#myForm").html(data);
                },
                error: function (){}
            });
        return true;
        }
    </script>
    
    Login or Signup to reply.
  4. Actually method and action tags are useful when you are not using any ajax request.

    ➡️ Suppose you are not using the ajax request then what will happen?

    1. Since your method is POST it’ll append the form-data inside the body of the HTTP request.
    2. Then the form-data is sent to the page specified in the action attribute.

    But since you are now controlling the form submission manually through ajax. You can skip those attributes. On that case you can specify on your formSubmit method what to do when your submission is completed.

    You can also prevent the form submission using preventDefault.
    For example:

    <form onSubmit="formSubmit(event);">
      <button>submit</button>
    </form>
    
    function formSubmit(e) {
      e.preventDefault();
      // Now you can set where to go when your form is submitted successfully.
    }
    
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search