1

Can I turn a link for example <a href="#">submit</a> into a submit button for a form? If so how? I'm using PHP, MySQL, XHTML and CSS.

asked May 1, 2010 at 18:46
1
  • <a href="javascript:document.forms["myform"].submit();"> Commented May 1, 2010 at 18:55

6 Answers 6

4

You can call submit() on your form. Like:

<a href="#" onclick="document.getElementById('myForm').submit();">e</a>

Which is something I really would not recommend, but its the answer to your question.

answered May 1, 2010 at 18:53
Sign up to request clarification or add additional context in comments.

2 Comments

@Francisco Soto: Yes, that should work, but note that it is against the practices of obtrusive javascript :)
Yes, that's what I said I would not recommend it :), just answered his question.
2

Not a real one.

You can fake it with JavaScript, but this violates Rule 2.

You can use CSS to make a submit button look like a link, but this isn't generally a good idea (although still a better one than using JS). Users expect buttons to submit forms, and for links to just go somewhere. Violating expectations is poor usability.

answered May 1, 2010 at 18:48

Comments

1

With javascript you can do a onclick

In jquery:

$('theElement').click(function(){ 
// submit the form...
});

Edited: (I wrote onclick!)

answered May 1, 2010 at 18:49

Comments

0

You can use jQuery for unobtrusive javascript practices and submit the form:

<form id="form_id">
 ...........
</form>
<a href="#" id="submit_link">submit</a>

jQuery:

$(function(){
 $('#submit_link').click(function(){
 $('#form_id').submit(); 
 return false;
 });
});
answered May 1, 2010 at 19:02

Comments

-2

(TESTED ON SAFARI and FF / OSX) I do not have DOS machine at home.

The answer to the question is Yes.

This is how you would code it in plain Javascript:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 <title>Form Submit</title>
 <script>
 window.onload = function(){
 document.getElementById("linksubmit").onclick = function(){
 if(document.getElementById("foo").value != ''){
 document.forms[0].submit();
 return false;
 }else{
 alert('Please fill in Foo');
 }
 }
 }
 </script>
 </head>
 <body>
 <form id="theform" action="index.html" method="post">
 <label for="foo">Foo</label>
 <input type="text" name="foo" id="foo" />
 <a href="javascript:void(0);" id="linksubmit">Submit</a>
 </form> 
 </body>
 </html> 

And this how you would code it with jQuery:

$(document).ready(function(){
 $("#linksubmit").click(function(){
 if($("#foo").val()!=''){
 $("#theform").submit();
 return false;
 }else{
 alert('Please fill in Foo');
 }
 });
});

Just use this piece between the script tags instead of the first example. Also remember to add the jQuery source code. Google has a hosted version that is good to use: http://code.google.com/apis/ajaxlibs/documentation/index.html#jquery

answered May 1, 2010 at 19:20

Comments

-2
<script language="javascript">
 function submitform()
 {
 document.form1.submit();
 }
</script>
<form name="form1"></form>
<a href="javascript:submitform()">Submit</a>
eckes
67.8k31 gold badges177 silver badges209 bronze badges
answered May 1, 2010 at 19:02

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.