1

I have a form on a remote server I'm trying to submit data to, example below

<html>
<head>
</head>
<form action="http://www.example.com/post.php" method="post">
<input type="text" name="input1" value="test1" />
<input type="text" name="input2" value="test2" />
<input type="submit" name="send" value="Submit" />
</form>
</body>
</html>

I'm trying to auto submit the data like this

<html>
<head>
</head>
<form action="http://www.example.com/post.php" method="post">
<input type="text" name="input1" value="test1" />
<input type="text" name="input2" value="test2" />
<script type="text/javascript"> 
 document.forms[0].submit(); 
</script> 
</form>
</body>
</html>

It works fine if the first example didn't have a (name="send") name but when it does have a name nothing submits. My question is how would I go about sending the data with a input button that has a name.

Thank you

asked Aug 15, 2012 at 0:10

2 Answers 2

2

Note: The answer turned out to be type="hidden" instead of type="submit", in which the second does not allow DOM submission while also submitting that input's value in the GET/POST data. type="hidden" does, and it works here since the button does not need to be physically clicked.


Pick one:

<form name="formname" id="formid" action="http://www.example.com/post.php" method="post">
<input type="text" name="input1" value="test1" />
<input type="text" name="input2" value="test2" />
<input type="submit" name="send" value="Submit" />
</form>
<script type="text/javascript">
if (confirm('Ok for "formname", Cancel for "getElementById"')) {
 console.log('document.formname.submit()');
 document.formname.submit();
} else {
 console.log('document.getElementById("formid").submit()');
 document.getElementById('formid').submit();
}
</script> 

http://jsfiddle.net/userdude/EAmwj/3

answered Aug 15, 2012 at 0:18
Sign up to request clarification or add additional context in comments.

6 Comments

I tried it and I think there is some confusion not the form name its the button submit has a name and it's looking for name="send" without it the name it don't submit.
It still works with a button: jsfiddle.net/userdude/EAmwj/3 Note, you need Firebug in Firefox (or something with a console, Chrome (native console, or IE9 (native console).
Yes your example will submit the form so will my second example. The problem is the remote server is looking for the name="send" and if it don't see it, it wont submit anything.
The problem is the remote server is looking for the name="send" on the submit button.
Change it from a submit to a hidden type.
|
2

Your JavaScript code will get executed as soon as the page is loaded. That is why it gets submitted immediately.

You need to wrap the JS code in a function and let an event call it from the page.

You can use a regular button and set the function for the onclick event of a button.


<html>
<head>
<body>
<form action="http://www.example.com/post.php" method="post">
<input type="text" name="input1" value="test1" />
<input type="text" name="input2" value="test2" />
<input type="button" name="send" value="test2" onclick="doSubmit()" />
<script type="text/javascript"> 
function doSubmit() {
 document.forms[0].submit(); 
}
</script> 
</form>
</body>
</html>
answered Aug 15, 2012 at 0:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.