PDA

View Full Version : session varible basics


Shaft
06-14-2005, 05:52 PM
I'm trying to use a session variable for the first time and it is doing my head in.

On the web page with the form that contains the field I want to store in the variable I have placed the following code.

<% Session("Audit_ID") = Request.Form("txtAuditID").value %>

On the destination page I have placed the following code in the body of the asp page.

<body>
<%= Session("Audit_ID") %>
</body>

This is producing no errors but no content either.

Please help

BaldEagle
06-14-2005, 06:15 PM
Is the form page submitting to itself? If not then move the Session("Audit_Id") = Request.Form("txtAuditId").value to the destination page (or page being submitted to).

BaldEagle

Shaft
06-14-2005, 06:32 PM
Tried that still can't get it to work heres the code on my destination page.

<%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>
<% Session("AuditID")=Request.Form("AuditID").value %>
<html>

<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

<body>
<%Response.Write(Session("AuditID"))%>
</body>
</html>

Freon22
06-14-2005, 07:11 PM
<% Session("AuditID")=Request.Form("AuditID").value %>

If your Request.Form is empty then you wouldn't have anything in it.
Try a test.
Just to see if the server is writing session cookies
session("test") = "1234"
Response.Write("<br>" & session("test"))
also check your form to make sure it has something in it. And are you writing this in javascript or vbscript?

fractalvibes
06-14-2005, 07:50 PM
unless that first page is posting back to itself, the form field will be empty it looks like.

fv

nikkiH
06-14-2005, 09:56 PM
.value?
The code is written in VB style, yet the directive says javascript...
I'm confuzzled.
Which did you want to use?

VB would use Request.Form("name")
NO .value at the end.
I don't use javascript for ASP, so I'm not sure if that .value is okay or not. Might be referencing a property that has no value or something.
Client-side javascript would use it, but...
Hrm.
Try taking that off there. See what happens.

miranda
06-14-2005, 11:08 PM
Tried that still can't get it to work heres the code on my destination page.

<%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>
<% Session("AuditID")=Request.Form("AuditID").value %>
<html>

<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

<body>
<%Response.Write(Session("AuditID"))%>
</body>
</html>


Request.Form("AuditID").value says that you are getting the AuditID from a previous page form where the forms method is set to post. Have you tried Response.Write(Request.Form("AuditID").value) to ensure that there is a value being passed?

Shaft
06-15-2005, 12:24 PM
Request.Form("AuditID").value says that you are getting the AuditID from a previous page form where the forms method is set to post. Have you tried Response.Write(Request.Form("AuditID").value) to ensure that there is a value being passed?

I've confirmed that session variables work by hard coding one with a value so it must be a problem with the form parameter.

I've tried the above suggestion and it returns nothing so I tried this :-

<% Response.Write(String(Request.Form("AuditID"))) %>

and it returns 'undefined'.

Any suggestions as to what the problem is?

NancyJ
06-15-2005, 01:00 PM
dumn question... does your form have method "post"?

Shaft
06-15-2005, 02:32 PM
Yes, it does and everything is spelt correctly.

I can't see why it doesn't work, it's driving me mad.

nikkiH
06-15-2005, 03:04 PM
Another "dumb" question...
You don't have an input type of file in that form, do you?
And you're not setting enctype to anything odd, are you?
Post your html for your form. Maybe we'll catch something you're missing.

Shaft
06-15-2005, 03:34 PM
I've realised the root of my problem now the form's action isn't the destination form which is why it is undefined.

I'm using dreamweaver to create the form which inserts the fields into a record of a database by means of a server behaviour which then redirects to my destination form which is where I was getting confused.

But I still don't know where to define my session variable, cos i can't work out whats going on in the code at the top. I've attached the first part of the code up to the form action.

I hate dreamweaver.


<%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>
<!--#include file="Connections/Quality_Audit_DB.asp" -->
<%
// *** Edit Operations: declare variables

// set the form action variable
var MM_editAction = Request.ServerVariables("SCRIPT_NAME");
if (Request.QueryString) {
MM_editAction += "?" + Request.QueryString;
}

// boolean to abort record edit
var MM_abortEdit = false;

// query string to execute
var MM_editQuery = "";
%>
<%
// *** Insert Record: set variables


