Retrieving variable in onclick with javascript - 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
}
%>

Related

How to pass a javascript variable in a PHP url??

I would like to redirect to the chosen page after having validated the form.
I started this:
<form action = "planning.php?court= variable" method = "POST">.
<script>
function courtNumber () {
var variable = document.getElementById ("tennis_court"). value;
alert (variable);
}
</script>
I catch the correct value in my alert when I click on validate, now I would like to insert it in my tag.
Thank you.
You can reassign the action of the form.
function courtNumber () {
var variable = document.getElementById("tennis_court").value;
document.querySelector("form").action = "planning.php?court=" + encodeURIComponent(variable);
}
First you can add one id to your form, so you can access it in your function.
<form action = "planning.php?court= variable" method = "POST" id="myForm">
</form>
Then, in your script you can acces the action property and set the value there.
<script>
function courtNumber ()
{
var variable = document.getElementById("tennis_court").value;
alert (variable);
document.getElementById("myForm").action = "planning.php?court=" + variable;
}
</script>

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

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

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()" />

Submit form and a textarea on ajax post

I want to submit a form and another textarea on my ajax post, will that be possible? Here is my code
<script>
function updateuseracc(form, password)
{
var p = document.createElement("input");
form.appendChild(p);
p.name="p";
p.type="hidden";
p.value=hex_sha512(password.value);
password.value="";
var email=$("#curemail").val();
$.post('updateuser.php',$('#myform').serialize(),textarea:textarea),(function(data){
});
}
</script>
I'm just getting errors with this
First textarea is not a variable. If you want to get the value of it do this:
var text = $(textarea).text ();
Put data in curly braces, function is third parameter.
$.post('updateuser.php', {
form:$('#myform').serialize(),
textarea:text},
function(data){
//code...
});
In addition, you should take care of ANY security features on the server side!
http://api.jquery.com/jquery.post/

javaScript Beginner: Sending data from java-script to servlet?

I am creating dynamic form in JavaScript. I want to send the data's from JavaScript to servlet. I dont use any submit Button in my Form.Using button only i want to send the data's. Anyone help me please.
This is my code:
var myform=document.createElement("form");
myform.setAttribute("id", "myform");
var tname=document.createElement('input');
tname.setAttribute('type','text');
tname.setAttribute('name', "name");
var leaveMessage=document.createElement('input');
leaveMessage.setAttribute('type','button');
leaveMessage.setAttribute('name', "msgButton");
leaveMessage.setAttribute('value', "Leave Message");
myform.appendChild(tname);
myform.appendChild(leaveMessage);
document.body.appendChild(myform);
add some statements also
var myform=document.createElement("form");
myform.setAttribute("id", "myform");
myform.setAttribute('action', "/mypage");
myform.setAttribute('method', "post");
var tname=document.createElement('input');
tname.setAttribute('type','text');
tname.setAttribute('name', "name");
var leaveMessage=document.createElement('input');
leaveMessage.setAttribute('type','button');
leaveMessage.setAttribute('name', "msgButton");
leaveMessage.setAttribute('value', "Leave Message");
myform.appendChild(tname);
myform.appendChild(leaveMessage);
document.body.appendChild(myform);
document.forms["myform"].submit();

Categories

Resources