Noob
Noob

Reputation: 1127

Servlets & JSP: How to send parameters with a link

My idea is to put a link in every message of a forum to delete the message if the author wants to delete it, and pass the necessary parameters to work in the doPost method of the servlet to delete the message.

<form id="frm-delete" action="Forum" method="POST">
    <input type="hidden" name="idMsg" value="${m.idMissatge}">
    <input type="hidden" name="action" value="delete">
</form>

How can I do to send the info only clicking on a link using it like a submit button?

Any ideas?

Thank you in advance.

Upvotes: 1

Views: 3013

Answers (3)

chedine
chedine

Reputation: 2374

<a href="#" onclick="submitForm();">My link</a>
 //Add this function in your script block
 function submitForm(){
   var form = document.getElementById('frm-delete');
   form.submit();
 }

I guess, this is what you are looking for.

Upvotes: 2

snitch182
snitch182

Reputation: 723

try:

    <form id="frm-delete" action="Forum" method="POST">
    <input type="hidden" name="idMsg" value="${m.idMissatge}">
    <input type="hidden" name="action" value="delete">
</form>

<a href="#" onclick='document.frm-delete.submit();'>delete message</a>

the link triggers the form submit

Upvotes: 2

Mathias Schwarz
Mathias Schwarz

Reputation: 7197

You cannot do that if you want to send a POST request. However you can apply CSS to the submit button to make it look like a link.

You can use something like this:

background-color: transparent;
padding: 0;
border-width: 0;
cursor: pointer;
text-decoration: underline;
float: right;
margin-top: 1px;

By the way: Be aware that the client is able to change the ID you store in the hidden field, so don't rely too much on it (by for example deleting the row with the given ID without checking).

Upvotes: 2

Related Questions