if (String(Request("MM_insert")) == "form1") {

var MM_editConnection = MM_Quality_Audit_DB_STRING;
var MM_editTable = "[Data Input]";
var MM_editRedirectUrl = "Confirm.asp";
var MM_fieldsStr = "txtAudDate|value|lstAuditor|value|lstEngineers|value|lstContract|value|txtAddress1|value|txtAddress2 |value|txtCounty|value|txtPostcode|value|txtDateComp|value|txtApp1|value|txtApp2|value|txtApp3|value |txtApp4|value|RadioGroup1|value|RadioGroup2|value|RadioGroup3|value|RadioGroup4|value|RadioGroup5|v alue|RadioGroup6|value|RadioGroup7|value|RadioGroup8|value|RadioGroup9|value|RadioGroup10|value|Radi oGroup11|value|RadioGroup12|value|RadioGroup13|value|RadioGroup14|value|RadioGroup15|value|RadioGrou p16|value|RadioGroup17|value|RadioGroup18|value|RadioGroup19|value|RadioGroup20|value|RadioGroup21|v alue|RadioGroup22|value|RadioGroup23|value|RadioGroup24|value|RadioGroup25|value|RadioGroup26|value| RadioGroup27|value|RadioGroup28|value|RadioGroup29|value|RadioGroup30|value|RadioGroup31|value|Radio Group32|value|RadioGroup33|value|txtComments|value|lstDiff1|value|txtDiff1|value|lstDiff2|value|txtD iff2|value|lstDiff3|value|txtDiff3|value|lstDiff4|value|txtDiff4|value|lstDiff5|value|txtDiff5|value |txtImprovement|value";
var MM_columnsStr = "[Audit Date]|',none,NULL|Auditor|',none,''|Engineer|',none,''|Contract|',none,''|AddressLine_1|',none,''|Address Line_2|',none,''|County|',none,''|Postcode|',none,''|JobCompleteDate|',none,NULL|App1|',none,''|App2 |',none,''|App3|',none,''|App4|',none,''|CheckListItem_1|none,none,NULL|CheckListItem_2|none,none,NU LL|CheckListItem_3|none,none,NULL|CheckListItem_4|none,none,NULL|CheckListItem_5|none,none,NULL|Chec kListItem_6|none,none,NULL|CheckListItem_7|none,none,NULL|CheckListItem_8|none,none,NULL|CheckListIt em_9|none,none,NULL|CheckListItem_10|none,none,NULL|CheckListItem_11|none,none,NULL|CheckListItem_12 |none,none,NULL|CheckListItem_13|none,none,NULL|CheckListItem_14|none,none,NULL|CheckListItem_15|non e,none,NULL|CheckListItem_16|none,none,NULL|CheckListItem_17|none,none,NULL|CheckListItem_18|none,no ne,NULL|CheckListItem_19|none,none,NULL|CheckListItem_20|none,none,NULL|CheckListItem_21|none,none,N ULL|CheckListItem_22|none,none,NULL|CheckListItem_23|none,none,NULL|CheckListItem_24|none,none,NULL| CheckListItem_25|none,none,NULL|CheckListItem_26|none,none,NULL|CheckListItem_27|none,none,NULL|Chec kListItem_28|none,none,NULL|CheckListItem_29|none,none,NULL|CheckListItem_30|none,none,NULL|CheckLis tItem_31|none,none,NULL|CheckListItem_32|none,none,NULL|CheckListItem_33|none,none,NULL|Comments|',n one,''|Difficulties_No1|none,none,NULL|Difficulties_Comm1|',none,''|Difficulties_No2|none,none,NULL| Difficulties_Comm2|',none,''|Difficulties_No3|none,none,NULL|Difficulties_Comm3|',none,''|Difficulti es_No4|none,none,NULL|Difficulties_Comm4|',none,''|Difficulties_No5|none,none,NULL|Difficulties_Comm 5|',none,''|Improvements|',none,''";

// create the MM_fields and MM_columns arrays
var MM_fields = MM_fieldsStr.split("|");
var MM_columns = MM_columnsStr.split("|");

// set the form values

for (var i=0; i+1 < MM_fields.length; i+=2) {
MM_fields[i+1] = String(Request.Form(MM_fields[i]));
}

// append the query string to the redirect URL
if (MM_editRedirectUrl && Request.QueryString && Request.QueryString.Count > 0) {
MM_editRedirectUrl += ((MM_editRedirectUrl.indexOf('?') == -1)?"?":"&") + Request.QueryString;
}
}
%>
<%
// *** Insert Record: construct a sql insert statement and execute it

