Link to home
Start Free TrialLog in
Avatar of James Coats
James CoatsFlag for United States of America

asked on

Session Variable won't stay in page that it is passed to after pressing submit

The code below belongs to a form with which I want to pass a session variable with to the next page. The session variable gets passed but once the second page does a submit to the database the driver name “txtDriverName” disappears from the form and I want it to stay in that form until that user logs out. I could swear that yesterday it did do this. Now it doesn’t appear to be doing so,any suggestions??




</head>

<body>
<form method="POST" action="insertPDA.asp" name="form1">
  <table align="left">
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"><strong>Name:</strong></div></td>
      <td>
        <input name="txtDriverName" type="text" id="txtDriverName" value="" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"><strong>Shift:</strong></div></td>
      <td>
<select name="shift">
  <%
While (NOT rs_shift.EOF)
%>
  <option value="<%=(rs_shift.Fields.Item("shift_letter").Value)%>"><%=(rs_shift.Fields.Item("shift_letter").Value)%></option>
  <%
  rs_shift.MoveNext()
Wend
If (rs_shift.CursorType > 0) Then
  rs_shift.MoveFirst
Else
  rs_shift.Requery
End If
%>
        </select>        
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"><strong>Date:</strong></div></td>
      <td>
        <input type="text" name="start_time" value="<%response.write( now())%>" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"></div></td>
      <td>
        <input type="submit" value="Insert record">
      </td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1">
</form>



This is the relevant section of the second page:

Header Section:




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%session("sessDriverName")=Request.Form("txtDriverName")%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>



Relevant section of form:




      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"><strong>Driver:</strong></div></td>
      <td>
        <input name="raw_matDriver" type="text" value="<%= Session("sessDriverName") %>" readonly="" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"></div></td>
      <td>
        <input type="submit" value="Insert record" name="Insert" onClick="return confirm('Are you sure you would like to insert this record?')">
      </td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1">
</form>

Avatar of Christopher Kile
Christopher Kile
Flag of United States of America image

In your first page, have you tried this?

<input name="txtDriverName" type="text" id="txtDriverName" value="<%= Session("sessDriverName") %>"  size="32">

This insures that the field is always initialized from the Session value.  If you have trouble with an uninitialized session variable, add

<% If Session("sessDriverName") Is Nothing Then Session("sessDriverName") = "" %>

just beneath the <body> tag.
Avatar of James Coats

ASKER

I have tried the above but it seems to have no effect. Thanks for the suggestion.
I guess I'll up the points a little on this question. I spent most of the day trying to get a handle on this one.
Can you show me where you put these lines in?  Are you sure conditional code isn't bypassing them?  Because if this doesn't work, something is either overwriting your field value or your Session, and my guess would be the field value.  However, it's not impossible that your Session is expiring values too quickly.  
Here is the page that I want to pass the Session Variable:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<% If Session("sessDriverName") Is Nothing Then Session("sessDriverName") = "" %>

<form name="form1" method="POST" action="<%=MM_editAction%>">
  <table width="200" border="1">
    <tr>
      <td><label>Name</label>&nbsp;</td>
      <td><input name="txtDriverName" type="text" id="txtDriverName" value="<%session("sessDriverName")%>" size="32"></td>
    </tr>
    <tr>
      <td><label>Date</label>&nbsp;</td>
      <td><input type="text" name="date" id="txt_date" value="<%response.write( now())%>" size="32"></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="Submit" value="Submit"></td>
    </tr>
  </table>

    <input type="hidden" name="MM_insert" value="form1">
</form>
</body>
</html>
<%
rs_user2.Close()
Set rs_user2 = Nothing
%>
This is the page I need to hold the Session variable. When I had it working it would hold the variable for one submission to the database which worked fine. However the redirect is back to this same form and the variable would not be present. I need that name to stay the same as long as the user is logged in. Also on the time field I need the time to occurr with an onfocus event. As it is now it enters the time when the page loads. And I need it to enter when the user taps the field. Here's the code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script>
function timeStamp(raw_matDateTime){
  if(raw_matDateTime.value==""){
    raw_matDateTime.value = ((new Date()).toString().split(" ")[3])+" "
  }
}
</script>

</head>

<body>

<div align="left"></div>

<form method="post" action="<%=MM_editAction%>" name="form1">
  <table align="left">
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"><strong>BCODE:</strong></div></td>
      <td>
        <input type="text" name="raw_matBCODE" value="" readonly="" size="32" >
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"><strong>Date:</strong></div></td>
      <td>
        <input type="text" name="raw_matDateTime" onFocus="timeStamp(this)"value="" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"><strong>PO num:</strong></div></td>
      <td>
        <input type="text" name="raw_matPO_num" value="" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"><strong>Locat:</strong></div></td>
      <td>
        <input type="text" name="raw_matLocation" value="" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"><strong>Supp:</strong></div></td>
      <td>
