-1

I have a form with a javascript submit function. I have 3 links (anchor tags) in the form & would like to set different values to the hidden parameter on submit based on the link clicked

Form submit works fine generally, but if I try to open the link in a new tab, form is not submitted. The alert within the javascript function is not printed. Does anyone have any idea to fix this?

Below is the Sample Code :

<html>

    <head>
        <script type="text/javascript">
            function submitForm(actionParam) {
            alert("in submitForm");
            document.getElementById("action").value = actionParam;
            document.forms['myForm'].action = action;
            document.forms['myForm'].submit();
        }
        </script>
    </head>

    <body>

        <form name="myForm" id="myForm" method="post" action="/businesspanel">
            <input type="hidden" id="action" name="action" value="" />

            <a href="#" onclick="submitForm('action1');">Action 1</a></span>
            <a href="#" onclick="submitForm('action2');">Action 2</a></span>
            <a href="#" onclick="submitForm('action3');">Action 3</a></span>

        </form>

    </body>
</html>
Imran AK
  • 103
  • 10

2 Answers2

1

Opening a link in a new window bypasses the JavaScript. Don't use links / JS to submit forms.

Just use submit buttons. You can even send your identifying values without involving JS.

<button name="action" value="action1">Action 1</button>
<button name="action" value="action2">Action 2</button>
<button name="action" value="action3">Action 3</button>
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0
document.forms['myForm'].action = action;

"action" is not defined here.If u want to open the link in new tab,just do:

document.forms['myForm'].target= '_blank';
cityvoice
  • 2,409
  • 4
  • 14
  • 25