if (String(Request("MM_insert")) != "undefined") {

// create the sql insert statement
var MM_tableValues = "", MM_dbValues = "";
for (var i=0; i+1 < MM_fields.length; i+=2) {
var formVal = MM_fields[i+1];
var MM_typesArray = MM_columns[i+1].split(",");
var delim = (MM_typesArray[0] != "none") ? MM_typesArray[0] : "";
var altVal = (MM_typesArray[1] != "none") ? MM_typesArray[1] : "";
var emptyVal = (MM_typesArray[2] != "none") ? MM_typesArray[2] : "";
if (formVal == "" || formVal == "undefined") {
formVal = emptyVal;
} else {
if (altVal != "") {
formVal = altVal;
} else if (delim == "'") { // escape quotes
formVal = "'" + formVal.replace(/'/g,"''") + "'";
} else {
formVal = delim + formVal + delim;
}
}
MM_tableValues += ((i != 0) ? "," : "") + MM_columns[i];
MM_dbValues += ((i != 0) ? "," : "") + formVal;
}
MM_editQuery = "insert into " + MM_editTable + " (" + MM_tableValues + ") values (" + MM_dbValues + ")";

if (!MM_abortEdit) {
// execute the insert
var MM_editCmd = Server.CreateObject('ADODB.Command');
MM_editCmd.ActiveConnection = MM_editConnection;
MM_editCmd.CommandText = MM_editQuery;
MM_editCmd.Execute();
MM_editCmd.ActiveConnection.Close();

if (MM_editRedirectUrl) {
Response.Redirect(MM_editRedirectUrl);
}
}

}

%>
<%
var Auditors = Server.CreateObject("ADODB.Recordset");
Auditors.ActiveConnection = MM_Quality_Audit_DB_STRING;
Auditors.Source = "SELECT * FROM Auditors ORDER BY Surname ASC";
Auditors.CursorType = 0;
Auditors.CursorLocation = 2;
Auditors.LockType = 1;
Auditors.Open();
var Auditors_numRows = 0;
%>
<%
var Engineers = Server.CreateObject("ADODB.Recordset");
Engineers.ActiveConnection = MM_Quality_Audit_DB_STRING;
Engineers.Source = "SELECT * FROM Engineers";
Engineers.CursorType = 0;
Engineers.CursorLocation = 2;
Engineers.LockType = 1;
Engineers.Open();
var Engineers_numRows = 0;
%>
<%
var Contract = Server.CreateObject("ADODB.Recordset");
Contract.ActiveConnection = MM_Quality_Audit_DB_STRING;
Contract.Source = "SELECT * FROM Contracts";
Contract.CursorType = 0;
Contract.CursorLocation = 2;
Contract.LockType = 1;
Contract.Open();
var Contract_numRows = 0;
%>
<%
var Appliance = Server.CreateObject("ADODB.Recordset");
Appliance.ActiveConnection = MM_Quality_Audit_DB_STRING;
Appliance.Source = "SELECT * FROM Appliances ORDER BY Make ASC";
Appliance.CursorType = 0;
Appliance.CursorLocation = 2;
Appliance.LockType = 1;
Appliance.Open();
var Appliance_numRows = 0;
%>
<%
var today= new Date()
var theyear=today.getYear()
var themonth=today.getMonth()+1
var theday=today.getDate()
%>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="Quality_StyleSheet.css" rel="stylesheet" type="text/css">

</head>

<body>
<form action="<%=MM_editAction%>" method="POST" name="form1">

miranda
06-15-2005, 04:52 PM
can you show the html for the form? if it is constructed dynamically then run the page, and then view the source code copy that and paste it here for us to see.

Also since this is Jscript that you are using on the server side it is case sensitive. So double check that.

PS I sure hope you are closing and disposing of all of them recordset objects when you are done with them. Also why do you have so many instances of the delimiters closing and then the next line opening? Granted it is a minor difference but it does slow down the execution of the page.
%>
<%

Shaft
06-15-2005, 06:22 PM
I haven't constructed that code myself it's just the crappy way that dreamweaver puts it together.

Anyway here's the html source code

I've had to attach it because it's too large to paste in.

Thanks for all your help so far I'm learning fast from my mistakes.

PS I do close my recordsets when i've finished and I'll be sure to tidy up my code.

miranda
06-15-2005, 07:19 PM
I don't see anything wrong there. The next thing I would do is write out the complete collection to the page and see if a value is passed to AuditID.
Response.Write("Request.Form = " + Request.Form)

nikkiH
06-15-2005, 07:42 PM
Oh, since you're using DW, you want Request.QueryString instead of Request.Form as it re-writes everything as URL params when it forwards like that.
I use DW for PHP stuff all the time.

miranda
06-15-2005, 09:31 PM
You could also use the Request object without the collection specified.
Request("AuditID")

nikkiH
06-15-2005, 09:55 PM
Ooh, I learned a new trick. :)

