Capture Value of Menu Label on ASP Page using VBScript

I have an asp page that I have built with Dreamweaver that updates a record in a database and simultaneously emails me with the results of the form.  However, one of the form fields is a Select Menu, so in the email I'm getting the value (which is a number).  Is there a way to capture the label for the Select Menu and assign it to a hidden field so that it is passed with the other form results, and then I can get a more useful value sent in the email?
ccbaileyAsked:
Who is Participating?
 
worthyking1Commented:
The easiest way to do this is put both the book ID and Title into the option value, then split them up afterwards to be used in both your DB update and email. You cannot use a hidden field because VBScript (unlike Java) is run on the server side not client side, so your hidden field hdn_BookTitle is currently only being populated with the title from the first record when the page loads and does not change when the user selects a different item from the select list.

So, here's how you do it:

1. Change line #8 of your form code above to this:

<option value="<%=rsEmployeeBooksNotRead("Book_ID")%>|<%=rsEmployeeBooksNotRead("Title")%>"><%=rsEmployeeBooksNotRead("Title")%></option>

[You don't need all the extra "fields.item" crap that Dreamweaver stuffs in there]. This will give you a pipe-delimited value containing both the book ID and title.


2. Once the form is submitted, split your value into two usable variables to hold the book ID and the Title, as follows (do this BEFORE you start your DB update or mail function):

arrBookLink = Split(request.Form("Book_Link"),"|")
BookID = CInt(arrBookLink(0))
BookTitle = Trim(arrBookLink(1))


3. Change line #50 of your jmail code above to now use the new BookTitle var, like so:

JMail.Body = Jmail.body & "Title: " & BookTitle & vbcrlf

If you want you could also include the ID in the email (if you need it) by adding another line like so:

JMail.Body = Jmail.body & "Book ID: " & BookID & vbcrlf


4. IMPORTANT - Change your database update code to use the BookID variable instead of the form value that you're currently using (you didn't post the db update code but basically what you'll need to do is replace Request.Form("Book_Link") with BookID in the update query).  If you don't do this your update query will bomb because the Book_Link form value now contains a string (both the id and title) instead of just an integer.

Hope that helps :)
0
 
LZ1Commented:
Can you post the form code as well as the email code?
0
 
ccbaileyAuthor Commented:
Sure!  See below:

Form Code:
 
<form name="form1" method="POST" action="<%=MM_editAction%>" >
            <table align="center">
              <tr valign="baseline">
                <td width="213" align="right" nowrap class="style1">Book Title</td>
                <td width="304"><select name="Book_Link">
                  <%
While (NOT rsEmployeeBooksNotRead.EOF)
%>
                  <option value="<%=(rsEmployeeBooksNotRead.Fields.Item("Book_ID").Value)%>"><%=(rsEmployeeBooksNotRead.Fields.Item("Title").Value)%></option>
                  <%
  rsEmployeeBooksNotRead.MoveNext()
Wend
If (rsEmployeeBooksNotRead.CursorType > 0) Then
  rsEmployeeBooksNotRead.MoveFirst
Else
  rsEmployeeBooksNotRead.Requery
End If
%>
                </select></td>
              </tr>
              <tr valign="baseline">
                <td align="right" nowrap class="style1">Certify this is Read with Initials:</td>
                <td><input type="text" name="Initial_Certification" value="" size="32"></td>
              </tr>
              <tr valign="baseline">
                <td align="right" nowrap class="style1">Date Completed</td>
                <td><input type="text" name="Date_Completed" value="" size="32"></td>
              </tr>
              <tr valign="baseline">
                <td colspan="2" align="center" nowrap><input type="submit" value="Add Book"></td>
                </tr>
            </table>
            <input type="hidden" name="Employee_Link" value="<%= Session("varEmpID") %>" >
            <input type="hidden" name="MM_insert" value="form1">
            <input name="hdn_UserName" type="hidden" id="hdn_UserName" value="<%= Session("varUserName") %>">
            <input name="hdn_Payment" type="hidden" id="hdn_Payment" value="<%=(rsEmployeeBooksNotRead.Fields.Item("Pay_Amount").Value)%>">
            <input type="hidden" name="hdn_BookTitle" id="hdn_BookTitle">
          </form>

