Click to See Complete Forum and Search --> : Session Cookie Problem


urbanxtremes
11-28-2006, 09:03 AM
I am writing a cookie (session base) based form that has 3 steps.

1st step: Title, 1st Name, Last Name, and a couple of selects (test_step1.html)
2nd step: Address details (test_step2.html)
3rd step: Displays both cookies ready to be sent (test_step3.html)

Now, the issues seems to lie in the fact, that I can pass step-1 cookie to step-2, but when I go to step-3 it only shows step-2 twice…

I have no idea why this is not working, would really appreciate some help.

Step 1
function process(){
var sal = document.form1.sal.value;
var firstname = document.form1.firstname.value;
var lastname = document.form1.lastname.value;
var investor = document.form1.investor.value;
var type = document.form1.type.value;
var hedge = document.form1.hedge.value;
var fund = document.form1.fund.value;
var other = document.form1.other.value;

var step1 = " Dear " + sal + " " + firstname + " " + lastname + " <br>Registering for " + investor + " <br> " + type + " <br> " + hedge + " <br> " + fund + " <br> " + other + " ";

//step1 = escape(document.cookie);
document.cookie = escape(step1);

window.location = "test_step2.html";
}

Step 2

<script language="JavaScript">
var step1 = unescape(document.cookie);
</script>
<SCRIPT type="text/javascript">

function process2(){
var address1 = document.form2.address1.value;
var address2 = document.form2.address2.value;
var country = document.form2.country.value;
var postal = document.form2.postal.value;
var contact = document.form2.contact.value;
var fax = document.form2.fax.value;
var email = document.form2.email.value;
var cemail = document.form2.cemail.value;
var password = document.form2.password.value;
var memorable = document.form2.memorable.value;

var step2 = " <br> " + address1 + " <br> " + address2 + " <br> " + country + " <br> " + postal + " <br> " + contact + " <br> " + fax + " <br> " + email + " <br> " + cemail + " " + password + " <br> " + memorable +" ";


document.cookie = unescape(step2);
window.location = "test_step3.html";
}
</SCRIPT>

Step 3
<script language="JavaScript">
var step1 = unescape(document.cookie);
</script>
<script language="JavaScript">
var step2 = unescape(document.cookie);
</script>
</head>

<body>
<script language="JavaScript">
document.write (step1);
</script>
<p></p>
<script language="JavaScript">
document.write (step2);
</script>


Here is an example of what I am trying to do:
http://www.markhebblewhite.net/Cookie_Form/test_step1.html

I hope someone can help me please? :confused:

eriksmoen
11-28-2006, 12:04 PM
step1 and step2 variables are calling the same cookie in Step 3, and Step 2 is over-writing Step 1's cookie... so in Step 3 step1 and step2 both contact step2's data, thus why it is displaying twice.

the format of a cookie is like such:

cookie_name=data that goes in cookie; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=path_to_cookie; domain=domain_cookie_resides_on; secure

expires, path, domain, and secure are all optional.

so to fix this problem, you would need to either create 2 cookies with different names (eg. cookie1, cookie2), or just get the data from step1 when on Step 2, then combine step1 and step2 and resave the cookie, and then just display the data when you get to Step 3.

I found a couple of functions somewhere on the net that have helped me greatly with Javascript cookies...


function getCookie( name )
{
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ';', len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure )
{
var today = new Date();
today.setTime( today.getTime() );
if ( expires )
{
expires = (1000 * 60 * 60) * expires;
}
var expires_date = new Date( today.getTime() + (expires) );
document.cookie = name+'='+escape( value ) +
( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) +
( ( path ) ? ';path=' + path : '' ) +
( ( domain ) ? ';domain=' + domain : '' ) +
( ( secure ) ? ';secure' : '' );
}

function deleteCookie( name, path, domain )
{
if ( getCookie( name ) ) document.cookie = name + '=' +
( ( path ) ? ';path=' + path : '') +
( ( domain ) ? ';domain=' + domain : '' ) +
';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}


getCookie( name ):
gets the data from the named cookie

setCookie( name, value, expires, path, domain, secure ):
creates a new cookie, or over-writes the old cookie

deleteCookie( name, path, domain )
deletes the cookie