Store div id value in a variable in JS/JSP - javascript

I have retrieved the value of postCode and assigned the same in
document.getElementById("zipCode").innerHTML=postcode;
I am able to access the same using <div id="zipCode">. I want to store the value of ZipCode in a variable or a dsp:param value in JSP such that i can pass this value in my nested JSP(s). Any help regarding this will be highly appreciated.

You can't do that.
Where as javascript plays on browser i.e on client side.
And jsp plays on server side. I.e Server side.
Inorder to pass that variable to serverside(jsp or what ever you have to make a request).

Why dont you fill an hidden input on your form, than, when you submit the form a param with this hidden input name will be passed together, than you will be able to get this parameter with dsp:param.

I finally figured out a solution for the same. I stored the value in a Cookie through Java script and in turn used the Cookie in my Java class to retrieve the Cookie Value.

Related

Is postback mandatory for hidden fields to work?

I wan't to know if a hidden field essentially need to have a postback to send or access its value to server side?
for eg: if we have a hidden field x and it was set to some value in Javascript on client side. Can't we access this field on Server codebehind as soon as it set on client side without postback?
If you want just to get the value from hidden field on server side without sending the post request, you can send the AJAX call. I believe we would need to have more details from you to give more specific answer.
Additionally your postback work might be missleading.
Hidden field needs to be inside tag to send this to server. If you check the form tag you can manipulate the method attribute to send values with POST or GET request.
if you set the hidden field value at the client side script you can also send an Ajax request with the value to the server instead of getting the field value from the HTML DOM.
Example :
function setValue(fieldValue){
$("#hiddenField").val(fieldValue);
$.get( "/setfield?value="+fieldValue, function( data ) {
//handle response data if any
});
}
note that this specific solution requires JQuery, if necessary, you can achieve the same thing without it but it's more complicated.
Hidden Fields store the value in the page itself, hence do not use server resources.

Accessing form values using javascript in Ruby on Rails

Let's say I have an input box, on the client-side, I want to access the value of this input box and check if it exists as a record in a model. If so, I want the data to be shown. However, I want this to be done without clicking a submit button/reloading the page.
Can someone show me some sample code?
Sample code is very much frowned upon in these parts, I can give you an outline of what you could do. Use jquery to get the value of the input box, and submit this using an ajax request to some url. Map that url to a controller action that checks to see if that record exists in the db, and return some json that indicates whether it does or not. Then in your javascript, when the data is returned, you can display that to the user.

How to pass javascript variable/object from one page to another?

I want to pass javascript object/ variable from page to another. so that i can use data for some fields, i want this values to be hidden and not visible while i my passing value from one page to another
I have already seen many examples of pass via http parameters, i dont want that and also session variables manage on server side, cause nothing has to be manage on sever side for that page.
i want to pass value as a hidden field or value to next page. how to pass it to new page when i open new page via
window.location="website/nextpage.jsp";
Also a note, i am beginner, so please sorry if the question seems to vague.
You can:
Use hashes in the url by reading the window.location.hash. Similar to GET requests, but does not need the server for passing. However, the parameters are visible in the url.
Using cookies. Have JS "plant" the cookies on the previous page and read them on the receiving page.
You could use DOM storage as well. Similar routine as cookies, plant and read.
Assuming you do not want to use session variables, cookies or local storage:
You can not "hide" a parameter so that your website user will not be able to see it.
If you submit data via a POST request - you can use hidden form elements.
<form method="post">
<input type="hidden" name="state" value="{YOUR PARAMETER}" />
If you use window.location - you will have to do it with a GET request
window.location="website/nextpage.jsp/param/{YOUR PARAMETER}";
I don't know about JSP specifically, but using a form with POST method hides data from (standard) users.
why dont you use html5's localStorage and sessionStorage for the purpose. for more help visit here

Setting values in <input> tags after retrieving them from Database

I have JSP page. In that page under HTML tags i have <input> tags. Now the values for these input tags are to be set, after they retrieved from the database using JDBC connectivity. Let's say i have a page page1.jsp in which i have a javascript code which contains a function inside which i am redirecting to the page where values are retrieved using database connection. Now when i execute query i get all the values in result set variable. I want these values to be send back again to page1.jsp so that i can use them to set as values for my tags. I want to know how should i do this. should i store each value from result set variable to a session variable and use them later, or should i return rs variable back to page 1 and set values in <input> tag like <%=rs.getInt("String")%>. A guy suggest me to use Dynamic objects. I have heard about them, but never used them. What's the better way of doing this.
First solutions is simple as you said you can store the values in session and retrieve them in page1.jsp. But i think you should go for Javascript Asyncronuos call generate your response in xml , Retrive it and populate into your page without refreshing your page.(Contents Updation). i.e
xmlhttp.open("GET","demo_get.asp",true);
xmlhttp.send();
More Information Mastering Ajax

get database field value using javascript

Is there any other way I could access the database using javascript? I have progress bars and I need to check whether the value of a field in my database is true to keep them moving. I've already google-d about it and I found out that it's not possible. If I use cookies, then I have to redefine the cookie everytime I change the computer where I log in.
Thank you very much! :)
You can use ajax to call a php (or asp or coldfusion) script which will return the value of the database field

Categories