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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting data from Select boxes using cookies 2

Status
Not open for further replies.

abienz

Programmer
Aug 13, 2001
53
GB
Hi there,

In reference to a previous post I read regarding displaying data from a
cookie into a form, I want to know if it's possible to have data
pre-selected in a select box via a cookie.

I have a form with 7 different select boxes, 3 of which are generated
dynamically from a database, once this form has been submitted I want a
cookie stored on the clients machine, so that next time they enter that
page, their previous selctions are automatically displayed. Is there a
way to do this, as it can be done with text boxes?

Cheers,
Alex.
 
You just need to check the values, and then enter 'selected' inside the option tag when you find the one that is stored. For example, say you have the value of your cookie in a variable called cookieValue, then it might look like:

<%
response.write(&quot;<select name=theSelect>&quot; & vbcr)
response.write(&quot;<option value=none>SELECT ONE</option>&quot; & vbcr)
while not rs.eof
response.write(&quot;<option value=&quot;&quot;&quot; & rs(&quot;value&quot;) & &quot;&quot;&quot;)
if rs(&quot;value&quot;) = cookieValue then
response.write(&quot; selected&quot;)
end if
response.write(&quot;>&quot; & rs(&quot;value&quot;) & &quot;</option>&quot; & vbcr)
rs.movenext
wend
response.write(&quot;</select>&quot; & vbcr)
%>

hope that helps! :)
Paul Prewett
penny.gif
penny.gif
 
Ok, that's cool, but I've realised that I've got a little more of a problem than I at first realised!

I want to create the cookies using ASP if possible, but this doesn't seem to be as easy as I expected. I've got a form called form1, with my select boxes as an example let's have a select box called 'type'. Now when I click on the submit button I have an onSubmit function in the form tag which points to a function called createcookie(). This function is thus..

function createcookie(){
Response.Cookies(&quot;myCookie&quot;)(&quot;type&quot;) = document.form1.type.value
}

This comes up with errors though saying response is undefined, but I found this code from an ASP book?! What am I doing wrong here?! Please help

Cheers,
Al.
 
The deal here is that you are trying to mix asp with javascript. The response object is part of asp and not javascript. You should create your cookie on the asp page that the form is being submitted to.

page the form is submitted to
---------------------------------------------

<%
Response.Cookies(&quot;myCookie&quot;)(&quot;type&quot;)=Request.form(&quot;Type&quot;)
'and all the rest of the fields you want to write
%>


 
Hi I understand now that I was doing that wrong, I keep forgetting that Javascript is dynamic and ASP and VBScript runs when the page opens, however now that I rectified my error from before (I now create the cookie on the page that opens once the form is submitted), I still can't create a cookie?!

basically at the top of my new page I Dim some variants and assign them to their relevant form objects eg..

Dim cmd_type
IF Request(&quot;type&quot;) <> &quot;&quot; THEN cmd_type = Request(&quot;type&quot;)

Then the cmd's are passed into a stored procedure, after this I declare my cookie

Response.Cookies(&quot;myCookie&quot;)(&quot;type&quot;) = Request.Form(&quot;type&quot;)

But no cookie is being created!? why not? Am I missing something here?

Cheers for all the responses by the way
 
Alright I managed to sort it, I was gettting confused with me response, and my requests heheh

It works a treat! Thanks guys
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top