Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Concatenate fields checkboxes

    Hello,

    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">
    <tr>
    <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>
    </tr>
    <tr>
    <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>
    </tr>
    <tr>
    <td colspan="4"><input name="category" type="text" style="width:500px"></td>
    </tr>
    </table>

    </form>

    Any help would be appriciated,

    Thanks
    Ian.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes I could do it server side in asp, but javascript seemed easier at the time.

    Out of curiosity can you suggest a solution.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Even if it seemed easier in javascript, you should do it in asp since javascript can be disabled.

    Code:
    <%
    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 
    %>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •