The form data can be sent as URL variables (method="get") or as an HTTP post transaction (method="post").
The formMethod
property sets or gets the formmethod attribute of a submit button.
The formmethod
attribute,
which overrides the method attribute of the <form> element,
defines the HTTP method for sending form-data to the action URL.
form |
Yes | 10.0 | Yes | Yes | Yes |
Return the formMethod property.
var v = submitObject.formMethod
Set the formMethod property.
submitObject.formMethod=get|post
Value | Description |
---|---|
get | Default. Appends the form-data to the URL in name/value pairs |
post | Sends the form-data as an HTTP post transaction |
A String type value representing the HTTP method that is used to submit the form to the server.
The following code shows how to get the method for sending form-data.
<!DOCTYPE html>
<html>
<body>
<!--from ww w . ja v a 2s.co m-->
<form target="_blank">
First name: <input type="text" name="fname" value="abc"><br>
Last name: <input type="text" name="lname" value="def"><br>
<input type="submit" id="mySubmit" formaction="demo_form_method.asp" formmethod="get" value="Submit">
</form>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
document.getElementById("mySubmit").formMethod = "post";
document.getElementById("mySubmit").formAction = "demo_form_method_post.asp";
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code shows how to get which HTTP method that is used to submit the form to the server.
<!DOCTYPE html>
<html>
<body>
<!--from w ww. ja va2 s. c o m-->
<form action="demo_form.asp" method="get">
First name: <input type="text" name="fname" value="abc"><br>
Last name: <input type="text" name="lname" value="def"><br>
<input type="submit" id="mySubmit" formaction="demo_post.asp" formmethod="post" value="Submit">
</form>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("mySubmit").formMethod;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
The code above is rendered as follows: