SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Addict djh's Avatar
    Join Date
    Apr 2000
    Location
    Long Beach, CA
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm using CDONTS to email the values from a form to myself.

    I want to use one ASP file to take care of all the forms on my site, and so I need to do a For Each loop thing for all fields and field names.

    If my body text is set as "MyBody" how do I loop through all the fields so that it writes the fieldname and the field values in the "MyBody", so that I can send it?

    Thanks!

  2. #2
    Your Lord and Master, Foamy gold trophy Hierophant's Avatar
    Join Date
    Aug 1999
    Location
    Lancaster, Ca. USA
    Posts
    12,305
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Below is the Visual Basic code you will need. This will work for forms that are submitted via the POST method. For forms that are submitted via the GET method you need to substitute "Request.QueryString" for "Request.Form".

    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>
    &lt;%
    formtext = ""
    For each key in Request.Form
    formtext= formtext & key & ": " & Request.Form(key) & vbcrlf
    Next
    %&gt;
    [/code]

    ------------------
    Wayne Luke - Sitepoint Moderator/Internet Media Developer
    Digital Magician Studios - Making Magic with Web Applications
    wluke@digitalmagician.com


    [This message has been edited by wluke (edited June 23, 2000).]

  3. #3
    SitePoint Addict djh's Avatar
    Join Date
    Apr 2000
    Location
    Long Beach, CA
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wluke,

    The CDONTs on my host didn't work for some reason.

    So I'm using ASPMail. It sends an email but the body of the email is blank. I used your code exactly as is. Do I need to modify parts of it?

    Sorry, I'm just learning!

    P.S. Do I need to declare "key" in my form for the code to work? I haven't done so...

  4. #4
    Your Lord and Master, Foamy gold trophy Hierophant's Avatar
    Join Date
    Aug 1999
    Location
    Lancaster, Ca. USA
    Posts
    12,305
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You don't need to declare "key". Each form element in your HTML document becomes a "Key" while its data becomes a "value". Values are represented by "Request.Form(key)" which access the value out of an Array. The code above is standard Visual Basic script and has nothing to do with any component.

    Use this revised code. Basically the same I just changed the text string variable name
    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>
    &lt;%
    myBody = ""
    For each key in Request.Form
    myBody = myBody & key & ": " & Request.Form(key) & vbcrlfNext
    %&gt;
    [/code]

    You need to assign formtext to your body property. i.e. myMail.body = myBody

    Then it will be included when you call the Send property. If this is still unclear contact me by email and we'll talk some more, or if the code isn't too proprietary post it here so others can learn as well.

    ------------------
    Wayne Luke - Sitepoint Moderator/Internet Media Developer
    Digital Magician Studios - Making Magic with Web Applications
    wluke@digitalmagician.com


    [This message has been edited by wluke (edited June 28, 2000).]

  5. #5
    SitePoint Addict djh's Avatar
    Join Date
    Apr 2000
    Location
    Long Beach, CA
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the help. I am still getting a blank email message.... Here is the code to my asp page.

    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>
    &lt;%
    Dim FormText
    Dim Mailer
    %&gt;
    &lt;%Set Mailer = Server.CreateObject("SMTPsvg.Mailer")
    Mailer.FromName = "AUTOMAIL"
    Mailer.FromAddress = "r@g.com"
    Mailer.AddRecipient "ahi ahi", "a@b.com"
    Mailer.RemoteHost = "a.com"
    Mailer.Subject = Request.form("aha")
    Mailer.BodyText = FormText
    %&gt;
    &lt;%FormText = ""
    For each key in Request.Form
    FormText= FormText & key & ": " & Request.Form(key) & vbcrlf
    Next
    %&gt;
    &lt;%
    Mailer.SendMail
    %&gt;
    [/code]

    My form is pretty simple, with some javascript validation....

    Thanks!!!

    [This message has been edited by djh (edited June 28, 2000).]

    [This message has been edited by djh (edited June 28, 2000).]

  6. #6
    Your Lord and Master, Foamy gold trophy Hierophant's Avatar
    Join Date
    Aug 1999
    Location
    Lancaster, Ca. USA
    Posts
    12,305
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Change your code to the below....
    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>
    &lt;%
    Dim FormText
    Dim Mailer
    Set Mailer = Server.CreateObject("SMTPsvg.Mailer")
    Mailer.FromName = "AUTOMAIL" Mailer.FromAddress = "r@g.com" Mailer.AddRecipient "ahi ahi", "a@b.com"
    Mailer.RemoteHost = "a.com"
    Mailer.Subject = Request.form("aha")

    'Build email body here
    FormText = ""
    For each key in Request.Form
    FormText= FormText & key & ": " & Request.Form(key) & vbcrlf
    Next

    'Set email body here
    Mailer.BodyText = FormText
    Mailer.Sendmail
    %&gt;
    [/code]

    You were setting your bodytext parameter before you had anything in it. You need to build your formtext string before assigning it to the bodytext.

    ------------------
    Wayne Luke - Sitepoint Moderator/Internet Media Developer
    Digital Magician Studios - Making Magic with Web Applications
    wluke@digitalmagician.com

  7. #7
    Your Lord and Master, Foamy gold trophy Hierophant's Avatar
    Join Date
    Aug 1999
    Location
    Lancaster, Ca. USA
    Posts
    12,305
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    umm yeah.....

    My fault. I didn't include the "line continuation" symbol. Add an underscore after each "Or" so they would read
    "Or _"

    ------------------
    Wayne Luke - Sitepoint Moderator/Internet Media Developer
    Digital Magician Studios - Making Magic with Web Applications
    wluke@digitalmagician.com

    [This message has been edited by wluke (edited June 29, 2000).]

  8. #8
    SitePoint Addict djh's Avatar
    Join Date
    Apr 2000
    Location
    Long Beach, CA
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nope, I get an

    "Expected 'Then' error."

    by using

    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>
    If not (inStr(,Request.Form(key),"r_")) Or_not (inStr(,Request.Form(key),"e_")) Or_not inStr(,Request.Form(key),"i_"))
    Then
    [/code]

    Can one use an Or statement on more than 2 expressions?

    [This message has been edited by djh (edited June 29, 2000).]

  9. #9
    SitePoint Addict djh's Avatar
    Join Date
    Apr 2000
    Location
    Long Beach, CA
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you wluke!!!!

    That was pretty stupid of me... Fixed it and it works.

    One last question though. I have hidden "keys" that are on my form. They all have a r_,e_, or i_ in front of the field name, e.g., r_Name.

    What would the IF statemement look like if I wanted to send in the email every other "key" except these? Is there a way? There must be...

    I'm using the same

    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>
    &lt;%
    Dim FormText
    Dim Mailer
    Set Mailer = Server.CreateObject("SMTPsvg.Mailer")
    Mailer.FromName = "AUTOMAIL" Mailer.FromAddress = "r@g.com" Mailer.AddRecipient "ahi ahi", "a@b.com"
    Mailer.RemoteHost = "a.com"
    Mailer.Subject = Request.form("aha")

    'Build email body here
    FormText = ""
    For each key in Request.Form
    FormText= FormText & key & ": " & Request.Form(key) & vbcrlf
    Next

    'Set email body here
    Mailer.BodyText = FormText
    Mailer.Sendmail
    %&gt;
    [/code]

  10. #10
    SitePoint Addict djh's Avatar
    Join Date
    Apr 2000
    Location
    Long Beach, CA
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you wluke!!!!

    That was pretty stupid of me... Fixed it and it works.

    One last question though. I have hidden "keys" that are on my form. They all have a r_,e_, or i_ in front of the field name, e.g., r_Name.

    What would the IF statemement look like if I wanted to send in the email every other "key" except these? Is there a way? There must be...

    I'm using the same

    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>
    &lt;%
    Dim FormText
    Dim Mailer
    Set Mailer = Server.CreateObject("SMTPsvg.Mailer")
    Mailer.FromName = "AUTOMAIL" Mailer.FromAddress = "r@g.com" Mailer.AddRecipient "ahi ahi", "a@b.com"
    Mailer.RemoteHost = "a.com"
    Mailer.Subject = Request.form("aha")

    'Build email body here
    FormText = ""
    For each key in Request.Form
    FormText= FormText & key & ": " & Request.Form(key) & vbcrlf
    Next

    'Set email body here
    Mailer.BodyText = FormText
    Mailer.Sendmail
    %&gt;
    [/code]

  11. #11
    Your Lord and Master, Foamy gold trophy Hierophant's Avatar
    Join Date
    Aug 1999
    Location
    Lancaster, Ca. USA
    Posts
    12,305
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You would use the inStr function. So now your going to replace:
    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>
    FormText= FormText & key & ": " & Request.Form(key) & vbcrlf
    [/code]
    With:
    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>
    If Not (inStr(,Request.Form(key),"r_")) Or
    Not (inStr(,Request.Form(key),"i_")) Or
    Not (inStr(,Request.Form(key),"e_")) Then
    FormText = FormText & Key & ": " & Request.Form(key) & vbCrLf
    End If
    [/code]

    I would like to suggest a very good online reference that will be of immense help to you. Check out Dev Guru. They have online references for VbScript, ASP, ADO, Javascript, Access SQL, Style Sheets and Windows Scripting Host.

    ------------------
    Wayne Luke - Sitepoint Moderator/Internet Media Developer
    Digital Magician Studios - Making Magic with Web Applications
    wluke@digitalmagician.com

  12. #12
    SitePoint Addict djh's Avatar
    Join Date
    Apr 2000
    Location
    Long Beach, CA
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm getting a syntax error, pointing to the end of the below line, after the "Or"

    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>
    If Not (inStr(,Request.Form(key),"r_")) Or[/code]

    Any ideas why?

  13. #13
    Your Lord and Master, Foamy gold trophy Hierophant's Avatar
    Join Date
    Aug 1999
    Location
    Lancaster, Ca. USA
    Posts
    12,305
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Or_not is not a valid combination. VBScript thinks its a variable. If you have it all on one line it should be "Or Not" if you have it on multiple lines it should be "Or _" with spaces. Your also missing a parentheses before inStr(,Request.Form(key),"i_")). It should be (instr(,Request.Form(key),"i_"))


    ------------------
    Wayne Luke - Sitepoint Moderator/Internet Media Developer
    Digital Magician Studios - Making Magic with Web Applications
    wluke@digitalmagician.com

    [This message has been edited by wluke (edited June 29, 2000).]

  14. #14
    SitePoint Addict djh's Avatar
    Join Date
    Apr 2000
    Location
    Long Beach, CA
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi wluke,

    Thank you so very much for the help. Unfortunately, it didn't work.

    If I put a space in between "Or" and "_" as in:

    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>&lt;%
    Dim FormText
    Dim Mailer
    Set Mailer = Server.CreateObject("SMTPsvg.Mailer")
    Mailer.FromName = "AUTOMATIC WEBSITE MAIL"
    Mailer.FromAddress = "webmaster@goboatingamerica.com"
    Mailer.AddRecipient "Webmaster", "webmaster@goboatingamerica.com"
    Mailer.RemoteHost = "goboatingamerica.com"
    Mailer.Subject = Request.form("subject")
    FormText = ""
    For each key in Request.Form
    If Not (inStr(,Request.Form(key),"r_")) Or _
    Not (inStr(,Request.Form(key),"i_")) Or _
    Not (inStr(,Request.Form(key),"e_")) Then
    FormText = FormText & Key & ": " & Request.Form(key) & vbCrLf
    End If
    Next

    Mailer.BodyText = FormText
    Mailer.Sendmail
    %&gt;[/code]

    I get a "Type Mismatch" error.

    If I put "Or_" with no space, I get an "Expected Then" error.

    If I put it all on one line and put ...)) Or Not (inStr(,...........)), I once again get a "Type Mismatch" error.

    I'm just at a loss...

    Learned quite a bit though...

  15. #15
    Your Lord and Master, Foamy gold trophy Hierophant's Avatar
    Join Date
    Aug 1999
    Location
    Lancaster, Ca. USA
    Posts
    12,305
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ok, getting rid of a type mismatch error is pretty easy as you can just force it to the type of variable you want.

    Here is one more version of your code.
    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>
    &lt;%
    Option Explicit
    Dim FormText
    Dim Mailer

    Set Mailer = Server.CreateObject("SMTPsvg.Mailer")
    Mailer.FromName = "AUTOMATIC WEBSITE MAIL"
    Mailer.FromAddress = "webmaster@goboatingamerica.com"
    Mailer.AddRecipient "Webmaster", "webmaster@goboatingamerica.com"
    Mailer.RemoteHost = "goboatingamerica.com"
    Mailer.Subject = Request.form("subject")
    FormText = ""
    For each key in Request.Form
    If Not (inStr(,CStr(Request.Form(key)),"r_")) Or _
    Not (inStr(,CStr(Request.Form(key)),"i_")) Or _
    Not (inStr(,CStr(Request.Form(key)),"e_")) Then
    FormText = FormText & Key & ": " & Request.Form(key) & vbCrLf
    End If
    Next

    Mailer.BodyText = FormText
    Mailer.Sendmail
    %&gt;
    [/code]

    If you look at the IF statement, You will notice that it has changed again. It now includes the CSTR function. This function takes the value supplied as its argument and forces it to be a string. There are similar functions for converting variables to the Integer, Date, Long, Double, Currency, Boolean, Byte, and Single variable sub-types. This is besides the fact that all variables are really the same data type of Variant.

    The other new line of code, that you should see is "Option Explicit". This tells VB that you want it to check your variables for you. This will prevent declaring new variables because of misspellings.

    Sorry to make you beta test this, but I haven't had time to actually test it myself.

    ------------------
    Wayne Luke - Sitepoint Moderator/Internet Media Developer
    Digital Magician Studios - Making Magic with Web Applications
    wluke@digitalmagician.com

    [This message has been edited by wluke (edited June 29, 2000).]

  16. #16
    SitePoint Addict djh's Avatar
    Join Date
    Apr 2000
    Location
    Long Beach, CA
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Close...

    Getting an error:

    Variable is undefined: 'Key'

    Help!

  17. #17
    SitePoint Addict djh's Avatar
    Join Date
    Apr 2000
    Location
    Long Beach, CA
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I'm just going to go about this manually and write out the actual lines, instead of having it do a "For Each" loop.

    That'll give me better control over how the email looks anyway...

    Very interesting....

    Thanks wluke, don't know what you do exactly but you sure know what you're doing. Thanks again!

  18. #18
    Your Lord and Master, Foamy gold trophy Hierophant's Avatar
    Join Date
    Aug 1999
    Location
    Lancaster, Ca. USA
    Posts
    12,305
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Just add:

    DIM KEY



    ------------------
    Wayne Luke - Sitepoint Forums Administrator
    Digital Magician Studios - Making Magic with Web Applications
    wluke@digitalmagician.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •