简体   繁体   中英

Onclick event of a certain page, perform POST action of other page

Let me explain my problem with a scenario:
I have webpages "A.jsp" and "B.jsp". "B.jsp" is a page with a form that requires input field, 'username' and on submission displays a certain page "C.jsp" with user-specific information. "A.jsp" has table with an element that would correspond to 'username' on "B.jsp". Onclick of a certain 'username' on "A.jsp", I want to be redirected to "C.jsp" with clicked username specific information.

How do I get it done?

I am using MVC framework. Only A.jsp and B.jsp are tied to controller, C.jsp is only a View. But, I believe that is less of an issue, since this seem to be more of a javascript question.

Using prototype javascript framework, I have tried the following:
redirectToC=function(pageurl){
var username=document.getElementById("user1").value;
url=pageurl; //I am using B.jsp as Pageurl
$j.ajax({ type: 'POST',
url: url, data: "username="+username,
success: function(request){
window.location=request;
}
});
}

Does C.jsp support only POST - or does it support GET requests as well?

  1. If it does, in the onClick of the 'username' link, set the location of the browser (i think document.location ) to the URL of C.jsp with the username as a GET parameter. Since you are generating the A.jsp page dynamically, this URL can be embedded into the page at generation time
  2. If it does not, a bad hack i can think of is to implement the the table element with the username, as a hidden form element which is submitted via javascript when clicked - 'form'.submit()
<form id="showUserForm" action="/controller/showUser" method="POST">
    <input type="hidden" id="username" name="username">
</form>

..........

<a onclick="showUser('${users.userName}')" href="#"></a>

..........

<script type="text/javascript">
    function showUser(userName){
        document.getElementById("username").value = userName;
        document.getElementById("showUserForm").submit();
    }
</script>

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