View Full Version : Help In Passing A Value
Hi,
I guess (simply) what I'm trying to do here is pass a value.
I'd like to take an input from a user, within a FORM, and then later refer to that value - displaying it. This will take place within the one .htm page.
Do I need to have SUBMITted/POSTed the value from the form first of all?
I seem to be going around the houses with this one.
Can anyone help please?
Thanks in advance,
N.
No, you can use ECMAScript to do it if it's all within that one page.
<input type="text" onchange="document.getElementById('pine').innerHTML = this.value;">
<!-- ... -->
<p id="pine">
Good Stuff - Thank you.
That really helps! :)
To take this further.... Would I be looking into the realms of cookies if I wanted to pass the same value outside of the .htm page and into another?
For example, a user enters their name on a menu page & I wanted to recall that value again within a 'sub'-page ... is this easily achieved?
Thanks again for your guidance.
The best thing to do there would be to use a server-side language such as PHP or ASP. But yes, you can use cookies or GET requests (you can read it off the address with ECMAScript).
I'm having to stick with a client-side approach here.
"But yes, you can use cookies or GET requests (you can read it off the address with ECMAScript)." ....
Do you know of a decent cookie example to show passing values between pages... ? Alternatively, are GET requests specific to server-side activity?
Thanks again,
N.
// cookiefuncs.js
function getCookie(name) {
var value = document.cookie;
value = value.substring(value.indexOf(name));
value = value.substring(value.indexOf("=") + 1);
value = value.substring(0, value.indexOf(";"));
return value;
}
function setCookie(name, value) {
var orig = document.cookie,
pos = orig.indexOf(name + "="),
valuepos = pos + name.length + 1;
if(pos == -1) document.cookie += name + "=" + value + ";";
else document.cookie = document.cookie.replace(new RegExp(name + "=[^;]+;"), name + "=" + value + ";");
}
<script type="text/javascript" src="cookiefuncs.js"></script>
<!-- ... -->
<input type="pine" onchange="setCookie(this.name, this.value);">
<script type="text/javascript" src="cookiefuncs.js"></script>
<script type="text/javascript">
document.write(getCookie("pine"));
</script>
djr33
05-24-2006, 05:42 PM
The above will work, but might be limited.
You may even need to use a database or text files to store data using a serverside language, depending on complexity.
For example, if other users need to see that data, it can't be a cookie.
Or if you want to store that data even if they clear cookies.
I mean... that's fine. But... there are limits, so start looking at php unless you just want a little bit of functionality.
Especially if you get into passwords or anything, then you will need a lot more than clientside code.
I've already suggested this. The OP cannot use server-side technologies.
djr33
05-24-2006, 05:49 PM
Ah, sorry.
And... not saying you're wrong... just making it clear that the only secure/fully functional way is to use a serverside language.
But the above code will mimic some of the features failing that, so, if you can't do serverside, that's a great alternative.
Just wanted to mention it :)
Thanks guys.
Sorry - I am still a relative beginner to all this...
I have "cookiefuncs.js" saved as a file and the following 2 simple .htm files:
<html>
<head>
<script type="text/javascript" src="cookiefuncs.js"></script>
</head>
<body>
<form>
<table>
<tr>
<td><input type="pine" onchange="setCookie(this.name, this.value);"></td>
</tr>
</table>
<input type="submit" value="Submit!">
</form>
</body>
</html>
Then,
<html>
<head>
<script type="text/javascript" src="cookiefuncs.js"></script>
</head>
<body>
<script type="text/javascript">
document.write(getCookie("pine"));
</script>
</body>
</html>
..... Can you spot my mistake ??!?!?!?!!!
Thanks again,
N.
Nope :)
Can you link us? I've probably made an error somewhere in the cookie functions.
I've only got them on my desktop at the moment - running the 3 files.
It takes the input - just doesn't return anything when I run the last .htm page containing.....
<script type="text/javascript">
document.write(getCookie("pine"));
Any thoughts? Appreciate your help...
Certainly beats watching Big Brother!!!;)
I don't know how local files will react to cookies. I suggest uploading it somewhere before testing.
Will give it a whirl on the server tomorrow & report back.
Cheers,
N.:)
Hmmm, your script works fine here today ... though this is, again, just on another desktop - not running from a server.
Are cookies known to be a bit 'hit & miss' - depending on system settings?
I haven't consciously disabled cookies on the other machine and I wasn't aware of my system flagging that a cookie was being blocked last night.
If so, is there a way of detecting this and flagging a message to the user - "this app. requires cookies to be enabled", etc...?? :confused:
Up until now, I thought that cookies just exist - it's up to the user to decide whether to delete them as part of the usual, timely desktop clean-up regime.
Thanks again,
N.
TWEY,
Can you please advise how your script could be adjusted to allow for multiple values to be remembered?
As it stands, if I
onchange="setCookie(this.name, this.value);"
more than once (using, Pine, Oak, Elm, etc) - it only remembers the last value entered.
Many thanks in advance,
N.
It will do that, yes. If you want to store another value, you need to use another name -- just like with a Javascript variable.
Hhhmm,
Am I doing this correctly?
<td><input type="pine" onchange="setCookie(this.name, this.value);"></td>
<td><input type="maple" onchange="setCookie(this.name, this.value);"></td>
<td><input type="oak" onchange="setCookie(this.name, this.value);"></td>
<td><input type="elm" onchange="setCookie(this.name, this.value);"></td>
Then...
<script type="text/javascript">
document.write(getCookie("pine"));
</script>
<script type="text/javascript">
document.write(getCookie("maple"));
</script>
<script type="text/javascript">
document.write(getCookie("oak"));
</script>
<script type="text/javascript">
document.write(getCookie("elm"));
</script>
???? - Thanks,
Yep.
If you're still having problems, please post a link to a demo page. I didn't test that script, so I may very well have done something wrong somewhere :)
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.