'Javascript anchor href change and form post
I have a form coded like below. Basically the href value for the anchor tag is determined by the value selected in a dropdown.
<form name=xyz method=post>
//random stuff
<a href="#" onclick="setAnchor()">
//some image
</a>
</form>
I am setting the href property of the form in javascript like this:
if(dropdown.option selected = "x")
windows.location.href = some URL;
else
windows.location.href = another URL;
The problem I am having is that using windows.location
causes the form to use the GET method. The code expects a POST method to continue.
How can I fix this? I am not married to the above method. Any way I can set the form action URL, based on the option selected by the user, would work for me.
Solution 1:[1]
If you have this form:
<form id="myForm" action="page.php" method="POST">
You can submit it (with a POST
method) (that is, instead of window.location...
) like so:
document.getElementById("myForm").submit();
You can also set the URL to be submited like this
document.getElementById("myForm").action = "page.php";
Solution 2:[2]
You should submit your form:
document.xyz.submit();
Solution 3:[3]
document.xyz.action=page1.php; (or page2.php based on dropdown selection)
document.xyz.submit();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | JCOC611 |
Solution 2 | KomarSerjio |
Solution 3 | Amol Katdare |