Using onclick event to submit 2 parameters into a the same servlet - javascript

I'm trying to use a submit button to pass 2 parameters, userId and friendId, using jsp into the same servlet. I am able to obtain friendId, however, I am not able to obtain the userId.
function anotherPageServlet(servletName) {
document.forms[0].method = "post";
document.forms[0].action = servletName;
document.forms[0].submit();
}
function anotherPageServlet1(servletName) {
document.forms[0].method = "post";
document.forms[0].action = servletName;
document.forms[0].submit();
}
<input style="color:#3333ff"
type="submit"
class="rsubmit"
value="<%= retrieving.getDisplayname()%> :"
onclick="anotherPageServlet('ViewingFriendServlet?stringParameter=<%= session.getAttribute("userId") %>'),
anotherPageServlet1('ViewingFriendServlet?stringParameter=<%=retrieving.getoID() %>')"
>

You probably only need to escape the quotes:
onclick="anotherPageServlet('ViewingFriendServlet?stringParameter=<%= session.getAttribute(\"userId\") %>'),
anotherPageServlet1('ViewingFriendServlet?stringParameter=<%=retrieving.getoID() %>')"
I'm not used to JSP, but in JSF you would rather assign values to a request-scoped bean and then call a function that works with that bean, rather than passing those arguments directly.
EDIT:
Define onclick functions first, then call only the simple reference.
function servletFoo() {
anotherPageServlet('ViewingFriendServlet?stringParameter=<%= session.getAttribute("userId") %>');
anotherPageServlet1('ViewingFriendServlet?stringParameter=<%=retrieving.getoID() %>');
}
Then in your HTML:
<input ... onclick="servletFoo()" />

Related

How to pass a String variable to javascript onClick Function in a JSP

In my jsp, I have something like this
<%
String isMultipleOfficesExists = (String)request.getAttribute("MultipleOfficesExists");
String isMultipleOfficeSecurity = (String)request.getAttribute("MultipleOfficeSecurityExists");
String envParm = "default";
if("true".equals(isMultipleOfficesExists)){
envParm = "multipleOffice";
}else if("true".equals(isMultipleOfficeSecurity)){
envParm = "multipleOfficeSecurity";
}
%>
At the bottom of my form, in my submit button, I am calling a JavaScript On-click function.
<input class="white_button_extra_large" type="button" value="<%=goBtn%>" onclick="javascript:selectEnvironment(envParm);">
And my script section is :
function selectEnvironment(envParm)
{
resetToken();
logoutFlag = false;
document.forms[0].action = contextURL+'/login/selectEnvironment?envParam=' +envParm;
document.forms[0].submit();
}
But I am getting Uncaught ReferenceError: envParam is not defined
How can I solve this?
Make your envParm variable global in JSP. And your on click event should be like follows
<input class="white_button_extra_large" type="button" value="<%=goBtn%>" onclick="javascript:selectEnvironment('<%=envParm%>')">
You should pass the value from javascript function. The parameter should have value. Refer this linkPassing arguments

MVC Ajax.BeginForm JavaScript parameters

I have an AJAX form where the url id needs to be from JavaScript
#using(Ajax.BeginForm("Add","Comments", new { ArticleID = 3 }, new AjaxOptions { UpdateTargetId="Comments"}))
Where ArticleID = 3 should be replaced so that the ArticleID value is set equal to the result of a called Javascript function. Something like
JS:
function GetArticleID()
{
return 3;
}
Razor:
#using(Ajax.BeginForm("Add","Comments", new { ArticleID = GetArticleID() }, new AjaxOptions { UpdateTargetId="Comments"}))
Controller:
public ActionResult Add(int ArticleID, Comment model)
{
}
How can I use JavaScript function result as BeginForm parameter?
The line #using(Ajax.BeginForm(" will be executed by razor on server. At that time it does not have any knowledge of the javascript methods in your client browser. So you cannot mix a javascript function there.
I prefer to write clean custom code to do the ajax form submit (instead of using Ajax.BeginForm) because it allows me to customize any way i want.
Keep your form as a normal form.
#using(Html.BeginForm("Add","Comments"))
{
<input type="hidden" name="ArticleId" id="ArticleId" value=""/>
<input name="CommentText" type="text" />
<input type="submit" id="saveCmntBtn" />
}
Now listen to the click event of the submit button. Assign the ArticleId value to the input field, get the serialized version of the form and post to server. You may use jQuery serialize() method to get the serialized version of your form.
$(function(){
$("#saveCmntBtn").click(function(e){
e.preventDefault();
$("#ArticleId").val(GetArticleID());
var f=$(this).closest("form");
$.post(f.attr("action"),f.serialize(),function(res){
$("#Comments").append(res);
});
});
});

