View Full Version : dropdown box value activates a setCookie
motormichael12
10-21-2006, 10:58 PM
I need a script that uses the thing like "if value="6" setcookie(i know what goes here)"
It needs to be in a login form, where if I have a dropdown box and they select a certain time and the cookie for keeping them logged in stays active until then.
I know how to do the setCookie part but I need help setting up the "if" script.
Thanks
jscheuer1
11-04-2006, 08:07 AM
function set_the_cookie(v){
if (v==1)
setcookie(you know what goes here);
else if (v==2)
setcookie(you know what goes here);
else if (v==3)
setcookie(you know what goes here);
else if (v==4)
setcookie(you know what goes here);
}
<select onchange="set_the_cookie(this.selectedIndex);">
The select will return 0 through however many options it has minus one to the 'set_the_cookie()' function when its value is changed by the user's selection. I left 0 out of the function because I thought that 0 means 0, no cookie. You can also have a final:
else
do something here if none of the conditions are met
if you like.
motormichael12
11-05-2006, 02:40 AM
okie... will that work in php as well?
and is there any editing required besides the (you know what goes here) parts and the "do something here if none of the conditions are met" part?
jscheuer1
11-05-2006, 03:43 AM
If you knew this was PHP and wanted it in that language, you should have posted in that forum as, you probably would have gotten to a resolution more quickly there. I don't mind though and, who knows, this background might still be of help to you. PHP is not my strong suit but, I do know a thing or two about it. It can interact with javascript, which is the language I used above but, javascript isn't secure and is useless if the user has it disabled or unavailable. However, if security of a particular operation is unimportant and there is a fall-back method for non-javascript users, javascript can streamline and enhance operations for PHP while at the same time taking some of the load off of the server. I also know that these determinations and actions can be made and performed in PHP but, probably not on a dynamic basis. The page or form would need to (most likely) be submitted before they can be carried through. The syntax and code space are different but, the logic is identical.
One other thing, javascript alone cannot set a server-side cookie if that is your objective, but it can set a client side cookie.
Joseph Lau
05-22-2014, 10:19 AM
I want to create a landing page with a dropdown menu for users to select their native language (e.g. English, French, etc), and they are accordingly redirected to the specific frontpage based on their choice of language. Next time, when they visit the site again, they are then automatically redirected to the frontpage they selected the language (So, they do not need to select the language again). Could you help me to have the codes/scripts?
jscheuer1
05-23-2014, 02:57 AM
Depending upon your country of origin, that might be illegal without also presenting users with an option to approve/disapprove use of cookies first (in those countries where it's mandatory, known as opt in).
Do you have PHP available?
Joseph Lau
05-24-2014, 02:03 AM
No. I do not have PHP scripts.
How about the use of session? Does it also fall into the legal problem?
Thanks again for your advice.
jscheuer1
05-24-2014, 03:17 AM
As far as I know, sessions (server side sessions or any type of session only cookie or storage) are OK (don't require opting in anywhere) but don't last all that long. Do you know the law for your country of origin? Most are not all that strict, and even those that are only require you allow people the opportunity to opt in without requiring it or making it the default. Once they do opt in (most people would if it's made clear that it's only for language and other user defined preferences), it would be transparent. And the selection to opt in could be done just like the selection for language. It's no big deal, you just have to be aware if it's required or not for your site and provide it if it is required.
If you don't have PHP available, what type of session are you talking about?
Joseph Lau
05-24-2014, 03:27 AM
In our place, so far, we do not have any law regulating such opt-in, but not sure in next development. I am afraid if overseas users visit our site and I then fall into the trap of legal problem.
How about session storage?
Thank you very much.
jscheuer1
05-24-2014, 04:45 AM
The law that governs your site is the law of the country that the site is hosted in and/or (if commercial) the primary country that it does business in (not where it's clients are, but where it's incorporated). Session storage and cookies are really the same thing, just a different way of accessing and storing data on the local machine except that session storage never lasts once the browser is closed. With cookies you can set the length of persistence. There is also local storage. With that the data lasts forever for that browser on that computer unless removed by the client or host. The rules (in those countries where they apply) governing cookies also apply to session and local storage - to wit, anything lasting after the browser is closed requires that the user knowingly opt in. Cookies are the most likely to be supported in all browsers, but also the one that users know the most about in general and therefore most likely that they might be turned off or periodically cleared. However, if all you're providing is a service (remember the language), people will be less likely to remove them. Session storage (whether server side or client side) doesn't persist long. Client side it's lost after the browser is closed, server side it times out after a set interval rarely longer than 30 minutes and is also usually lost when the browser closes, so it's probably not what you're interested in. Local storage does persist but is supported only in more recent browsers. If you have no server side login, I think I would be tempted to use local storage with a cookie fall back. A server side login is best though if available. A user logs in. His/her preferences can be saved in a database entry for their account. No opt in is required. And any time that user logs in from any browser or computer, his/her preferences are recalled. With cookies and/or local storage, even if the preferences persist, the user must return with the same browser on the same computer for them to be remembered.
If you're doing any sort of commerce, server side code is almost a necessity.
But, unless I hear differently from you I will set something up as I described (local storage with a cookie fall back).
Joseph Lau
05-24-2014, 05:03 AM
You are right. I am going to set up e-commerce, and planning to identify which functions should be included and how to include.
May I ask you a favour to have 2 options (client side and server side) for study.
Thank you very much of you offer your generous teaching and guidance.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.