passing multiple checkbox variables w/same cb name ?

Z

zig

I've posted this on alt.comp.lang.coldfusion, 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="JavaScript">
function win(){
var s = '';
f = document.boxs;
for (var i=0;i < f.length;i++){
if (f.elements.name == 'printprop' && f.elements.checked){
if (s.length != 0) s += ',';
s += f.elements.value;
}
} window.open("openprintR.cfm?ID=" +
s,"","height=600,width=600,left=0,top=0")
}
</script>

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

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


Steve.
 
K

kaeli

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

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="javascript" 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.checked)
{
if (s.length != 0) s += ',';
s += f.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>

--
 
S

Steve McMahon

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.
 
K

kaeli

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:

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.

--
 
S

Steve McMahon

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">print this property&nbsp;<input
type="checkbox" name="printprop" value=88></form>

<---stuff inbetween--->

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

<---stuff below--->

so how do you cope with this?


Steve.
 
K

kaeli

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?

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="javascript:win()" method="post">print this
property&nbsp;
<cfoutput query="myquery">
<input type="checkbox" name="printprop"
value=#ID#>
</cfoutput>
</form>

--
 
S

Steve McMahon

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("Session.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"><cflocation
url="#variables.prevpage#S"><cfelse><cflocation
url="requestinfo.cfm"></cfif>
</cfif>

<cfset infoarray=ListToArray(#Form.Info#)>

<cfset size=ArrayLen(infoarray)>
<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.count#]=variables.InfoArray[#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(Session.ReqInfo)#>
<cfif Session.ReqInfo EQ Session.ReqInfo[i-1]>
<cfset temp=ArrayDeleteAt(Session.ReqInfo,i)>
<cfset i=i-1>
</cfif></cfif>
</cfloop>
</cfif>


Steve.
 
K

kaeli

sorry, but now I'm getting confused.

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.
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.

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.
Would you then be calling on that query with the javascript seperatley?

The CF query runs on the server and generates html.
The JS runs on the client.
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:
<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="javascript:win()">print report</a>

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


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

<form name="boxs" action="javascript:win()" method="post">print 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.

--
 
S

Steve McMahon

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.
 
K

kaeli

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

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
--
 
K

kaeli

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?

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?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 
S

Steve McMahon

Kaeli,

You're a star :eek:)

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

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


Steve.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top