Calling a function through buttons in ASP.NET using VB

I'm having some issues with calling the function I have defined when I click a button. For example, my function is something like this:
<script runat = "server" language = "vb">
Sub deleteFunction()
Response.Write("hello test test")
End Sub
</script>
This is all within the <head> tags. In the <body> tags, I have this:
<form method = "post" action = "HubPage.aspx">
<input type = "submit" value = "Delete" onclick = "<% deleteFunction() %>">
</form>
That doesn't work because it just calls the function without me ever having to click the button. So I tried this:
<form method = "post" action = "HubPage.aspx" onsubmit = "<% deleteFunction() %>">
<input type = "submit" value = "Delete">
</form>
However, that also calls the function regardless.
Can anyone point me in the right direction?
You can remove onclick event and but this in your .Net code to call javascript code
Page.ClientScript.RegisterStartupScript(Me.GetType, Nothing, "FunctionName();", True)

Retrieving variable in onclick with javascript

I'm trying to submit a JSP page after retrieving a variable from the javascript function with onclick. Is there a way to do that?
Javascript:
function sub(str)
{
alert(str); //To test if I am getting the variable on this function
}
JSP contents:
<c:forEach items="${mybean.flag}" var="element">
${element.CASE}
</c:forEach>
<%String w = request.getParameter("str");
if(!w.equals("null") {
Call a java bean to do further manipulations
}
%>
To submit a form per Javascript, you could do anytime the following:
document.forms[0].submit(); // assuming you have at least one Form on your page and want to submit the first one
I was able to do pass the variable from onclick using the below code.
Javascript:
function subb(str){
alert("Subb" +str);
form = document.createElement('form');
form.setAttribute('method', 'POST');
form.setAttribute('action', 'index.jsp');
myvar = document.createElement('input');
myvar.setAttribute('name', 'mydata');
myvar.setAttribute('type', 'hidden');
myvar.setAttribute('value',str);
form.appendChild(myvar);
document.body.appendChild(form);
form.submit();
}
${element.CASE}
</c:forEach>
<%String w = request.getParameter("mydata");
if(!w.equals("null") {
Call a java bean to do further manipulations
}
%>

How can i access javascript variables in JSP?

I want to access Javascript variables in JSP code. How can I do that?
JavaScript variable is on client side, JSP variables is on server side, so you can't access javascript variables in JSP. But you can store needed data in hidden fields, set its value in client and get it on server over GET or POST.
Client side:
<script type="text/javascript">
var el = document.getElementById("data");
el.value = "Needed_value";
</script>
<form action="./Your_JSP.jsp" method="POST">
<input id="data" type="hidden" value="" />
<input type="submit" />
</form>
server side:
<%
if (request.getParameter("data") != null) { %>
Your value: <%=request.getParameter("data")%>
<%
}
%>
I came across the same issue. I wanted to fetch couple of data in jsp. What I did is, in javascript function I concatenated the data in one variable with delimiter and passed that variable in parameter like this.
function myname()
{
var fname = "FirstName";
var lname = "LastName";
var fullname = fname+","+lname;
document.location.href = "demopage.jsp?name="+fullname;
}
And then in jsp page, we can fetch the full name as,
<c:set var="varname" value='<%= request.getParameter("name") %>' />
Now we can split the first name and last name using delimiter ','.
function call()
{
var name="xyz";
window.location.replace=("a.jsp?m="+name);
}
String name=request.getParameter("name");
if(name!=null){
out.println(name);
}

Categories

Resources