16

This is what I am trying to do without success :

<form name="something" action="ht.php" method="post">
 <a href="#" onclick="document.url.submit('helloworld');">Submit</a>
</form>

When I click on the link I want to post the value helloworld to ht.php. How can I do this?

AllJs
1,8204 gold badges30 silver badges51 bronze badges
asked Mar 4, 2011 at 4:13
1
  • odd, either use a form as its intended or $_GET Commented Mar 4, 2011 at 4:16

5 Answers 5

35

You can't just do document.url.submit(), it doesn't work like that.

Try this:

<form id="myForm" action="ht.php" method="post">
 <input type="hidden" name="someName" value="helloworld" />
 <a href="#" onclick="document.getElementById('myForm').submit();">Submit</a>
</form>

That should work!

answered Mar 4, 2011 at 4:20
Sign up to request clarification or add additional context in comments.

4 Comments

Your welcome! Just remember that this can be a very insecure way if you are having user interaction. The hidden value can easily be modified.
@Flipper "very insecure way" what do you mean.?
@donvitto, If you are going to use this to have a user submit some data then the user could simply copy your HTML, make their own HTML file with a different value for the hidden input. Then submit the form and submit a different value. (Or with Chrome they can just use the Developer Tools.) Make sure that you are checking the data that is submitted to be exactly what you want and nothing else. ESPECIALLY if you are doing SQL queries using the data.
@Flipper not im just submitting the value thats all no data file and the script its not hooked up to anything so it should be good. :) thanks for the reply.
7

Using jQuery, its rather easy:

$('form .submit-link').on({
 click: function (event) {
 event.preventDefault();
 $(this).closest('form').submit();
 }
});

Then you just code as normal, assigning the class submit-link to the form submission links:

<form action="script.php" method="post">
 <input type="text" name="textField" />
 <input type="hidden" name="hiddenField" value="foo" />
 <a href="#" class="submit-link">Submit</a>
</form>

I find this method useful, if you want to maintain an aesthetic theme across the site using links rather than traditional buttons, since there's no inline scripting.

Here's a JSFiddle, although it doesn't submit anywhere.

answered Mar 4, 2011 at 4:26

Comments

2

try

<form id="frmMain" action="ht.php" method="post">
 <a href="#" onclick="document.forms['frmMain'].submit();">Submit</a>
</form>
answered Mar 4, 2011 at 4:18

Comments

2

Try this,

<!-- you need to give some name to hidden value [index for post value] -->
<form name="something" action="ht.php" method="post">
 <input type="hidden" name="somename" value="helloworld" />
 <a href="javascript: document.something.submit();">Submit</a>
</form>

Also try this

<!-- you need to give some name to hidden value [index for post value] -->
<!-- also you can use id to select the form -->
<form name="something" action="ht.php" method="post" id="myform">
 <input type="hidden" name="somename" value="helloworld" />
 <a href="javascript: document.getElementById('myform').submit();">Submit</a>
</form>
answered Mar 4, 2011 at 4:18

3 Comments

Form name is not recommended to be used because it does not follow XHTML 1.0 Strict. I haven't checked, but I wouldn't be surprised if HTML5 also doesn't support the form "name". Therefore, it is best to just not use it.
@Flipper maybe i could use id
ID is the way to go, but you just shouldn't declare name at all. It won't pass XHTML validation with name.
1

You could add a hidden field on the page (set it's name property), set it's value to helloworld.

Then in your hyperlink's onclick call form.submit()

answered Mar 4, 2011 at 4:16

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.