Click to See Complete Forum and Search --> : Cookie issue
Arguo
02-17-2004, 02:00 PM
Hi all,
I am still confused by setting cookies. Would you tell me what
is wrong with the code below?
Thank you very much.
Arguo
<html>
<head>
<script language="JavaScript">
<!--
function setField()
{
var v = document.myType.value;
document.cookie = "myV=" + v;
}
-->
</script>
</head>
<body onLoad="setField()">
This is my cookie:
<%
Dim thisNum
thisNum = request.cookies("myV")
%>
<input name="myType" type="text" size="8"/><%= thisNum%><br>
</body>
</html>
kbrown2974
02-17-2004, 02:16 PM
Try putting you input inside a form in the html. In your javascript access it by document.myform.txt.value
Arguo
02-17-2004, 03:44 PM
Thanks a lot.
Arguo
buntine
02-17-2004, 08:34 PM
If you want to set cookies, do it on the server side.. Its not a good idea to mix Javascript with ASP.
Arguo
02-18-2004, 10:59 AM
Yes, you are right. I really want to set this cookie on server side.
However, I don't know how to do it.
My page consists of a text input box, and a select list, please look at
the following code.
1st step: Opening the page
At this moment, the page shows users only the text box, and waits for
a user inputing his favor number;
2nd step: Showing the select list
Based on user's favor number, the page reloads again, showing the text box
with entered number and specific selection.
My concerns are:
1. How to keep the number appearing in the text box when the page reloads?
2. How to set cookies on server side in this case?
Thank you very much.
Arguo
<html>
<head>
<script language="JavaScript">
<!--
function setFnum(wValue)
{
var v = wValue;
document.cookie = "myV=" + v;
LoadOnce();
}
function LoadOnce()
{
window.location.reload();
}
-->
</script>
</head>
<body>
<form name='myForm'>
Your favor number is:
<%
Dim thisNum
thisNum = request.cookies("myV")
%>
<input name="myType" value= "<%= thisNum%>" type="text" size="8" onChange="setFnum(this.value)" />
<br>
<!-- Display a select list based on the favor number that a user entered -->
<br>
</form>
</body>
</html>
buntine
02-18-2004, 11:42 AM
You do not need to set a cookie for this.. The value of the selected index will be passed along the queryString when the form is submitted.
Invoke it using the following code:
dim selectedIndex = request.queryString("yourSelectBox")
Infact, setting a cookie will only slow this script down.
Arguo
02-18-2004, 01:27 PM
Buntine, thx for your reply.
The following code is getting a user input, and reloading the page. When the page loads again, the text box should keep the value that the user already entered.
The following code doesn't work in correct way. When the page loads again, the text box appears empty.
Do you know what 's wrong with it?
Thanks a lot.
Arguo
<html>
<head>
<script language="JavaScript">
<!--
function LoadOnce()
{
window.location.reload();
}
-->
</script>
</head>
<body>
<form name='myForm'>
Your favor number is:
<%
dim thisNum
%>
<input name="myType" value= "<%= thisNum%>" type="text" size="8" onChange="LoadOnce()" />
<%
thisNum = request.form("myType")
%>
</form>
</body>
</html>
buntine
02-18-2004, 11:45 PM
You are not submitting the form.. ASP needs to be invoked from the server.
This data is not being processed because the user has not clicked a submit button.
If you add a submit button and check the <form> element it will work.