Jump to content

post the checkbox to db


Recommended Posts

hi i am trying to get the checkbox value to my db but its does'work how can give me a simple code ?? :)

When you submit a form with a checkbox, it sends an empty string for unchecked boxes and sends the value of checked boxes just like any other form element. Example:animals.asp
<form action="process.asp" method="post"><p>What kind of pets do you have:<br>Kitties: <input type="checkbox" name="animal1" value="true"><br>Bunnies: <input type="checkbox" name="animal2" value="true"><br>Puppies: <input type="checkbox" name="animal3" value="true"><br>Fishies: <input type="checkbox" name="animal4" value="true"></p><p><input type="submit" value="Submit"></p></form>

Imagine that the user submitted the following data:

Kitties [x]Bunnies [ ]Puppies [ ]Fishies [x]

The page will send the following querystring:


So, to put that information into a database, you use something like this:

Dim Animal1, Animal2, Animal3, Animal4'The following automatically assign the variables declared above to a boolean'value of True or False based on whether they equal the word "true"Animal1 = Cbool(lcase(Request("animal1")) = lcase("true"))   'Returns TrueAnimal2 = Cbool(lcase(Request("animal2")) = lcase("true"))   'Returns FalseAnimal3 = Cbool(lcase(Request("animal3")) = lcase("true"))   'Returns FalseAnimal4 = Cbool(lcase(Request("animal4")) = lcase("true"))   'Returns True

After you determine the value of your checkboxes, you can insert them into your database as usual:

Dim Conn, RS, SQLSet Conn = Server.CreateObject("ADODB.Connection")Set RS = Server.CreateObject("ADODB.Recordset")sql = "SELECT * FROM table_to_update WHERE ID = 65"Conn.Open your_connection_stringRS.Open sql, Conn, 3, 3    RS("Animal1") = Animal1    RS("Animal2") = Animal2    RS("Animal3") = Animal3    RS("Animal4") = Animal4    RS.UpdateRS.CloseConn.CloseSet rs=nothingSet conn=nothing

Link to comment
Share on other sites

Imagine that the user submitted the following data:CODEKitties [x]Bunnies [ ]Puppies [ ]Fishies [x]The page will send the following querystring:CODEprocess.asp?animal1=true&animal2=&animal3=&animal4=true
Actually, oddly enough, it doesn't even send the unchecked boxes. The server receives this:process.asp?animal1=true&animal4=true
Link to comment
Share on other sites

  • 1 month later...

I was working on a solution who's a lot better then this I know it's working on php probably on ASP toif you put a hidden input before the checkbox with the same name and the value to false and after the checkbox with the value to true normal since you have to field with the same name the last one will be keep.So because the info of a uncheck box is not send you will get false of the hidden field.

<input name="accept" type="hidden" value="0" /><input name="accept" type="checkbox"  value="1"  />

You can event don't set the value of the hidden input it will send {field_name}= to the server...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...