Open in new window


Here's the jmail code:
 
<html>
<head>
<title>Confirmation </title>
<body>

<%


'First, initialize the JMail program...
Set JMail = Server.CreateObject("JMail.SMTPMail")

'Next, define your SMTP server & port address...
'(note that you just need to use the following line

JMail.ServerAddress = "localhost"
JMail.ISOEncodeHeaders = False

'The JMail Sender property is what you set to let your recipient
'know who the e-mail is from, for example...

JMail.Sender = "christybailey@otdsolutions.com"

'The Subject property allows you to fill in the subject of the
'e-mail message you're sending...

JMail.Subject = "Book Submitted"

'To add recipients to the e-mail message, you use the AddRecipient
'method. Note that it does not require the use of an = sign!

JMail.AddRecipient "christybailey@otdsolutions.com"

'You can keep adding recipients until you're done...
'JMail.AddRecipient "joe@blow.com"
'JMail.AddRecipient "shag@austinbaby.com"

' Get the recipients mailbox from a form (note the lack of an equal sign).
'JMail.AddRecipient Request.Form("Email")
'You can also add recipients to what would normally be the CC:
'and BCC: fields of your e-mail, as follows (respectively)...
'JMail.AddRecipientCC "boss@job.com"
'JMail.AddRecipientBCC "secret@squirrel.com"
'If you want to make the To: address more personal, you can
'use the AddRecipientEx method to add not only the person's
'e-mail address, but their name, too...
'JMail.AddRecipientEx "lame@address.com", "Sparky O'Malley"
'The body of your message can be set in a number of ways. To
'set the body explicitly, you can just do something like this...

JMail.Body = Jmail.body & "Title: " & Request.Form("hdn_BookTitle") & vbcrlf
JMail.Body = Jmail.body & "Completion Date: " & Request.Form("Date_Completed") & vbcrlf
JMail.Body = Jmail.body & "Certification: " & Request.Form("Initial_Certification") & vbcrlf
JMail.Body = Jmail.body & "Emp ID:   " & Request.Form("Employee_Link") & vbcrlf
JMail.Body = Jmail.body & "Employee:   " & Request.Form("hdn_UserName") & vbcrlf
JMail.Body = Jmail.body & "Payment Amount:   " & Request.Form("hdn_Payment") & vbcrlf




'JMail.Body = "This is a test from jmail.asp."
'Or you might need to add text later after a generic greeting,
'in which case you'd append text to the body like this...
'JMail.Body = JMail.Body & "    Have a nice day!"
'You can also append text with the AppendText method...again,
'note the lack of an = sign here because you're using a method,
'not setting a property
'JMail.AppendText "Have a nice day!"
'If you have some text stored in a file, you can also use the
'contents of that file to set the body of the message, as
'follows...

'JMail.AppendBodyFromFile "c:\webserver\CHANGEMYDOMAIN\htdocs\mytext.txt"
'If your message is really important, you may want to set the
'message priority with the Priority property. When setting this
'property, remember that 1 is highest priority (urgent) and
'5 is lowest priority...

'JMail.Priority = 1

'You can even add attachments to your message. As with the
'AddRecipient method, this does not use = signs and can be called
'as many times as you have attachments...
'JMail.AddAttachment "c:\webserver\CHANGEMYDOMAIN\htdocs\pix\myphoto.jpg"
'JMail.AddAttachment "c:\webserver\CHANGEMYDOMAIN\htdocs\faq\faq1.txt"
'Once you've set everything up, it's just a matter of sending the
'message...

'IF NOT JMail.execute THEN
'	Response.Write( "ERROR MESSAGE: " & JMail.ErrorMessage & "<BR>" & vbcrlf );
'	Response.Write( "ERROR SOURCE: " & JMail.ErrorSource & "<BR>" & vbcrlf );
'	Response.Write( "LOG: " & JMail.Log & "<BR>" & vbcrlf );
'ELSE
'		Response.Write( "Email sent successfully!" )
'END IF
JMail.Execute

