Log in

View Full Version : Help In Passing A Value



NGJ
05-24-2006, 02:46 PM
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.

Twey
05-24-2006, 02:49 PM
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">

NGJ
05-24-2006, 03:15 PM
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.

Twey
05-24-2006, 03:31 PM
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).

NGJ
05-24-2006, 04:16 PM
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.

Twey
05-24-2006, 04:42 PM
// 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.

Twey
05-24-2006, 05:45 PM
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 :)

NGJ
05-24-2006, 08:24 PM
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.

Twey
05-24-2006, 08:36 PM
Nope :)
Can you link us? I've probably made an error somewhere in the cookie functions.

NGJ
05-24-2006, 08:59 PM
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!!!;)

Twey
05-24-2006, 09:06 PM
I don't know how local files will react to cookies. I suggest uploading it somewhere before testing.

NGJ
05-24-2006, 09:15 PM
Will give it a whirl on the server tomorrow & report back.

Cheers,

N.:)

NGJ
05-25-2006, 08:10 AM
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.

NGJ
06-01-2006, 04:30 PM
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.

Twey
06-01-2006, 04:32 PM
It will do that, yes. If you want to store another value, you need to use another name -- just like with a Javascript variable.

NGJ
06-01-2006, 04:39 PM
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,

Twey
06-01-2006, 04:47 PM
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 :)