Forum Moderators: open

Message Too Old, No Replies

How to write a value into a hidden field?

         

wolter09

4:13 am on May 14, 2006 (gmt 0)

10+ Year Member



hi there folks,

could someone perhaps provide me a sample code that will take the name=value pairs from the cookie and add them to a hidden field?

I'm new to javascript so I have no idea. (I'm learning perl already so javascript will have to wait). I've tried searching for it but no luck so far. I did stumble upon quite a few tutorials and I think that you use windows.location.search to get the name=value pairs and innerhtml to set the value of the hidden field. Am I close? Anyways, a working example is the way I learn best so if someone could provide me with one that would be greatly appreciated.

MichaelBluejay

11:05 am on May 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Cookie parsing code is available all over the net so I won't repeat that here. Once you have the data in variables, getting the data into a field is easier. It doesn't matter whether the field is hidden or not, it works the same way:

document.myform.myfield.value = variable;

Make sure your form and fields are named, so Javascript can fiend them, i.e.

<form name=myform>
<input type=hidden name=myfield>

wolter09

1:17 pm on May 14, 2006 (gmt 0)

10+ Year Member



Shouldn't the javascript below work if someone enters an url like this in their browser: [mydomain.com?refid=435...]

What am I doing wrong?

<HTML>
<HEAD>
<script type="text/javascript">
<!--
function querySt() {
hu = window.location.search.substring(1);
}

document.myform.myfield.value = hu;
}
-->
</script>
</HEAD>
<BODY onload="querySt">
<form name="myform" method="post" action="">
<input type="hidden" name="myfield">
</form>
</BODY>
</HTML>

MichaelBluejay

3:52 pm on May 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think this...

<BODY onload="querySt">

should be this...

<BODY onload="querySt()">

wolter09

6:23 pm on May 14, 2006 (gmt 0)

10+ Year Member



ok, I found a script that works but thee's one function inside the script that needs this one minor change. This is the function unchanged(in other words, it works like this):

function DoTheCookieStuff()
{
username=getCookie('username');
if (username!=null) {alert('Hi there '+username+' - Good to see you again!')}
else {username=window.location.search.substring(1);setCookie('username',username,365)}
}

As you can see there's a if/else in there. But I don't need an if/else. I just need the if in there and get rid of the else. So I changed it to the following:

function DoTheCookieStuff()
{
username=getCookie('username');
if (username=null) {username=window.location.search.substring(1);setCookie('username',username,365)}
}

After doing that the script doesn't work anymore. Can anyone help me figure out why?

wolter09

6:30 pm on May 14, 2006 (gmt 0)

10+ Year Member



aha.. i found the problem. I only used one "=" sign!