%>

<center>

An e-mail has been sent to your mailbox christybailey@otdsolutions.com

</center>
</body>
</html>

Open in new window

0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
LZ1Commented:
Do you have a rendered HTML version of the form?
0
 
ccbaileyAuthor Commented:
OK, I am not sure I know what you're asking.  Do you mean the HTML after the page has run?  Take a look and see if this is what you need.

 
<form name="form1" method="POST" action="/OTDS/Intranet/BookReading/employee_book_update.asp" >
            <table align="center">
              <tr valign="baseline">
                <td width="213" align="right" nowrap class="style1">Book Title</td>
                <td width="304"><select name="Book_Link">
                  
                  <option value="14">Boundaries</option>
                  
                  <option value="3">Guerrilla Marketing</option>
                  
                  <option value="4">Guerrilla Marketing for Consultants</option>
                  
                  <option value="5">Permission Marketing</option>
                  
                  <option value="18">The 7 Habits of Highly Effective People</option>
                  
                  <option value="19">The Dip</option>
                  
                </select></td>
              </tr>
              <tr valign="baseline">
                <td align="right" nowrap class="style1">Certify this is Read with Initials:</td>
                <td><input type="text" name="Initial_Certification" value="" size="32"></td>
              </tr>
              <tr valign="baseline">
                <td align="right" nowrap class="style1">Date Completed</td>
                <td><input type="text" name="Date_Completed" value="" size="32"></td>
              </tr>
              <tr valign="baseline">
                <td colspan="2" align="center" nowrap><input type="submit" value="Add Book"></td>
                </tr>
            </table>
            <input type="hidden" name="Employee_Link" value="" >
            <input type="hidden" name="MM_insert" value="form1">
            <input name="hdn_UserName" type="hidden" id="hdn_UserName" value="">
            <input name="hdn_Payment" type="hidden" id="hdn_Payment" value="30">
            <input type="hidden" name="hdn_BookTitle" id="hdn_BookTitle">
          </form>

Open in new window


Thanks for your help!

Christy
0
 
LZ1Commented:
You have a couple of options I believe. The easiest from what I can see would be to add the Title to the value of the option field.

Like so:

<option value="<%=(rsEmployeeBooksNotRead.Fields.Item("Book_ID").Value)%><%=(rsEmployeeBooksNotRead.Fields.Item("Title").Value)%>"><%=(rsEmployeeBooksNotRead.Fields.Item("Title").Value)%></option>

Open in new window

0
 
ccbaileyAuthor Commented:
I'm not sure I follow you.  Where in my code do I put this?  I tried putting it in my form, and it did include the text of the title in the bottom of the form.  But it didn't change when I selected a different book, and I don't see how to include it in my email.  I am sure I'm missing something - can you explain a little further?

Thanks!
0
 
LZ1Commented:
In the ASP form above that you posted, that would replace line 9. Basically we're putting the option number right next to the title inside of the value field for the drop down.
0
 
ccbaileyAuthor Commented:
LZ1 & worthyking1 - I just wanted to let you know that I am not ignoring you - my site is having a server-side error on my pages that run the asp code which is preventing me from testing this.  I am in contact with my internet service provider and hopefully will be back up an running soon.  I will get back to this question - thanks for you help!
0
 
ccbaileyAuthor Commented:
OK, things are actually up and running - I do have a server issue on another page, but it wasn't causing my problem here.

OK, LZ1 - this solution would be great if I didn't need to insert my value of the option field into my database, but when you add the Title to the option field, the database insert portion of the code bombs.  I do need the values split up so I can send one to the database and the other to the email.  

worthking1 - This is a great idea!  I had to use session variables for BookID and BookTitle, because my jmail is in a separate file, it was losing the value of the variables.  Once I did this, the values were passed to the jmail page and everything worked beautifully.  Thanks a bunch!  And thanks, LZ1, for your ideas and collaboration!
 
0
 
ccbaileyAuthor Commented:
Other than having to change the variables to session variables, it worked beautifully.  Thanks for the help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.