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
Related
I am trying to figure out the best way to go about this. I have a form with a bunch of input fields eg. first name, last name, address, etc. My question is what is the best method to go about storing cookies as the user is entering data and progressing through the form.
My first thoughts were to use the onchange event and call a create cookie function, but my worry is that if the user were to change a field twice would two cookies be made even though they were passed in the same name? Or would the first one with the same name be overridden?
Thank you
Do you really need to create a separate cookie for every form field? You could have a method that serializes the whole form, and stores all the data in a cookie.
With jQuery, you can use the .serialize() method:
http://api.jquery.com/serialize/
And you can also use the jquery-cookie for storing the data in the cookie:
https://github.com/carhartl/jquery-cookie
Depending on the usage you're looking for a cookie may not be what you're looking for. Let us know what you want to do and we'll help you get your solution. In any case:
You can create and destroy cookies with javascript: http://www.w3schools.com/js/js_cookies.asp
To update a cookie you would delete the cookie and recreate it.
However I would posit that you probably don't need cookies. You could store all your data in hidden fields and send them on form submission. Or send them to the server with ajax and have the server store them in the session.
Cookies are stored on the user's local machine as key-value pairs, with an optional expiration period (the amount of time before the cookie is no longer "valid").
Depending on your key naming choice, it will persist as long as it hasn't had its value changed or has been explicitly cleared.
You could do this with an onchange event, and just use the form field ID as the key and the entered text as the value.
Though it might be a better idea to store the entire form as a cookie using jQuery.
Depending on your browser-compatibility requirements, you might look into something like local storage instead of setting and resetting cookies
https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage
Or, as others have suggested, serialize the full form in a single cookie.
Through a little experimentation, creating a cookie with simply document.cookie and a name= value pair and then updating the cookie using the same name and a different value does not create two cookies, it does indeed just update the first one. So my onchange idea is seemingly working fine. Through the experience I have also found that local document.cookies do not seem to work on google chrome, but function just fine in mozilla firefox.
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.
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.
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
I'm developing a login page in which i have a three fields and a checkbox.
Three fields are:
a code
an username
a password
I want to let user clicking on checkbox to remember (even if close browser) the code and login but not the password. Can you help me? I hope to choose right question :)
That's simple, Use browser cookies to store the same. So that next time the user logs in you can pick the value from the cookie in his browser .
IMPORTANT : BUT Here's how you make it secure.
Since you are storing the UserID etc. I would recommend encrypting and storing it, next time you can pic the value decrypt and then show back
OR
Just set a flag in cookie on click of checkbox (to remember) and populate the User ID and code from the server code if that flag is true the next time you see from cookie.
EDIT : Since you have not mentioned what language/tech you are developing this, just use server side api's to read the cookie values or jQuery/script
Have you never read about cookies?