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
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 thejQuery.ajax
inside the function. In this case, the<form>
and themethod="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 theformSubmit
function will not be called (since the button wasn’t clicked), and the user’s browser will send thename
and themessage
as form data to the server, on the current page – and yes, as a POST. (Is the current page this code is onsubmit.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 calle.preventDefault
so that all network activity goes through the.ajax
call.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:
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
Actually
method
andaction
tags are useful when you are not using any ajax request.➡️ Suppose you are not using the
ajax
request then what will happen?method
isPOST
it’ll append the form-data inside the body of the HTTP request.action
attribute.But since you are now controlling the form submission manually through
ajax
. You can skip thoseattributes
. On that case you can specify on yourformSubmit
method what to do when your submission is completed.You can also prevent the
form
submission usingpreventDefault
.For example: