PDA

View Full Version : how to have radio button posts record in asp database


gcapp
04-28-2004, 03:55 PM
I have a question that pertains to an asp form and radio buttons.

I have a form that has 4 radio buttons and I wanted to know if it is possible to have each time a radio button is checked to have that recorded in an asp database??

I have done this before with checkboxes, but i'm not sure if it can be done with radio buttons.

the code for the radio buttons are:
<input type="radio" value="1" name="contest_button">Website
<input type="radio" value="1" name="contest_button">Radio
<input type="radio" value="1" name="contest_button">Print
<input type="radio" value="1" name="contest_button">Other

My Access database has 4 fields for Website, Radio, Print and Other.

How can I get it where if someone checks say Website, that a number "1" will appear in that field??

If someone can help, I would really appreciate it.

raf
04-28-2004, 08:07 PM
Whatever radiobutton someone clicks from this group, it will always just post "1" So if you wanna know which radiobutton he/she clicked, you'll need to have discriminating values. Like

<input type="radio" value="Website" name="contest_button">Website
<input type="radio" value="Radio" name="contest_button">Radio
<input type="radio" value="Print" name="contest_button">Print
<input type="radio" value="Other" name="contest_button">Other

inside the processing page you can then use that to build the sql statement.
Like
sql="update table set " & request.form("contest") & "=1 where whatever=whatever"

gcapp
04-29-2004, 05:16 PM
Raf,
Ok I'm a bit confused about setting up the sql statement (or a little weak as far as the knowledge)

I changed the values of each radio button to Website, Radio, Print and Other and I have this as my sql statements but it is not working:

dlSQL = "INSERT INTO Entries (Website, Radio, Print, Other)" &_
"VALUES ('" & IntWebsite & "','" & IntRadio & "','" & IntPrint & "','" & IntOther & "')"
Set dloRS = Conn.Execute(dlSQL)

dlSQL = "SELECT sum(Website) AS IntWebsite, sum(Radio) AS IntRadio, sum(Print) AS IntPrint, sum(Other) AS IntOther FROM Entries"
Set dloRS = Conn.Execute(dlSQL)

I am not sure how fix the sql as far getting the number "1" to appear in the database.

Any help would be greatly appreciated.

BigDaddy
04-29-2004, 06:11 PM
Do a little validation before your sql statement:

if contest_button = "Website" then
website = "1"
elseif contest_button = "Radio" then
radio= "1"
elseif contest_button = "Print" then
print= "1"
elseif contest_button = "Other" then
other= "1"
end if

This way you're setting each of those values to 1 if the value of the radio button matches. You're doing nothing to it and it is null if it doesn't match.

gcapp
04-30-2004, 02:28 PM
Well I am running into a problem with the last piece of code that Big Daddy told me to use or maybe I am not doing something right.

My code looks like this:

Dim Strcontest_button, IntRadio, IntOther, IntPrint, IntWebsite, StrRadio, StrWebsite, StrPrint, StrOther

<input type="radio" value="Website" name="contest_button">Website
<input type="radio" value="Radio" name="contest_button">Radio
<input type="radio" value="Print" name="contest_button">Print
<input type="radio" value="Other" name="contest_button">Other

If Request.Form("posted") = "True" Then

IntWebsite = 0 & Request.Form("Website")
IntRadio = 0 & Request.Form("Radio")
IntPrint = 0 & Request.Form("Print")
IntOther = 0 & Request.Form("Other")

if contest_button = "Website" then
website = "1"
elseif contest_button = "Radio" then
radio= "1"
elseif contest_button = "Print" then
print= "1"
elseif contest_button = "Other" then
other= "1"
end if


dlSQL = "INSERT INTO Entries (Website, Radio, Print, Other)" &_
"VALUES ('" & IntWebsite & "','" & IntRadio & "','" & IntPrint & "','" & IntOther & "')"
Set dloRS = Conn.Execute(dlSQL)

dlSQL = "SELECT sum(Website) AS IntWebsite, sum(Radio) AS IntRadio, sum(Print) AS IntPrint, sum(Other) AS IntOther FROM Entries"
Set dloRS = Conn.Execute(dlSQL)


If IntWebsite <> "" Then
StrALL = StrALL & IntWebsite & vbcrlf
End if
If IntRadio <> "" Then
StrALL = StrALL & IntRadio & vbcrlf
End if
If IntPrint <> "" Then
StrALL = StrALL & IntPrint & vbcrlf
End if
If IntOther <> "" Then
StrALL = StrALL & IntOther & vbcrlf
End if
StrALL = StrALL & "Total of Website: " & dloRS("IntWebsite") & vbcrlf
StrALL = StrALL & "Total of Radio: " & dloRS("IntRadio") & vbcrlf
StrALL = StrALL & "Total of Print: " & dloRS("IntPrint") & vbcrlf
StrALL = StrALL & "Total of Other: " & dloRS("IntOther") & vbcrlf


Response.Redirect("contest_thanks.asp")


What's happening is no matter where I put the if contest_button section, it causes the response.redirect to not work and I get a blank page.

Am I putting that if contest_button section in the wrong place or is there something else I need to do??

Anybody's help would be greatly appreciated.

BigDaddy
04-30-2004, 03:26 PM
Put this right below your sql statement, but before the conn.execute line:

response.write dlsql : response.end

This would be the first thing to do to troubleshoot the problem -- write it out to the screen to see what you're sending to the database. It appears that your sql statement is being created incorrectly, and it's just doing an insert w/4 null values.

My code that I gave you has the variable names as "website", "radio", blah blah blah...but your sql statement is being created by calling the variables "intwebsite", "intradio", etc..

Just makes sure that you use the same variable names in both spots:

f contest_button = "Website" then
intwebsite = "1"
elseif contest_button = "Radio" then
radio= "1"
elseif contest_button = "Print" then
print= "1"
elseif contest_button = "Other" then
other= "1"
end if


dlSQL = "INSERT INTO Entries (Website, Radio, Print, Other)" &_
"VALUES ('" & IntWebsite & "','" & IntRadio & "','" & IntPrint & "','" & IntOther & "')"
Set dloRS = Conn.Execute(dlSQL)

gcapp
04-30-2004, 04:03 PM
I appreciate your help, however I just can't get it to work. Even with the changes, when I add the if contest_button code, the redirect after the form is filled out, goes to a blank page, instead of the "contest_thanks" page.

I don't know if you can look over my brief code below, but it is all there and the information is sent to my database, except for the radio buttons, which as i said before, needs to the number "1" put into the database fields when checked.

Again any help woudl be appreciated.

-------------------------------------------------------------------------

Dim Strcontest_button, IntRadio, IntOther, IntPrint, IntWebsite, StrRadio, StrWebsite, StrPrint, StrOther

<input type="radio" value="Website" name="contest_button">Website
<input type="radio" value="Radio" name="contest_button">Radio
<input type="radio" value="Print" name="contest_button">Print
<input type="radio" value="Other" name="contest_button">Other

If Request.Form("posted") = "True" Then

IntWebsite = 0 & Request.Form("Website")
IntRadio = 0 & Request.Form("Radio")
IntPrint = 0 & Request.Form("Print")
IntOther = 0 & Request.Form("Other")

if contest_button = "Website" then
IntWebsite = "1"
elseif contest_button = "Radio" then
IntRadio= "1"
elseif contest_button = "Print" then
IntPrint= "1"
elseif contest_button = "Other" then
IntOther= "1"
end if


Set Conn = Server.CreateObject("ADODB.Connection")
StrdlConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("/databases/Contest.mdb") & ";"
Conn.Open StrdlConn

SMTP_SERVER = "smtp.enchantedmountains.info" 'alentus



dlSQL = "INSERT INTO Entries (Website, Radio, Print, Other)" &_
"VALUES ('" & IntWebsite & "','" & IntRadio & "','" & IntPrint & "','" & IntOther & "')"
Set dloRS = Conn.Execute(dlSQL)

dlSQL = "SELECT sum(Website) AS IntWebsite, sum(Radio) AS IntRadio, sum(Print) AS IntPrint, sum(Other) AS IntOther FROM Entries"
Set dloRS = Conn.Execute(dlSQL)


If IntWebsite <> "" Then
StrALL = StrALL & IntWebsite & vbcrlf
End if
If IntRadio <> "" Then
StrALL = StrALL & IntRadio & vbcrlf
End if
If IntPrint <> "" Then
StrALL = StrALL & IntPrint & vbcrlf
End if
If IntOther <> "" Then
StrALL = StrALL & IntOther & vbcrlf
End if
StrALL = StrALL & "Total of Website: " & dloRS("IntWebsite") & vbcrlf
StrALL = StrALL & "Total of Radio: " & dloRS("IntRadio") & vbcrlf
StrALL = StrALL & "Total of Print: " & dloRS("IntPrint") & vbcrlf
StrALL = StrALL & "Total of Other: " & dloRS("IntOther") & vbcrlf



If Request.ServerVariables("HTTP_HOST") <> "alentus" then


ToName = "Enchanted Mountains Region"
ToEmail = "visitor@enchantedmountains.info"

Set Mailer = Server.CreateObject("SMTPsvg.Mailer")
Mailer.FromName = streMail
Mailer.FromAddress= streMail
Mailer.RemoteHost = SMTP_SERVER
Mailer.AddRecipient ToName, ToEmail
Mailer.Subject = "Maple Contest Form"
Mailer.BodyText = StrALL
Mailer.SendMail

End If


Response.Redirect("contest_thanks.asp")

Conn.close
set Conn = nothing

End If

glenngv
05-03-2004, 05:19 AM
If Request.Form("posted") = "True" Then

contest_button = Request.Form("contest_button")

'set default to 0
IntWebsite = 0
IntRadio = 0
IntPrint = 0
IntOther = 0

select case contest_button
case "Website"
IntWebsite = 1
case "Radio"
IntRadio = 1
case "Print"
IntPrint = 1
case "Other"
IntOther = 1
end select

...

end if

gcapp
05-04-2004, 02:09 PM
I appreciate the help that I have been given here on this matter.

I put the code in that was suggested to me in the last post and it still will not record a number "1" in my Access database. I don't know why.


My code looks like this:

Dim Strcontest_button, IntRadio, IntOther, IntPrint, IntWebsite, StrRadio, StrWebsite, StrPrint, StrOther

<input type="radio" value="Website" name="contest_button">Website
<input type="radio" value="Radio" name="contest_button">Radio
<input type="radio" value="Print" name="contest_button">Print
<input type="radio" value="Other" name="contest_button">Other

If Request.Form("posted") = "True" Then

Strcontest_button = Request.Form("contest_button")

'set default to 0
IntWebsite = 0
IntRadio = 0
IntPrint = 0
IntOther = 0

select case Strcontest_button
case "Website"
IntWebsite = 1
case "Radio"
IntRadio = 1
case "Print"
IntPrint = 1
case "Other"
IntOther = 1
end select


dlSQL = "INSERT INTO Entries (Website, Radio, Print, Other)" &_
"VALUES ('" & IntWebsite & "','" & IntRadio & "','" & IntPrint & "','" & IntOther & "')"
Set dloRS = Conn.Execute(dlSQL)



If IntWebsite <> "" Then
StrALL = StrALL & IntWebsite & vbcrlf
End if
If IntRadio <> "" Then
StrALL = StrALL & IntRadio & vbcrlf
End if
If IntPrint <> "" Then
StrALL = StrALL & IntPrint & vbcrlf
End if
If IntOther <> "" Then
StrALL = StrALL & IntOther & vbcrlf
End if

---------------------------------------------------------------

My Access database has 4 fields (Website, Print, Radio, Other) and all have data type of number.

I also added the "Str" in front of "contest_button" to see if it would work and it still doesn't.

Again if anyone has any ideas why this isn't working, I would appreciate it.

glenngv
05-05-2004, 03:26 AM
Debug by putting response.write statements and outputting the SQL statement.

If Request.Form("posted") = "True" Then

Response.Write "Posting records...<br />"

Strcontest_button = Request.Form("contest_button")

Response.Write "Strcontest_button:" & Strcontest_button & "<br />"

'set default to 0
IntWebsite = 0
IntRadio = 0
IntPrint = 0
IntOther = 0

select case Strcontest_button
case "Website"
IntWebsite = 1
case "Radio"
IntRadio = 1
case "Print"
IntPrint = 1
case "Other"
IntOther = 1
end select

dlSQL = "INSERT INTO Entries (Website, Radio, Print, Other)" &_
"VALUES ('" & IntWebsite & "','" & IntRadio & "','" & IntPrint & "','" & IntOther & "')"

Response.Write "SQL:" & dlSQL
Response.End 'end processing for debugging purposes

Set dloRS = Conn.Execute(dlSQL)

'...

End If

Then tell us if the "Posting records..." is displayed then post the SQL statement here.

gcapp
05-05-2004, 02:40 PM
Well I added the code you suggested and figured out what was wrong. I had part of the sql statement wrong, so I changed it and it works now.

Thanks to everyone for helping - this is still the best forum on the net!!!

glenngv
05-05-2004, 02:55 PM
It always helps to debug by doing response.writes to know exactly what the values of variables are and to see where the execution goes. :p