开发者

Servlets & JSP: How to send parameters with a link

开发者 https://www.devze.com 2023-03-17 21:50 出处:网络
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

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="hid开发者_如何学Goden" 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.


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).


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


<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.

0

精彩评论

暂无评论...
验证码 换一张
取 消