Reputation: 5865
I wonder if there is a clean way to do an html form that will get posted if the user pushes a post button and that could be sent as well with the get method if the user pushes a get button. I need this because I would like to show a form which can both enter new data or search for existing data in the database. If the user wants to enter data in the database they could fill the form and send via a 'send' button (POST) and if they are not sure what they want they can partly fill the form and push the search button (GET) to receive some insights on what is in the database. From what I know the method is defined for the form, not for the submit buttons. Any idea how I could do that without javascript? or with javascript?
Upvotes: 0
Views: 253
Reputation:
Although you shouldn't need this functionality, I will answer your question anyway.
Here. First you should assign an ID to the form. Our sample form will have the ID 'YYYY'.
<form action="whatever" id="YYYY" method="" >
<input type="button" onClick="usingget();" value="GET" />
<input type="button" onClick="usingpost();" value="POST" />
</form>
<script type="text/javascript">
function usingget() {
document.getElementById('YYYY').method = 'GET';
setTimeout(function(){
document.getElementById('YYYY').submit();
}},1000);
function usingpost() {
document.getElementById('YYYY').method = 'POST';
setTimeout(function(){
document.getElementById('YYYY').submit();
}},1000);
</script>
Now, when you click one of the buttons it will change the method. EDIT: There is now a 1 second delay because I am tired and unsure of whether or not the form will submit before the method is changed without it.
Upvotes: 2