View Full Version : Concatenate fields checkboxes

07-14-2005, 12:39 PM

Javascript is not my specialty so I would greatly appriciate any help anyone can give.

My Problem:
I have a form with several check boxes. These are categories for a seach field.
When a user checks different boxes on submit I want the values for the "selected" check boxes to be concatenated into a field.

Currently I have an onclick of the checkbox which concatenates the values into a field. The problem with this is when some one unselects a checkbox the value is still left / added again to the concatenated field.

So the javascript concatenate needs to be on the form submit when the user is happy with their selections, and only for the selected check boxes.

Here is my simple current code:
<form action="" method="get" name="categories">
<table cellspacing="2" cellpadding="2" border="0">
<td><input name="inter" type="checkbox" value="International Films" onclick="this.form['category'].value=this.form['category'].value + ' ' + this.form['inter'].value;"></td><td>International Films</td>
<td><input name="conferences" type="checkbox" value="Conferences" onclick="this.form['category'].value=this.form['category'].value + ' ' + this.form['conferences'].value;"></td><td>Conferences</td>
<td><input name="adverts" type="checkbox" value="Adverts" onselect="this.form['category'].value=this.form['category'].value + ' ' + this.form['adverts'].value;"></td><td>Adverts</td>
<td><input name="corporate" type="checkbox" value="Conferences" on></td><td>Corporate Films</td>
<td colspan="4"><input name="category" type="text" style="width:500px"></td>


Any help would be appriciated,


07-14-2005, 01:02 PM
Are you going to submit the form to a server-side page (asp, php, etc.)? If yes, then you should concatenate the checkbox values on the server-side and not on the client-side.

07-14-2005, 01:10 PM
Yes I could do it server side in asp, but javascript seemed easier at the time.

Out of curiosity can you suggest a solution.

07-14-2005, 02:19 PM
Even if it seemed easier in javascript, you should do it in asp since javascript can be disabled.

dim strChecked, inter, conf, ads
inter = request.querystring("inter")
conf = request.querystring("conferences")
ads = request.querystring("adverts")
if inter <> "" then
strChecked = inter & "<br />"
end if
if conf <> "" then
strChecked = strChecked & conf & "<br />"
end if
if ads <> "" then
strChecked = strChecked & ads & "<br />"
end if
response.write "<h1>Selected items:</h1>" & strChecked