15

How to replace the submit button with a link without using javascript ? is there any way to add the submit attribute to a link ? because the form will not be sent by a simple link without "submit".

<form:form action="/getPage" >
     <div class="buttons">

         <a class="link" href=""> 
           <img src="icon.png" />
         </a>

     </div>         
</form:form>

I work in an application web so if i use javascript the link will not be mapped by the server ..

d-_-b
  • 21,536
  • 40
  • 150
  • 256
Souad
  • 4,856
  • 15
  • 80
  • 140

4 Answers4

30

You can not submit a form using an <a> tag alone. You will need some type of javascript to assist, if you insist on using the <a> tag:

<a href="#" onclick="$(this).closest('form').submit()">Submit the Form</a>

or

<a href="#" onclick="document.getElementById('form-id').submit()">Submit the Form</a>

or super hack:

<a href="backendPage.php?param1=yes&param2=no">Submit the Form</a>, then on backendPage.php you can use $_GET requests to process information, and redirect the visitor to the intended page. But that's a horrible idea haha

d-_-b
  • 21,536
  • 40
  • 150
  • 256
  • Hay hello man, I used this method and it worked fine, but didnt work when I changed href value to empty string (href=" ") can you tell me what is the significance of the #.... – fazkan Sep 29 '16 at 00:06
  • Hi @fazkan, the`href="#whatever"` tells the browser to change the hash in the URL, which usually means no page reload (excluding angular, etc.). An empty `href` attribute will be the same as pointing to the current page which will reload the page. Alternatively, you could remove the `href` attribute completely and use CSS `cursor:pointer` to make the mouse hover look like a link. – d-_-b Sep 29 '16 at 04:37
22

Try this:

CSS

button {
    background:none!important;
    border:none; 
    padding:0!important;
    /*border is optional*/
    border-bottom:1px solid #444; 
}

HTML

<button>your button that looks like a link</button>
Omar Wagih
  • 8,504
  • 7
  • 59
  • 75
8

This can achieve the same effort and it looks like <a>

Note: btn btn-link comes from bootstrap

<button class="btn btn-link" type="submit"><img src="icon.png" /></button>
Angelina
  • 141
  • 2
  • 5
0

There is an another way out. Using this method you can use any element to submit a form. Following is an example:

<span onclick="$('#submit').trigger('click')" style="cursor:pointer;">
     anything
</span>

Add a hidden submit button:

 <input style="display:none;" type="submit" id="submit"/>

This will behave exactly as actual submit button.