<select name="raw_matSupplier">
  <%
While (NOT rs_suppliers.EOF)
%>
  <option value="<%=(rs_suppliers.Fields.Item("supplier_name").Value)%>"><%=(rs_suppliers.Fields.Item("supplier_name").Value)%></option>
  <%
  rs_suppliers.MoveNext()
Wend
If (rs_suppliers.CursorType > 0) Then
  rs_suppliers.MoveFirst
Else
  rs_suppliers.Requery
End If
%>
        </select>        
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"><strong>Grade:</strong></div></td>
      <td>
<select name="raw_matGrade">
  <%
While (NOT rs_grades.EOF)
%>
  <option value="<%=(rs_grades.Fields.Item("grade").Value)%>"><%=(rs_grades.Fields.Item("grade").Value)%></option>
  <%
  rs_grades.MoveNext()
Wend
If (rs_grades.CursorType > 0) Then
  rs_grades.MoveFirst
Else
  rs_grades.Requery
End If
%>
        </select>        
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"><strong>Thick:</strong></div></td>
      <td>
<select name="raw_matThickness">
  <%
While (NOT rs_thickness.EOF)
%>
  <option value="<%=(rs_thickness.Fields.Item("thickness").Value)%>"><%=(rs_thickness.Fields.Item("thickness").Value)%></option>
  <%
  rs_thickness.MoveNext()
Wend
If (rs_thickness.CursorType > 0) Then
  rs_thickness.MoveFirst
Else
  rs_thickness.Requery
End If
%>
        </select>        
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"><strong>Quant:</strong></div></td>
      <td>
        <input type="text" name="raw_matQuantity" value="" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"><strong>Status:</strong></div></td>
      <td>
<select name="raw_matStatus">
  <%
While (NOT rs_status.EOF)
%>
  <option value="<%=(rs_status.Fields.Item("status").Value)%>"><%=(rs_status.Fields.Item("status").Value)%></option>
  <%
  rs_status.MoveNext()
Wend
If (rs_status.CursorType > 0) Then
  rs_status.MoveFirst
Else
  rs_status.Requery
End If
%>
        </select>        
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"><strong>Driver:</strong></div></td>
      <td>
        <input name="raw_matDriver" type="text" value="<%= Session("sessDriverName") %>" readonly="" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"></div></td>
      <td>
        <input type="submit" value="Insert record" name="Insert" onClick="return confirm('Are you sure you would like to insert this record?')">
      </td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1">
</form>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</body>
</html>
<%
rs_rawMaterials.Close()
Set rs_rawMaterials = Nothing
%>
<%
rs_suppliers.Close()
Set rs_suppliers = Nothing
%>
<%
rs_grades.Close()
Set rs_grades = Nothing
%>
<%
rs_thickness.Close()
Set rs_thickness = Nothing
%>
<%
rs_status.Close()
Set rs_status = Nothing
%>
<%
rs_users.Close()
Set rs_users = Nothing
%>
<%
rs_drivers.Close()
Set rs_drivers = Nothing
%>
OK, in the second document (the one that the first form posts to) immediately after the body tag you should have the following:


<% Session("sessDriverName") = Request.Form("txtDriverName") %>

This actually stores the field value to the Session, where it will be available throughout your session.  This code should only be in place in the form that implements the form post action of the form where txtDriverName is input.

Now, this code will only update the time if raw_matDateTime.value is blank - thus, after the first call, unless something else has made  raw_matDateTime.value blank in the meantime, the assignment line will only be executed once

function timeStamp(raw_matDateTime){
   if(raw_matDateTime.value==""){
    raw_matDateTime.value = ((new Date()).toString().split(" ")[3])+" "
  }
}

So, why not try

function timeStamp(raw_matDateTime){
    raw_matDateTime.value = ((new Date()).toString().split(" ")[3])+" ";
}
I will give this a try on Monday. Yesterday, Friday I found out that the companies copy of Dreamweaver was version 7.0 which had a bug. I will be upgrading it to version 7.0.1 which fixes the bug. I had found some odd code errors which I had to correct by hand. Maybe this was most of my problem in the first place. At any rate there have been some interesting soloutions given. I will still give out the points to the soloution I use. Check late on Monday for updates.
I gone back to the begining because I can't even get what I had working to work again. This is the page I want to pass the Session Variable to the second page:



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<form method="post" action="<%=MM_editAction%>" name="form1">
  <table align="left" width=200 border="1">
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"><strong>Name:</strong></div></td>
            <input name="txtDriverName" type="text" id="txtDriverName" value=""  size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"><strong>Start Time:</strong></div></td>
      <td><input type="text" name="start_time" value="" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"></div></td>
      <td><input type="submit" value="Insert record">
      </td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1">
