passing multiple checkbox variables w/same cb name ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zig

    #1

    passing multiple checkbox variables w/same cb name ?

    I've posted this on alt.comp.lang.c oldfusion, but is predominantly a
    javascript problem:

    I have a CF query which returns several rows of records. I wanted to
    have a checkbox on each record of that returned query which would
    allow the user to select which record to print if checked.

    As the checkboxes are generated within the CF, they all have tha same
    name. With the help of a fellow a.c.l.c. member, we've got the script
    to a point where it will define whether 1 box is checked or not, only
    when there are more than 1, I receive the error: 'f.length is null or
    not an object'.

    Here's the script as it is now. Any help greatly appreciated:


    <script language="JavaS cript">
    function win(){
    var s = '';
    f = document.boxs;
    for (var i=0;i < f.length;i++){
    if (f.elements[i].name == 'printprop' && f.elements[i].checked){
    if (s.length != 0) s += ',';
    s += f.elements[i].value;
    }
    } window.open("op enprintR.cfm?ID =" +
    s,"","height=60 0,width=600,lef t=0,top=0")
    }
    </script>

    <body>
    <a href="javascrip t:win()">print report</a>

    <cfoutput query="myquery" >
    <form name="boxs" action="javascr ipt:win()" method="post">p rint this
    property&nbsp;< input type="checkbox" name="printprop "
    value=#ID#></form>
    </cfoutput>


    Steve.
  • kaeli

    #2
    Re: passing multiple checkbox variables w/same cb name ?

    In article <f7d3ce0f.04012 41333.22a08b24@ posting.google. com>,
    vidiwell@btopen world.com enlightened us with...

    [color=blue]
    >
    > Here's the script as it is now. Any help greatly appreciated:[/color]

    Assumption 1: you want a string "s" with the values of the checked
    checkboxes, separated by commas.
    Assumption 2: all the checkboxes are named "printprop" .

    Note that this is the test script, so values are hardcoded and link set
    to alert. Change this as needed.

    <html>
    <head>
    <title> New Document </title>
    <script language="javas cript" type="text/javascript">
    function win()
    {
    var s = '';
    f = document.forms["boxs"].elements["printprop"];
    if (f.length)
    {
    for (var i=0;i < f.length;i++)
    {
    if (f[i].checked)
    {
    if (s.length != 0) s += ',';
    s += f[i].value;
    }
    }
    }
    else if (f)
    {
    s += f.value;
    }
    alert(s);
    }
    </script>
    </head>

    <body>
    <a href="#" onClick="win(); return false;">print report</a>

    <form name="boxs" action="" method="get">
    property 1:<input type="checkbox" name="printprop " value='1'><br>
    property 2:<input type="checkbox" name="printprop " value='2'><br>
    property 3:<input type="checkbox" name="printprop " value='3'><br>
    </form>
    </body>
    </html>

    --
    --
    ~kaeli~
    You feel stuck with your debt if you can't budge it.



    Comment

    • Steve McMahon

      #3
      Re: passing multiple checkbox variables w/same cb name ?

      Kaeli,

      first off - thanks for the reply!

      OK, I tried your testing script and it works fine with one checkbox, but
      I think the problem lies with the fact that I'm having to create the
      checkboxes within a coldfusion output table and therefore their
      quantities vary dependant upon how many rows are returned.

      When there is more than 1 present, I get this error upon submit:

      'document.forms .boxs.elements. printprop is null or not an object'


      Steve.



      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • kaeli

        #4
        Re: passing multiple checkbox variables w/same cb name ?

        In article <401534af$0$703 05$75868355@new s.frii.net>,
        vidiwell@btopen world.com enlightened us with...[color=blue]
        > Kaeli,
        >
        > first off - thanks for the reply!
        >
        > OK, I tried your testing script and it works fine with one checkbox, but
        > I think the problem lies with the fact that I'm having to create the
        > checkboxes within a coldfusion output table and therefore their
        > quantities vary dependant upon how many rows are returned.
        >
        > When there is more than 1 present, I get this error upon submit:
        >[/color]

        The test script works just fine with multiples - therefore, the error is
        not the script per se.

        Let's see your generated HTML - that is, view the source of the page to
        see the html output the script is actually working with.

        I don't need the whole thing if it's huge - just the form and the
        script.

        How the html was generated doesn't matter a bit, whether by CF, JSP, or
        ASP. The browser sees only html.

        --
        --
        ~kaeli~
        Never mess up an apology with an excuse.



        Comment

        • Steve McMahon

          #5
          Re: passing multiple checkbox variables w/same cb name ?

          Ah, I see what you're geting at - the form is duplicated by the CF if
          more than one row returned, and therefore returns more than one form,
          both with the same name + elements:

          <---stuff above--->

          <form name="boxs" action="" method="get">pr int this property&nbsp;< input
          type="checkbox" name="printprop " value=88></form>

          <---stuff inbetween--->

          <form name="boxs" action="" method="get">pr int this property&nbsp;< input
          type="checkbox" name="printprop " value=149></form>

          <---stuff below--->

          so how do you cope with this?


          Steve.



          *** Sent via Developersdex http://www.developersdex.com ***
          Don't just participate in USENET...get rewarded for it!

          Comment

          • kaeli

            #6
            Re: passing multiple checkbox variables w/same cb name ?

            In article <40156126$0$703 06$75868355@new s.frii.net>,
            vidiwell@btopen world.com enlightened us with...[color=blue]
            > Ah, I see what you're geting at - the form is duplicated by the CF if
            > more than one row returned, and therefore returns more than one form,
            > both with the same name + elements:
            >
            > so how do you cope with this?
            >[/color]

            Change my CF to do it properly. :)

            I should have caught this the first time you posted. Sorry. It's been
            awhile since I used CF.

            Change it to:

            <form name="boxs" action="javascr ipt:win()" method="post">p rint this
            property&nbsp;
            <cfoutput query="myquery" >
            <input type="checkbox" name="printprop "
            value=#ID#>
            </cfoutput>
            </form>

            --
            --
            ~kaeli~
            A man's home is his castle..., in a manor of speaking.



            Comment

            • Steve McMahon

              #7
              Re: passing multiple checkbox variables w/same cb name ?

              sorry, but now I'm getting confused.

              You asked to see the html output, but in my CF code the form has to
              exist within a larger output [there are many other CF variables above
              and below] but all within the same output row. I suppose I could place a
              seperate output within the form and continue after, but I'm not sure I
              understand what difference that makes.

              Would you then be calling on that query with the javascript seperatley?

              I don't know if htis helps or confuses matters more, but I saw a similar
              feature on a website recently. The designer kindly forwarded the CF code
              they used to iterate the form, only I'm not sure how to implement it
              into my schema. As you seem to have worked with CF I thought I'd include
              it, just in case it's of some use:


              <!--- Checks if the Array is Defined and Creates --->
              <cfif NOT IsDefined("Sess ion.ReqInfo")>
              <cfset Session.ReqInfo =ArrayNew(1)>
              </cfif>

              <!--- If no information was requested proceed to Request Information
              page --->
              <cfif NOT IsDefined("Form .Info")>
              <cfif left(prevpage,6 ) IS "detail"><cfloc ation
              url="#variables .prevpage#S"><c felse><cflocati on
              url="requestinf o.cfm"></cfif>
              </cfif>

              <cfset infoarray=ListT oArray(#Form.In fo#)>

              <cfset size=ArrayLen(i nfoarray)>
              <cfset count=ArrayLen( session.reqinfo )>

              <!--- Load Array with new Requests --->
              <cfloop index="i" from="1" to="#variables. size#">
              <cfset count=count+1>
              <cfset Session.ReqInfo[#variables.coun t#]=variables.Info Array[#i#]>
              </cfloop>

              <cfif variables.count GT 1>
              <!--- Sort Array --->
              <cfset temp=ArraySort( Session.ReqInfo , "numeric")>

              <!--- Eliminate Duplicates --->
              <cfset i=1>
              <cfloop condition="i LT count">
              <cfset i=i+1>
              <cfif i LE #ArrayLen(Sessi on.ReqInfo)#>
              <cfif Session.ReqInfo[i] EQ Session.ReqInfo[i-1]>
              <cfset temp=ArrayDelet eAt(Session.Req Info,i)>
              <cfset i=i-1>
              </cfif></cfif>
              </cfloop>
              </cfif>


              Steve.





              *** Sent via Developersdex http://www.developersdex.com ***
              Don't just participate in USENET...get rewarded for it!

              Comment

              • kaeli

                #8
                Re: passing multiple checkbox variables w/same cb name ?

                In article <401588fc$0$703 02$75868355@new s.frii.net>,
                vidiwell@btopen world.com enlightened us with...[color=blue]
                > sorry, but now I'm getting confused.
                >[/color]

                The key is understanding that CF executes on the server and javascript
                executes on the client.
                The way you had your CF, the loop was creating a separate form each
                time.
                [color=blue]
                > You asked to see the html output, but in my CF code the form has to
                > exist within a larger output [there are many other CF variables above
                > and below] but all within the same output row. I suppose I could place a
                > seperate output within the form and continue after, but I'm not sure I
                > understand what difference that makes.
                >[/color]

                The difference is what the browser sees and the html that is generated.
                This example helps illustrate the problem.
                There is a very big difference between

                1.
                var j=0;
                var k=0;
                for (i=0; i<10; i++)
                {
                k++;
                }
                j++;

                2.
                var j=0;
                var k=0;
                for (i=0; i<10; i++)
                {
                k++;
                j++;
                }

                What is happening is that you are doing something in a loop that should
                not be in a loop.
                [color=blue]
                > Would you then be calling on that query with the javascript seperatley?
                >[/color]

                The CF query runs on the server and generates html.
                The JS runs on the client.
                [color=blue]
                > I don't know if htis helps or confuses matters more, but I saw a similar
                > feature on a website recently. The designer kindly forwarded the CF code
                > they used to iterate the form, only I'm not sure how to implement it
                > into my schema. As you seem to have worked with CF I thought I'd include
                > it, just in case it's of some use:
                >[/color]
                <snip>

                There is no form in that code. That code *processes* a form. BIG
                difference.

                Just change your original code to my correction (or equivalent) and see
                if it helps you.
                This was your original:
                <a href="javascrip t:win()">print report</a>

                <cfoutput query="myquery" >
                <form name="boxs" action="javascr ipt:win()" method="post">p rint this
                property&nbsp;< input type="checkbox" name="printprop "
                value=#ID#></form>
                </cfoutput>


                Change it to
                <a href="javascrip t:win()">print report</a>

                <form name="boxs" action="javascr ipt:win()" method="post">p rint this
                property&nbsp;
                <cfoutput query="myquery" >
                <input type="checkbox" name="printprop "
                value=#ID#>
                </cfoutput>
                </form>

                See how I moved the query so it iterated only for the form elements, not
                the form tag?
                Your goal is to have only one form tag and one end form tag. The queries
                that generate the elements go inside. Any other CF variables that do not
                affect the html output make no difference as to placement. They run on
                the server and the client doesn't see them.

                --
                --
                ~kaeli~
                A boiled egg in the morning is hard to beat.



                Comment

                • Steve McMahon

                  #9
                  Re: passing multiple checkbox variables w/same cb name ?

                  Kaeli,

                  Thanks for the time you've given to this. I took onboard your
                  suggestions re the form and moved the <cfoutput> and <form> tags around
                  and bingo, I now can retrieve the variables as destinct values within a
                  comma delimeted list.

                  My sring is now something like:

                  http://www.,mysite.com/printpage.cfm?ID=10,20,30 etc

                  My only problem now being how to interprate that list on my printpage. I
                  have tried SQL statements:

                  WHERE ID LIKE '#ListFirst(ID) #'

                  and I've tried

                  WHERE ID LIKE '#ListRest(ID)# '

                  but they only return the residual values of their conditions. Do you
                  know of a way to retrieve ALL of the Lists values?

                  Thanks,


                  Steve.



                  *** Sent via Developersdex http://www.developersdex.com ***
                  Don't just participate in USENET...get rewarded for it!

                  Comment

                  • kaeli

                    #10
                    Re: passing multiple checkbox variables w/same cb name ?

                    In article <4017e102$0$703 09$75868355@new s.frii.net>,
                    vidiwell@btopen world.com enlightened us with...[color=blue]
                    > Kaeli,
                    >
                    > Thanks for the time you've given to this. I took onboard your
                    > suggestions re the form and moved the <cfoutput> and <form> tags around
                    > and bingo, I now can retrieve the variables as destinct values within a
                    > comma delimeted list.
                    >
                    > My sring is now something like:
                    >
                    > http://www.,mysite.com/printpage.cfm?ID=10,20,30 etc
                    >[/color]

                    Well, it needs to be url safe. No spaces. No special characters.
                    I don't know if your real url is b/c I don't remember if a comma is cool
                    in the url.

                    To check, in the body (between <body> and </body> html tags) of
                    printpage (or just a temp CF page with nothing but this to make it
                    easy), do
                    <cfif isDefined("url. ID")>
                    <cfoutput>#url. ID#</cfoutput>
                    <cfelse>
                    Arg! No ID!
                    </cfif>

                    See if that gets printed as
                    10,20,30 etc

                    If it does, change your SQL to

                    WHERE ID IN (#url.ID#)

                    assuming ID is a numeric field in the DB, not a string.
                    If it's a string, you'll have to quote the values. The ID is taken in as
                    a string, not a list (I believe), so what I would do (assuming CF has no
                    function for this, which IIRC it does not) is prepend a quote to the
                    string, run replace on the commas and make them quote, comma, quote,
                    then append a quote to the end, resulting in
                    '10','20','30'

                    HTH
                    --
                    --
                    ~kaeli~
                    Support your local medical examiner: die strangely!



                    Comment

                    • kaeli

                      #11
                      Re: passing multiple checkbox variables w/same cb name ?

                      In article <4017e102$0$703 09$75868355@new s.frii.net>,
                      vidiwell@btopen world.com enlightened us with...[color=blue]
                      > WHERE ID LIKE '#ListFirst(ID) #'
                      >
                      > and I've tried
                      >
                      > WHERE ID LIKE '#ListRest(ID)# '
                      >
                      > but they only return the residual values of their conditions. Do you
                      > know of a way to retrieve ALL of the Lists values?
                      >[/color]

                      In case I wasn't clear in my other post, the url string is NOT a List. I
                      know it was a List when you made the url on the first page, but the
                      printpage doesn't know about that. It sees a url as a querystring. The
                      commas might make it into a List by default in CF, but technically, it's
                      a string. So be careful before using List functions.

                      ListFirst would only get one value, if it were a List. Not much help
                      there.
                      LIKE assumes the value is one big string. Putting an entire list in
                      there wouldn't do much for you. You want IN, not LIKE.
                      See my other post for more on that.

                      --
                      --
                      ~kaeli~
                      Is it true that cannibals don't eat clowns because they
                      taste funny?



                      Comment

                      • Steve McMahon

                        #12
                        Re: passing multiple checkbox variables w/same cb name ?

                        Kaeli,

                        You're a star :o)

                        WHERE ID IN (#url.ID#) - did the trick!

                        I can't begin to thank you enough for your persistance.


                        Steve.



                        *** Sent via Developersdex http://www.developersdex.com ***
                        Don't just participate in USENET...get rewarded for it!

                        Comment

                        • kaeli

                          #13
                          Re: passing multiple checkbox variables w/same cb name ?

                          In article <401829a7$0$703 01$75868355@new s.frii.net>,
                          vidiwell@btopen world.com enlightened us with...[color=blue]
                          > Kaeli,
                          >
                          > You're a star :o)
                          >
                          > WHERE ID IN (#url.ID#) - did the trick!
                          >
                          > I can't begin to thank you enough for your persistance.
                          >
                          >[/color]

                          You're more than welcome.

                          --
                          --
                          ~kaeli~
                          If it's tourist season, why can't we shoot them?



                          Comment

                          Working...