Change HTML Post parameter on Submit

10,698
<form action="/test" method="post" name="myform">

    <input type="hidden" id="bool" name="bool" value="" />

    <input type="button" value="Yes" onclick="test()" />
    <input type="button" value="No" onclick="test1()" />

</form>

<script type="text/javascript">
    function test() {
        document.getElementById('bool').value = "true";
        document.myform.submit();
    }
    function test1() {
        document.getElementById('bool').value = "false";
        document.myform.submit();
    }
</script>
Share:
10,698
Jaanus
Author by

Jaanus

Doing C#, Java 50-50. SOreadytohelp

Updated on June 04, 2022

Comments

  • Jaanus
    Jaanus almost 2 years

    I have 1 form, with 2 Submit buttons.

    When I click one button, I want certain values to be posted. When I click other button I want other values to be posted.

    This is what I tried, did not work:

    <form action="/test" method="post">
    
        <input type="hidden" id="bool" name="bool" value="" />
    
        <input type="submit" value="Yes" onclick="test()" />
        <input type="submit" value="No" onclick="test1()" />
    
    </form>
    
    <script type="text/javascript">
        function test() {
            document.getElementById('bool').value = "true";
        }
        function test1() {
            document.getElementById('bool').value = "false";
        }
    </script>
    
  • talltolerablehuman
    talltolerablehuman almost 12 years
    so when submitting, you always set it to true?
  • freefaller
    freefaller almost 12 years
    You're not taking into account that the OP wants a different value on post-back
  • techfoobar
    techfoobar almost 12 years
    Yeah, that's a problem. This wont work! I saw only the true part! Will need to handle the button clicks and submit the form via form.submit() then..