SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot moretea's Avatar
    Join Date
    Jun 2004
    Location
    Texas, USA
    Posts
    118
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Parsing returned values from multiple checkboxes in JScript

    When multiple checkboxes are checked, they are returned in the form of a comma delimited string, right? So you'd think it would be easy to do Request.Form('checkboxes').Item.split(",") and then iterate through the array to see which boxes were checked, right? Except I get an ASP error that the form object does not support that method. So, how do I parse that returned string to get the checked values?

    Here's the HTML:
    Code:
    <p>
            <label for="meat">Meat</label> <span class="err" id="errMeat">Please select one.</span><br />
            <input type="checkbox" id="meat_turkey" name="meat" value="turkey" />&nbsp;Turkey<br />
            <input type="checkbox" id="meat_ham" name="meat" value="ham" />&nbsp;Ham<br />
            <input type="checkbox" id="meat_roastbeef" name="meat" value="beef" />&nbsp;Roast beef<br />
            <input type="checkbox" id="meat_double" name="meat" value="doublemeat" />&nbsp;Double meat
            </p>
    And here, I'm doing server-side validation. I'm collecting the checked values to add to a comma delimited string of column names to be used in a SQL INSERT.
    Code:
    function valid8(myArray,frmVal) { //myArray = allowed values, frmVal = returned values
        var ln = myArray.length;
        var arrFrmVal = Array(frmVal.split(",")); // Error thrown here; "Object doesn't support this property or method"
        for (i = 0;i < ln;i++) {
            for (j = 0; j < arrFrmVal.length; j++) {
                if (arrFrmVal[j] == (myArray[i])) {
                    strColNames = strColNames + myArray[i] + ",";
                    strColVals = strColVals + "'Y',";
                    matched = true;
                }
            }
        }
        if (matched == false) {
            finalMsg = finalMsg + "<br />Error: found an incomplete field";
        }
        Response.Write("valid8 = " + strColNames + ", " + strColVals + "<br />");
    }
    Thanks in advance for any help!

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Location
    OH, USA
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in asp page request("meat") will be equal to "" if no any chaeckboxes checked, or single value in checked only one checkbox, so if you trying to split(",") single value like "turkey" or "" you will get error and this is what happence

  3. #3
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The Split method creates an array on its own - you don't need to use the Array call. That'll be why you get the error thrown.
    Ian Anderson
    www.siteguru.co.uk


Bookmarks

Posting Permissions

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