Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing checkbox values to stored procedure

Status
Not open for further replies.

rmagan

Programmer
Jun 3, 2003
35
US
I have a HTML form that uses a checkboxes in a table. The form calls a stored procedure. When the user checks all the checkboxes the procedure works fine. If not, the procedure does not work because the HTML form is not passing anything for the checkbox. I tried the code below, but it doesn't help. Any suggestions?

<script language=&quot;Javascript&quot;>
function checkFunction(obj) {
if (obj.checked) {
obj.value = &quot;on&quot;;
}
else {
obj.value = &quot;off&quot;;
}
}
</script>

<input type=&quot;checkbox&quot; name=&quot;p_drop_class&quot; onclick=&quot;checkFunction(this)&quot; value=&quot;off&quot;>
 
this is not how checkboxes work. a checkbox will only send its &quot;value&quot; when checked. if you need to always send a value, use radio buttons or a select list.


=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
How many checkboxes do you have? Are they all named the same? Are you using a function to pass the values? From the code you supplied you're only showing one checkbox. Post a little more code and it'll be easier for us to give ya a suggestion.

-kaht

banghead.gif
 
rmagan,

When you create a checkbox, you give it a value.

The user can check (or uncheck) the checkbox at will.

When the form is sent, all checkboxes in the form are parsed. If any checkbox is checked, then the checkbox name and value will be sent. If a checkbox is not checked, then nothing about the checkbox will be sent... not even the name of the checkbox.

If you can't get around using checkboxes, you can get rid of all the onClick code. Instead, add an onClick to the submit button for the form only:

Code:
<input type=&quot;submit&quot; value=&quot;Send data&quot; onClick=&quot;mySubmit()&quot;>

Then write a function that checks if each checkbox is on (or not), setting values in hidden input fields -- one hidden input field for each checkbox. Take care to name the hidden input fields the same as the checkboxes are currently named, and to rename the checkboxes to something else. That way the server-side code will still work.

I'm assuming you don't have access to the server-side code? If so, you could set defaults on the parameters (so that if a checkbox isn't supplied it would default to &quot;no&quot;, maybe).

Regards,
Jeff
 
Alternately you could make only one hidden variable so that your server side doesn't have to accuont for pulling a dynamic size of hidden fields - it would only pull one - a string with all the values you need delimited by a space or whatever character you choose.

it would look something like this:

&quot;on on off on off off on off&quot;

Once the server received the string you can use the split function to turn it into an array



-kaht

banghead.gif
 
You could use hidden fields to pass the values...

<input type=&quot;checkbox&quot; name=&quot;p_drop_class&quot; onclick=&quot;this.form[this.name+'_value'].value=this.checked?'on':'off'&quot;>
<input type=&quot;hidden&quot; name=&quot;p_drop_class_value&quot; value=&quot;off&quot;>

Or even better would be to have a function on the server side that parses the checkbox value. In ASP it would look like this...

<%
Function chk(c)
If Len(c)>0 Then
chk=&quot;on&quot;
Else
chk=&quot;off&quot;
End If
End Function

Response.Write &quot;The checkbox is &quot; & chk(Request(&quot;p_drop_class&quot;))
%>

Adam
while(ignorance==true){perpetuate(violence,fear,hatred);life--};
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top