Fidd
Fidd

Reputation: 722

HTML form - POST or GET depending on button clicked

Let there be a search form with quite many of the input fields. The user may either click:

How do I achieve that? I've only found solutions to make two types of POST request on two separate buttons.

Maybe a JavaScript onclick function to set <form method="??"> right before its submitted? Would that work?

Upvotes: 0

Views: 1691

Answers (2)

tkausl
tkausl

Reputation: 14279

In HTML5, the <input> element got some new attributes

<input type="submit" formmethod="post" formaction="save.php" value="Save" />
<input type="submit" formmethod="get" formaction="search.php" value="Search" />

The same attributes are valid for <button>

Upvotes: 6

bipin patel
bipin patel

Reputation: 2111

Yes you can change form method before submit form using jquery

<form method="POST" id="myForm">
    <button onclick="changeMethod()">submit</button>
</form>

Function changeMethod in jquery

function changeMethod(){
    $("#myForm").attr("method", "get");
}

Upvotes: 0

Related Questions