Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Other Programming > VBScript
|
VBScript For questions and discussions related to VBScript.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VBScript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 30th, 2008, 10:33 AM
Registered User
 
Join Date: Apr 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Response.write if statements in a form?

Hope this is the correct thread to post in.
I am a student and currently studying sql, vbscript and web based databases etc.
However i am pretty much stuck on the following piece of script.

<%
    Set MyConn=Server.CreateObject("ADODB.Connection")
    MyConn.Open "pqrtrav"
    Set Rs=MyConn.Execute("SELECT*From[trip]Order by activity")
    Previousactivity="None"
    WHILE NOT Rs.EOF

    CurrentActivity=Rs("Activity")

    if (CurrentActivity<>PreviousActivity) then
%>
        <%Response.Write(Rs("Activity")) %> <input name="<%Response.Write(Rs("Activity")) %>" type="checkbox" value="<% Response.Write(Rs("Activity")) %>" >
<%
       end if
       PreviousActivity=CurrentActivity

       Rs.MoveNext

       WEND
       Rs.Close

       MyConn.Close
%>

I have opened the database and produced a list of 'activities' taken from the database with a checkbox along side it. However, when I submit the form, the information is not displayed on the next page when boxes are checked. How would it be possible to display this information on the submit page after a box has been checked??

Please help,
kind regards Natalie
Reply With Quote
  #2 (permalink)  
Old April 30th, 2008, 10:43 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

You would have to do a Request on your second page something like:

Response.Write Request.Form("<formfieldName>")

hth.
-Doug

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
Reply With Quote
  #3 (permalink)  
Old April 30th, 2008, 11:03 AM
Registered User
 
Join Date: Apr 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

But what would the form field name be? As I am picking the data out of a database, if i were to specify one form field name, it would only display that one activity if it were checked. Would i need to include another if statement on the first page to make sure the checkboxes that are checked appear?
regards, natalie
Reply With Quote
  #4 (permalink)  
Old April 30th, 2008, 11:08 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

On your second page you can loop the entire Forms collection:

For x = 1 to Request.Form.Count
    'This is the form control name
    Response.Write Request.Form.Key(x) & " = "
    'This is the form control value
    Response.Write Request.Form.Item(x) & "<br>"
Next

In the case of a checkbox, if the checkbox is not ticked it will return an empty string otherwise it will return the value.

hth.
-Doug

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
Reply With Quote
  #5 (permalink)  
Old April 30th, 2008, 11:21 AM
Registered User
 
Join Date: Apr 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm really sorry, but I haven't been doing it for that long and dont understand where abouts and how to assign that to my coding :S What does the 'x' bit mean and would i have to replace anything to make it revelant to my code?
regards, natalie
Reply With Quote
  #6 (permalink)  
Old April 30th, 2008, 01:00 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

x is a counting variable so as the For loop executes x will be incremented by 1. So what you are saying in the code is:

For x = 1 to Request.Form.Count = Until the value of x is equal to the number of items in Request.Form preform this action:
Response.Write Request.Form.Key(x) = Return the key value from x place inside the collection
Response.Write Request.Form.Item(x) = Return the item (which is the form value) from x place inside the collection
Next = Cause the code to loop

If you place the code snipped i provided on your Submit page you will see how it works. (It will write out all of the form element names and values from the previous page)

Does that make more sense?
-Doug

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
Reply With Quote
  #7 (permalink)  
Old April 30th, 2008, 01:23 PM
Registered User
 
Join Date: Apr 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That does make more sense, thankyou. However, before I try this, what do i need to put in between the speech marks instead of formfield name on the second page?? As i cannot specify a particular 'activity'.

Response.Write Request.Form("formfieldName")

Or do I not need to use this anymore??

Regards, Natalie
Reply With Quote
  #8 (permalink)  
Old April 30th, 2008, 02:46 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

This is where it gets a bit more tricky. If you simply put this code on your page:

For x = 1 to Request.Form.Count
    'This is the form control name
    Response.Write Request.Form.Key(x) & " = "
    'This is the form control value
    Response.Write Request.Form.Item(x) & "<br>"
Next

and run it you will see a print out to the screen something like

Checkbox1 = <value>
Checkbox2 =
Checkbox3 = <value>
etc

You should try this out anyway just to see how the code works. In so far as getting specific values, that is where it gets complex. Since you are naming the activity checkboxes dynamically based on data from the database you will need to use some form of loop. If you appened cbAct_, for example, to each of your checkboxes (so that when the checkbox was rendered the name would be cbAct_MyActivity) you could do a bit of string checking:

For x = 1 to Request.Form.Count
    If InStr(Request.Form.Key(x), 'cbAct_') > 0 Then
        Response.Write Request.Form.Key(x) & " = "
        'This is the form control value
        Response.Write Request.Form.Item(x) & "<br>"
    End If
Next
This code looks for the exsitance of the characters cbAct_ at the beginning of each of the form field names, if it is found you can assume that it is your activity and write the data out ot the screen, if the characters aren't found you will do nothing.

Finally, you could also do something like this:

    Set MyConn=Server.CreateObject("ADODB.Connection")
    MyConn.Open "pqrtrav"
    Set Rs=MyConn.Execute("SELECT*From[trip]Order by activity")
    Previousactivity="None"
    WHILE NOT Rs.EOF
     Response.Write Request.Form(rs("Activity"))
    Wend
I think that a second call to the database here is a bit of overkill but either method will work.

hth.
-Doug


================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
response.write crmpicco Classic ASP Basics 1 January 27th, 2005 06:31 PM
response.write netcrawler Classic ASP Databases 4 September 29th, 2004 10:13 AM
Response.write Sach Classic ASP Components 5 March 19th, 2004 06:40 AM
Response.Write an SSI hcweb Classic ASP Basics 2 November 2nd, 2003 05:21 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.