Shaft
06-16-2005, 01:37 PM
I thought with them last bits of advice I'd cracked it but it's still returning 'undefined' with these bits of code.

Response.Write(String(Request.QueryString("AuditID")))
Response.Write(String(Request("AuditID")))

any ideas anybody?

If I create a form that doesn't submit to a database I can use the request.form fine but with the server behaviour inplace I can't get anything.

it's driving me crazy i've been trying for 3 days now.

glenngv
06-16-2005, 02:16 PM
You could also use the Request object without the collection specified.
Request("AuditID")
But that is not efficient as the asp parser will look for the specified key in the different request collections Form, QueryString, Cookies, ServerVariables and ClientCertificate until the key is found. There is a fixed order of lookup but I forgot it. An unexpected value may be retrieved if the specified key can also be found in another collection that comes first in the lookup than the intended collection.


Going back to the problem...
Shaft, is the Request.Form("AuditID") statement in Audit_Form.asp?

Shaft
06-16-2005, 02:26 PM
I've tried it in both the destination and the originating page but i'm using request .querystring now as nikkiH said that DW rewrites the form parameters to url parameters.

Could it be that DW is clearing all the parameters after it has written the information to the database as it works if I don't insert a record into the database.

glenngv
06-16-2005, 03:02 PM
But your form method is POST so you should be able to get the submitted data in the destination page via Request.Form even if you have those querystrings.

Hmm...the problem could be in setting MM_editRedirectUrl variable. Does the page redirect to Confirm.asp and in that page you lost the Request.Form data? Since you redirect to that page, you need to use Request.QueryString and make sure all the needed querystring data is passed to the redirect url.

var MM_editRedirectUrl = "Confirm.asp";
...
// append the query string to the redirect URL
//since you came from POST method, you need to use Request.Form
if (MM_editRedirectUrl && Request.Form&& Request.Form.Count > 0) {
MM_editRedirectUrl += ((MM_editRedirectUrl.indexOf('?') == -1)?"?":"&") + Request.Form;
}

//debug
Response.Write("MM_editRedirectUrl:" + MM_editRedirectUrl + "<br />");
...

if (MM_editRedirectUrl) {
//debug
Response.End();

//Response.Redirect(MM_editRedirectUrl);
}

nikkiH
06-16-2005, 03:17 PM
I've tried it in both the destination and the originating page but i'm using request .querystring now as nikkiH said that DW rewrites the form parameters to url parameters.

Could it be that DW is clearing all the parameters after it has written the information to the database as it works if I don't insert a record into the database.

DW rewrites all params in to the querystring before the update is confirmed (if you set it to do that).
Then it updates the DB.
Then it forwards (redirects). This keeps a user refresh from accidentally updating the DB more than once, and is usually a desireable behavior.
Once it is on the new forwarded page, though, everything in the form and querystring is lost. You have to get it on the update page, before the forward happens.

Shaft
06-16-2005, 03:18 PM
Cracked it, it was the response.redirect that was messing everything up so if fixed it with this bit of code.

if (MM_editRedirectUrl) {
Session("AuditID") = String(Request.Form("AuditID"));
Response.Redirect(MM_editRedirectUrl);
}


it didn't seem to be writing the fields to querystrings though cos when i deleted the response.redirect and did Response.Write(Request.QueryString) I got nothing but when I did Response.Write(Request.Form) it returned all my fields.

I'm so happy I've got this sorted I think I would have ended up in looney bin if it wasn't for everybodys help especially glenngv.

Anyway you live and learn thank you everybody I can get on with some work now.

ghell
06-17-2005, 09:41 PM
i never use javascript to write asp i stick to vbs.. until today i thought it was just vbscript or jscript but apparently something i was reading tday said javascript and perlscript !?!? ...anyway.. in vbscript String(int, chr) is used to generate a string, not the same as CStr() but i duno about the language ur doing it in :o