61

I'm currently working on a basic form. When you hit the submit button, it should first change the value of a field, and then submit the form as usual. It all looks a bit like this:

<form name="myform" id="myform" action="action.php">
    <input type="hidden" name="myinput" value="0" />
    <input type="text" name="message" value="" />
    <input type="submit" name="submit" onclick="DoSubmit()" />
</form>

And this is how far I've come with the JavaScript code. It changes "myinput"'s value to 1, but it does not submit the form.

function DoSubmit(){
  document.myform.myinput.value = '1';
  document.getElementById("myform").submit();
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Paparappa
  • 2,099
  • 3
  • 18
  • 35

9 Answers9

88

You could do something like this instead:

<form name="myform" action="action.php" onsubmit="DoSubmit();">
    <input type="hidden" name="myinput" value="0" />
    <input type="text" name="message" value="" />
    <input type="submit" name="submit" />
</form>

And then modify your DoSubmit function to just return true, indicating that "it's OK, now you can submit the form" to the browser:

function DoSubmit(){
  document.myform.myinput.value = '1';
  return true;
}

I'd also be wary of using onclick events on a submit button; the order of events isn't immediately obvious, and your callback won't get called if the user submits by, for example, hitting return in a textbox.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
valderman
  • 8,365
  • 4
  • 22
  • 29
7
document.getElementById("myform").submit();

This won't work as your form tag doesn't have an id.

Change it like this and it should work:

<form name="myform" id="myform" action="action.php">
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Claude Schlesser
  • 849
  • 6
  • 15
6

Here is simple code. You must set an id for your input. Here call it 'myInput':

var myform = document.getElementById('myform');
myform.onsubmit = function(){
    document.getElementById('myInput').value = '1';
    myform.submit();
};
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Said Marar
  • 95
  • 2
  • 7
5

No. When your input type is submit, you should have an onsubmit event declared in the markup and then do the changes you want. Meaning, have an onsubmit defined in your form tag.

Otherwise change the input type to a button and then define an onclick event for that button.

Ashwin Krishnamurthy
  • 3,750
  • 3
  • 27
  • 49
2

You're trying to access an element based on the name attribute which works for postbacks to the server, but JavaScript responds to the id attribute. Add an id with the same value as name and all should work fine.

<form name="myform" id="myform" action="action.php">
  <input type="hidden" name="myinput" id="myinput" value="0" />
  <input type="text" name="message" id="message" value="" />
  <input type="submit" name="submit" id="submit" onclick="DoSubmit()" />
</form>

function DoSubmit(){
  document.getElementById("myinput").value = '1';
  return true;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chris Snowden
  • 4,982
  • 1
  • 25
  • 34
1

My problem turned out to be that I was assigning as document.getElementById("myinput").Value = '1';

Notice the capital V in Value? Once I changed it to small case, i.e., value, the data started posting. Odd as it was not giving any JavaScript errors either.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dinesh Rajan
  • 2,376
  • 23
  • 19
  • 1
    Why would it give an error? You're creating a new property "Value", which is perfectly fine. – Xan May 22 '14 at 12:26
  • What I am trying to say is i was referring to "value" as "Value". It of course will give a undefined error as this property "Value" was not initialized – Dinesh Rajan May 22 '14 at 17:48
  • Well, it would: 1) Give no error if you're setting it, 2) Silently return `undefined` if you're using it, which is not an error. – Xan May 22 '14 at 17:49
  • You are right..I should have clarified a bit more.. I was assigning a "Value" property and posting a form. The POST received in the the server was looking for "value" and hence never found the value I was setting – Dinesh Rajan May 22 '14 at 17:53
1

I have done this and it works for me. At first you must add a script such as my SetHolderParent() and call in the html code like below.

function SetHolderParent(value) {
   alert(value);
}
<input type="submit" value="Submit" onclick="SetHolderParent(222);" />
navidsoft
  • 71
  • 1
  • 2
  • 6
  • 2
    Please don't provide code-only answers - it is very hard to understand stuff when it isn't explained. – ethry Jul 02 '22 at 21:18
0

You can use the onchange event:

<form name="myform" id="myform" action="action.php">
    <input type="hidden" name="myinput" value="0" onchange="this.form.submit()"/>
    <input type="text" name="message" value="" />
    <input type="submit" name="submit" onclick="DoSubmit()" />
</form>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
iCrazybest
  • 2,935
  • 2
  • 24
  • 24
0

This might help you.

Your HTML

<form id="myform" action="action.php">
<input type="hidden" name="myinput" value="0" />
<input type="text" name="message" value="" />
<input type="submit" name="submit" onclick="save()" />
</form>

Your Script

    <script>
        function save(){
            $('#myinput').val('1');
            $('#form').submit();
        }
    </script>