</form>
<p>&nbsp;</p>
</body>
</html>
This is the second page that I want the session variable(the value of which s/b: "txtDriverName") to place the value in the "driver" field of this form on this second page. My problem the first time was that the variable was in fact passed and went to the correct form field but after the form was submitted to the database the session variable went with it. I need it to stay in the driver field until the user ends his session which will normally be 12hrs. They will only be able to use this form during their shift.

Here's the code:



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<form method="POST" action="<%=MM_editAction%>" name="form1">
  <table align="left">
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"><strong>BCODE:</strong></div></td>
      <td><input type="text" name="raw_matBCODE" value="" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"><strong>Date:</strong></div></td>
      <td><input type="text" name="raw_matDateTime" value="" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"><strong>Num:</strong></div></td>
      <td><input type="text" name="raw_matPO_num" value="" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"><strong>Locat:</strong></div></td>
      <td><input type="text" name="raw_matLocation" value="" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"><strong>Supp:</strong></div></td>
      <td><input type="text" name="raw_matSupplier" value="" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"><strong>Grade:</strong></div></td>
      <td><input type="text" name="raw_matGrade" value="" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"><strong>Thick:</strong></div></td>
      <td><input type="text" name="raw_matThickness" value="" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"><strong>Quant:</strong></div></td>
      <td><input type="text" name="raw_matQuantity" value="" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"><strong>Status:</strong></div></td>
      <td><input type="text" name="raw_matStatus" value="" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"><strong>Driver:</strong></div></td>
      <td><input type="text" name="raw_matDriver" value="" size="32">
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left"></div></td>
      <td><input type="submit" value="Insert record">
      </td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1">
</form>
<p>&nbsp;</p>
</body>
</html>


Your assistance will be much welcomed. I feel like a moron. I don't need this @#!!* stuff just now. Thank you for any help you can provide.
*blech* been there done that, my sympathies
and *blech* I suspect now that the answer will be....cookies.

I dislike cookies for many applications because they proliferate like insects in the average user's file system in one of those never-to-be-accessed-except-by-Gurus areas where your personal information is stored.  However, this app is tailor-made for cookies, and I'm sorry I didn't go there first.

Cookies are not dependent on sessions, a good thing since I suspect your sessions are lasting nowhere near 12 hours.  Cookies can be given an expiration date-time and, when accessed, that expiration will exclude the cookie from access and, I believe, schedule it for deletion.

Cookies also have the capability of being read directly by both client and server.

The following functions allow you to set/create a cookie, get an existing, in-force cookie, and delete a cookie (note that it does so by changing the expiration date):

function SetCookie(sName, sValue, expirationDate)
{
      date = new Date(expirationDate);    // suggestion:  YYYY/MM/DD hh:mm:ss
      document.cookie = sName + "=" + escape(sValue) + "; expires=" + date.toGMTString();
}

function GetCookie(sName)
{
      // cookies are separated by semicolons
      var aCookie = document.cookie.split("; ");

      for (var i=0; i < aCookie.length; i++)
      {
            //      a name/value pair (a crumb) is separated by an equal sign
            var aCrumb = aCookie[i].split("=");
            if (sName == aCrumb[0])
                  return unescape(aCrumb[1]);
      }

      // a cookie with the requested name does not exist
      return null;
}

function DelCookie(sName)
{
      var date = new Date();
      document.cookie = sName + "=0; expires=" + date.toGMTString();
}

With these functions, save a cookie with the value of start_time, then, in the page that needs it, get the cookie and read the value.  I recommend saving them to a Javascript file (.js) and including them as needed, they come in handy.  In my code below, I include a line to declare a script called cookie_functions.js in the default application directory in my code below.

Then, in your first file, change this

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

<body>
<title>Untitled Document</title>
<script src="cookie_functions.js" language="javascript" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
<!--
function form1_Post() {
    var id = "txtDriverName";
    var el = document.GetElementById(id);

    if (el) {        // this won't be null unless we're using the wrong field id
        var expDate = new Date();
       
        SaveCookie(id, el.value, expDate.setHours(expDate.getHours() + 12));
    }
    window.location = "<%=MM_editAction%>";  //  assumes that this server property supplies a URL.
}
-->
</script>
<form method="POST" onsubmit="form1_Post()" name="form1">
 
In your second file, you change

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

<body>
<script src="cookie_functions.js" language="javascript" type="text/javascript"></script>
<form method="POST" action="<%=MM_editAction%>" name="form1">

and right before the </form> tag add

<script language="javascript" type="text/javascript">
<!--
    var el = document.getElementByID("raw_matDriver");

    if (el) {
        el.value = GetCookie("txtDriverName");
    }
-->
</script>

This last bit sets the field you need set (if I understand your previous code) with the name captured from the first screen.  This name will be available for 12 hours after you leave the first screen.  I'll leave it to you to tune the expiration date to your exact needs.  NOTE:  first-party cookies must be enabled on the user's web browser in order to use this feature.
ASKER CERTIFIED SOLUTION
Avatar of Christopher Kile
Christopher Kile
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial