Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing CF variable to an html page

Status
Not open for further replies.

mkyzar

Programmer
Oct 19, 2000
56
US
We are using CF4.5 for our applications. We have some new software that we want to use along side of it that uses html pages. I want to set cookies in the cf pages to store login information to pass on to the html pages. So far I've had no luck doing this. I set the cookies and then try to retrieve them on the html page with a javascript function.

In the CF page:
<cfcookie name="loggedin" value="yes">

In the html page:
function Preinitialize(){
$frm.fld0.value= getCookie("loggedin");
}

This is executed onLoad.

We've never used cookies in anything before this and I have only used javascript for very basic form validation.
Please help?

Thanks in advance,
mkyzar
 
We'll need to see the page where the cookies are being set (source) but what it sounds like is you're using a cflocation or something..

Cflocation cannot be processed by the same action before or after cookies are set because they do not actually get set..

Code:
[b]bad:[/b]
  <cfif method eq 1>
    <cfcookie...>
    <cflocation...>
  </cfif>

[b]bad:[/b]
  <cfif method eq 1>
    <cflocation...>
    <cfcookie...>
  </cfif>

[b]good:[/b]
  <cfif method eq 1>
    <cflocation...>
  <cfelse>
    <cfcookie...>
  </cfif>

[b]good:[/b]
  <cfif method eq 1>
    <cfcookie...>
    <script language="javascript" type="text/javascript">
      <!--
        window.location='...url here...may need cfoutput tags...';
      -->
    </script>
  </cfif>

[b]good:[/b]
  <cfif method eq 1>
    <script language="javascript" type="text/javascript">
      <!--
        window.location='...url here...may need cfoutput tags...';
      -->
    </script>
    <cfcookie...>
  </cfif>

in the last two examples, they're good because the html/javscript does not get processed til after it is delivered to the browser.. all cf is already processed.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Okay, this area is completely new to me. The samples you are showing should be placed in the .cfm file and the window.location= should be the url to the page I am going to?

Thanks for your patience.

mkyzar
 
So you are using cflocation?

And your right about the window.location... and yes, use any of the "good" examples you think will help.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
I'm not using cflocation, but here is what I am trying to do. Using CF, we log in to the web site and there are session variables set that helps define which applications a user can access. These various applications are listed on the index.cfm page. I guess I'm not really understanding how exactly cookies work, but on this index page or at some point while logging in, I want to set the cookie to be read by an html form. From the index.cfm page, this page could be any number of html pages. So when you use <cfcookie name="loggedin" value="True"> this doesn't write the cookie to the browser? And getCookie("loggedin") will not retrieve it?

Thank you so much for your help. I think I'm just really, really confused today.

mkyzar
 
Instead of using #getcookie("loggedIn")# try #cookie.loggedIn#

Also for debugging, on the page where the cookie is set, try this piece of code immediately after the cookie is set so it would look something like..

Code:
    <cfcookie name="loggedin" value="True">
    <script language="javascript" type="text/javascript">
      <!--
        alert('Cookie has been set.');
      -->
    </script>

If that box pops up, then you know the cookie should have been set, if it doesn't, the conditions you are requiring for the cookie to be set aren't being met.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
After a second look, the area where I was trying to set my cookie did include cflocation. I moved it to another area and now the cookie is setting, but the field on the html form that I am trying to populate with the cookie's value is still NULL. What am I doing wrong?

 
Are you using #getCookie(name)# or #cookie.name#?

Can you show us the code where you're trying to read the cookie value.. preferrably at least from surrounding <cfoutput> to </cfoutput>?

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
I'm not trying to retrieve the cookie inside of a .cfm file. I'm trying to retriev it in an *.HTML form. I was told that getCookie("cookiename") should retrieve the value of the cookie.
 
Is the answer right here in front of me and I'm too confused to see it? The only code I put into the .cfm page to set the cookie is <cfcookie name="loggedin" value="True">. I can put <cfoutput>#cookie.loggedin#</cfoutput> and it displays that the value is "True."

If I use javascript to direct me to the .html page and use getCookie("loggedin") on that page to populate the field on that form, I get NULL.

The *.html page that I am trying to populate is created with a package called OneForm. I was told by them that this small bit of code should be all I need to populate the field on the form:

function Preinitialize(){
$frm.fld0.value=getCookie("cookiename");
}

This bit of code is executed on the page's onLoad event.

There is no other relevant code.

Thanks for all your help.

mkyzar
 
Oh duh, you're trying to read the cookie with javascript.. I'm sorry... are they on the same domain?

I've honestly never tried setting a cookie with cf or js and reading with the other.

Try this

document.getElementById('fld0').value=getCookie("cookiename");

You may need to name your form.. something like cookieform or something.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
The page I'm setting my cookie on is index.cfm. I've set a link on this page pointing to testform.html. When testform.html loads, it executes function Preinitialize() in javascript which now executes the code you suggested above. I'm still getting a null value. I know you said that you've never tried setting a cookie with cf or js and reading with the other. Do you know if it is even possible or maybe I should try to set a cookie in the index.cfm page using javascript instead and then reading it with testform.html? If so, how do you do that? I'm grasping at straws.

Thanks SO much for your help.

Eternally grateful,
mkyzar
 
Sorry,

I tried to use

<Cfcookie name="test2" expires="2" value="tester">
<Script>
<!--
document.cookie = "test=true";
bikky=document.cookie;
function getCookie(name) { // use: getCookie("name");
var index = bikky.indexOf(name + "=");
if (index == -1) return null;
index = bikky.indexOf("=", index) + 1; // first character
var endstr = bikky.indexOf(";", index);
if (endstr == -1) endstr = bikky.length; // last character
return unescape(bikky.substring(index, endstr));
}

alert(getCookie("test") + getCookie("test2"));
-->
</script></cfif>

And I couldn't get a cookie to set by cf that javascript would read but it could read its own.. so yeah, I'd say if you can, use js to set the cookie.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top