0

I have a form and when the form is loaded through a browser, I want it to submit the data automatically.

The problem is I wrote a PHP script that will submit it all fine on a test form. My problem is the server I'm trying to submit the data to named the button "submit".

Here is the example form:

<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" />
</form>
<script type="text/javascript">
 document.forms[0].action="submit"
</script>
</body>
</html>

The person that created the form on the other server named it "submit". Is there a workaround until they fix it?

Here is a example of the person's form

<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="submit" class="submit" value="Send Data" />
</form>
</body>
</html>
Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
asked Apr 25, 2012 at 8:23
2
  • This is a pretty localized question. Generally on StackOverflow, it's preferred that questions be phrased in such a way that they can apply to the developer community at large and not just to a specific problem that you're having. Commented Apr 25, 2012 at 15:04
  • The HTML is not well formed. The starting body tag is missing. Commented Sep 18, 2021 at 0:50

3 Answers 3

1

You want to submit the form? Then simply use its submit() method:

document.forms[0].submit();

If the server expects submit to be POSTed, i.e. the button being clicked, you can simply trigger the click() event of the button. Since you cannot access it using its name, I'd use jQuery for it:

$('input[name="submit"]').click();
answered Apr 25, 2012 at 8:25
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of:

<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" />
</form>
<script type="text/javascript">
document.forms[0].action="submit"
</script>
</body>
</html>

Try this:

<html>
<head>
</head>
<form name="MyForm" action="http://www.example.com/post.php" method="post">
<input type="text" name="input1" value="test1" />
<input type="text" name="input2" value="test2" />
</form>
<script type="text/javascript">
document.MyForm.submit();
</script>
</body>
</html>
Bob Aman
33.4k10 gold badges74 silver badges95 bronze badges
answered Apr 25, 2012 at 8:27

2 Comments

It will try to submit the data but doesn't recognize anything on the server side like nothing was submitted.
It would be better if it included the missing starting body tag is .
0

If I understand your question correctly, the input element has name="submit" and type="submit". They are different things.

So you can also create the same behavior easily.

Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
answered Apr 25, 2012 at 8:29

1 Comment

Create the same behavior how exactly? Can you elaborate? Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today).

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.