简体   繁体   中英

Sending text-field value to server without form tag

I need to post a text-field value to server but i have not placed the text-field in side the form tag.Here is the details of my use-case

i have an anchor tag like

<a href="${request.contextPath}/login" class="login">LOGIN</a>

This anchor tag is not inside any form tag and i need to send one extra value to the server and don't want that value to append as query string. I have created a hidden filed and have provided the required value to that hidden field, but when i click on the Login link and its getting to my Controller class this hidden field value is not available.

Is there any way to send that value to server side class as a request parameter?

You can use ajax to do that, i suggest to use Jquery

$.post('loginhandle', {username:$('#username').val(), password: $('#password').val()} function(){});

By using Javascript get value from hidden fields like this 
<script>
var name= document.getElementById("login").value
document.getElementById("topage").innerHTML='<a href="${request.contextPath}/login.jsp"'+'&loginname='+name+' class="login">LOGIN</a>'
</script>
<input type="hidden" name="name" value="ashraf" id='login'>
<div id='topage'>

<a href="" class="login">LOGIN</a>

</div>

You're doing a get rather than a post. You could append to he querystring as this works with get.

Get the hidden field value using javascript before the form is submitted. Use document.getElementById("hiddenID").value; Append the value obtained in the URL before the form is submitted. The value should be there in the server.

Regards, Ajai G

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM