Arrays as form elemtents

S

szar

I'm passing numerous array elements called list[] when a form is submitted.
the brackets [ ] are necessary for PHP to see all the values as an array.
The problem is I can't seem to reference the form elements from javascripts
because of the brackets. For example,
form.list[].value is no good. Anyone know a way around this? Thanks.
 
R

Richard Cornford

szar said:
I'm passing numerous array elements called list[] when a form is submitted.
the brackets [ ] are necessary for PHP to see all the values as an array.
The problem is I can't seem to reference the form elements from javascripts
because of the brackets. For example,
form.list[].value is no good. Anyone know a way around this? Thanks.

Quick answer:-

<URL: http://jibbering.com/faq/#FAQ4_25 >

- and follow links from :-

<URL: http://jibbering.com/faq/#FAQ4_39 >

- to understand .

Richard.
 
L

Lasse Reichstein Nielsen

szar said:
I'm passing numerous array elements called list[] when a form is submitted.
the brackets [ ] are necessary for PHP to see all the values as an array.
The problem is I can't seem to reference the form elements from javascripts
because of the brackets. For example,
form.list[].value is no good. Anyone know a way around this? Thanks.

<URL:http://jibbering.com/faq/#FAQ4_25>

/L
 
S

szar

Richard Cornford said:
szar said:
I'm passing numerous array elements called list[] when a form is submitted.
the brackets [ ] are necessary for PHP to see all the values as an array.
The problem is I can't seem to reference the form elements from javascripts
because of the brackets. For example,
form.list[].value is no good. Anyone know a way around this? Thanks.

Quick answer:-

<URL: http://jibbering.com/faq/#FAQ4_25 >

- and follow links from :-

<URL: http://jibbering.com/faq/#FAQ4_39 >

- to understand .

Richard.

Call me stupid but that didn't seem to work. Here's the script I'm using,
maybe it'll help:

<SCRIPT LANGUAGE="JavaScript">
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field.checked = true;
}
checkflag = "true";
return "Uncheck All";
} else {
for (i = 0; i < field.length; i++) {
field.checked = false;
}
checkflag = "false";
return "Check All";
}
}
</script>

<form>
<input type=checkbox value="Check All"
onClick="this.value=check(this.form.list)">
<input type="checkbox" name="list" value="thisisdifferentforeach">
<input type="checkbox" name="list" value="thisisdifferentforeach">
<input type="checkbox" name="list" value="thisisdifferentforeach">
..
..
..
</form>

Basically, clicking the top checkbox checks (or unchecks) all the ones
below. This works how it is but I need the checkbox names to be list[]
instead of list. If anyone could edit the above code to give me a better
idea I'd be greatful!!!
Thanks!
 
Z

ziemon

this helpfull ?

<SCRIPT LANGUAGE="JavaScript">
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field.checked = true;
}
checkflag = "true";
return "Uncheck All";
} else {
for (i = 0; i < field.length; i++) {
field.checked = false;
}
checkflag = "false";
return "Check All";
}
}

function test()
{
myname.list[0].checked = true;
}

function test2(checkboxnum)
{
this.checkboxnum = checkboxnum
myname.list[checkboxnum].checked = true;
}
</script>

<form name="myname">
<input type=checkbox value="Check All"
onClick="this.value=check(this.form.list)">
<input type="checkbox" name="list" value="thisisdifferentforeach">
<input type="checkbox" name="list" value="thisisdifferentforeach">
<input type="checkbox" name="list" value="thisisdifferentforeach">
</form>
<a href="javascript:test()">turn on the checkbox list 0</a>
<br>
or
<br>
<a href="javascript:test2('0')">turn on the checkbox list 0</a>



Lasse Reichstein Nielsen said:
szar said:
I'm passing numerous array elements called list[] when a form is submitted.
the brackets [ ] are necessary for PHP to see all the values as an array.
The problem is I can't seem to reference the form elements from javascripts
because of the brackets. For example,
form.list[].value is no good. Anyone know a way around this? Thanks.

<URL:http://jibbering.com/faq/#FAQ4_25>

/L
 
Z

ziemon

added 3rd option


<SCRIPT LANGUAGE="JavaScript">
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field.checked = true;
}
checkflag = "true";
return "Uncheck All";
} else {
for (i = 0; i < field.length; i++) {
field.checked = false;
}
checkflag = "false";
return "Check All";
}
}

function test()
{
myname.list[0].checked = true;
}

function test2(checkboxnum)
{
this.checkboxnum = checkboxnum
myname.list[checkboxnum].checked = true;
}

function test3(checkboxnum)
{
this.checkboxnum = checkboxnum
myname.elements[checkboxnum].checked = true;
}
</script>

<form name="myname">
<input type=checkbox value="Check All"
onClick="this.value=check(this.form.list)">
<input type="checkbox" name="list" value="thisisdifferentforeach">
<input type="checkbox" name="list" value="thisisdifferentforeach">
<input type="checkbox" name="list" value="thisisdifferentforeach">
..
..
..
</form>
<a href="javascript:test()">turn on the checkbox list 0</a>
<br>
or
<br>
<a href="javascript:test2('0')">turn on the checkbox list 0</a>
<br>
or
<br>
<a href="javascript:test3('0')">turn on the checkbox list 0</a>



Lasse Reichstein Nielsen said:
szar said:
I'm passing numerous array elements called list[] when a form is submitted.
the brackets [ ] are necessary for PHP to see all the values as an array.
The problem is I can't seem to reference the form elements from javascripts
because of the brackets. For example,
form.list[].value is no good. Anyone know a way around this? Thanks.

<URL:http://jibbering.com/faq/#FAQ4_25>

/L
 
D

Douglas Crockford

var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field.checked = true;
}
checkflag = "true";


It isn't a good idea to use the string "true" and "false" as boolean flags. The
language has boolean true and false, use those instead. Otherwise, you could
have a mixup. "false" is usually assumed to be true.

http://www.crockford.com/#javascript
 
S

szar

Douglas Crockford said:
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field.checked = true;
}
checkflag = "true";


It isn't a good idea to use the string "true" and "false" as boolean flags. The
language has boolean true and false, use those instead. Otherwise, you could
have a mixup. "false" is usually assumed to be true.

http://www.crockford.com/#javascript


Thanks Douglas, good point. This was just a script I downloaded and I
followed another good rule, if it ain't broke, don't fix it, so I just left
it.
 
S

szar

ziemon said:
added 3rd option


<SCRIPT LANGUAGE="JavaScript">
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field.checked = true;
}
checkflag = "true";
return "Uncheck All";
} else {
for (i = 0; i < field.length; i++) {
field.checked = false;
}
checkflag = "false";
return "Check All";
}
}

function test()
{
myname.list[0].checked = true;
}

function test2(checkboxnum)
{
this.checkboxnum = checkboxnum
myname.list[checkboxnum].checked = true;
}

function test3(checkboxnum)
{
this.checkboxnum = checkboxnum
myname.elements[checkboxnum].checked = true;
}
</script>

<form name="myname">
<input type=checkbox value="Check All"
onClick="this.value=check(this.form.list)">
<input type="checkbox" name="list" value="thisisdifferentforeach">
<input type="checkbox" name="list" value="thisisdifferentforeach">
<input type="checkbox" name="list" value="thisisdifferentforeach">
.
.
.
</form>
<a href="javascript:test()">turn on the checkbox list 0</a>
<br>
or
<br>
<a href="javascript:test2('0')">turn on the checkbox list 0</a>
<br>
or
<br>
<a href="javascript:test3('0')">turn on the checkbox list 0</a>



Lasse Reichstein Nielsen said:
szar said:
I'm passing numerous array elements called list[] when a form is submitted.
the brackets [ ] are necessary for PHP to see all the values as an array.
The problem is I can't seem to reference the form elements from javascripts
because of the brackets. For example,
form.list[].value is no good. Anyone know a way around this? Thanks.

<URL:http://jibbering.com/faq/#FAQ4_25>

/L

I appreciate all the help but it still doesn't work. I think using elements
was on the right track but appearently I don't know enough about it to get
it to work. Here's what I want to happen:

<SCRIPT LANGUAGE="JavaScript">
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field.checked = true;
}
checkflag = "true";
return "Uncheck All";
} else {
for (i = 0; i < field.length; i++) {
field.checked = false;
}
checkflag = "false";
return "Check All";
}
}
</script>
<form>
<input type=checkbox value="Check All"
onClick="this.value=check(this.form.list[])">
<input type="checkbox" name="list[]" value="thisisdifferentforeach">
<input type="checkbox" name="list[]" value="thisisdifferentforeach">
<input type="checkbox" name="list[]" value="thisisdifferentforeach">
..
..
..
</form>

However, the passing of list[] to the check function done in the onClick of
the first checkbox gives an error. Help, pleeeeeeeeeeeeeeeeease!
 
V

Vjekoslav Begovic

szar said:
Call me stupid but that didn't seem to work. Here's the script I'm using,
maybe it'll help:

<SCRIPT LANGUAGE="JavaScript">
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field.checked = true;
}
checkflag = "true";
return "Uncheck All";
} else {
for (i = 0; i < field.length; i++) {
field.checked = false;
}
checkflag = "false";
return "Check All";
}
}
</script>

<form>
<input type=checkbox value="Check All"
onClick="this.value=check(this.form.list)">
<input type="checkbox" name="list" value="thisisdifferentforeach">
<input type="checkbox" name="list" value="thisisdifferentforeach">
<input type="checkbox" name="list" value="thisisdifferentforeach">
.
.
.
</form>

Basically, clicking the top checkbox checks (or unchecks) all the ones
below. This works how it is but I need the checkbox names to be list[]
instead of list. If anyone could edit the above code to give me a better
idea I'd be greatful!!!
Thanks!



<input type=checkbox value="Check All"
onClick="this.value=check(this.form.elements['list[]'])">
<input type="checkbox" name="list[]" value="thisisdifferentforeach">
<input type="checkbox" name="list[]" value="thisisdifferentforeach">
<input type="checkbox" name="list[]" value="thisisdifferentforeach">
 
G

Grant Wagner

szar said:
ziemon said:
added 3rd option


<SCRIPT LANGUAGE="JavaScript">
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field.checked = true;
}
checkflag = "true";
return "Uncheck All";
} else {
for (i = 0; i < field.length; i++) {
field.checked = false;
}
checkflag = "false";
return "Check All";
}
}

function test()
{
myname.list[0].checked = true;
}

function test2(checkboxnum)
{
this.checkboxnum = checkboxnum
myname.list[checkboxnum].checked = true;
}

function test3(checkboxnum)
{
this.checkboxnum = checkboxnum
myname.elements[checkboxnum].checked = true;
}
</script>

<form name="myname">
<input type=checkbox value="Check All"
onClick="this.value=check(this.form.list)">
<input type="checkbox" name="list" value="thisisdifferentforeach">
<input type="checkbox" name="list" value="thisisdifferentforeach">
<input type="checkbox" name="list" value="thisisdifferentforeach">
.
.
.
</form>
<a href="javascript:test()">turn on the checkbox list 0</a>
<br>
or
<br>
<a href="javascript:test2('0')">turn on the checkbox list 0</a>
<br>
or
<br>
<a href="javascript:test3('0')">turn on the checkbox list 0</a>



Lasse Reichstein Nielsen said:
I'm passing numerous array elements called list[] when a form is submitted.
the brackets [ ] are necessary for PHP to see all the values as an array.
The problem is I can't seem to reference the form elements from javascripts
because of the brackets. For example,
form.list[].value is no good. Anyone know a way around this? Thanks.

<URL:http://jibbering.com/faq/#FAQ4_25>

/L

I appreciate all the help but it still doesn't work. I think using elements
was on the right track but appearently I don't know enough about it to get
it to work. Here's what I want to happen:

<SCRIPT LANGUAGE="JavaScript">
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field.checked = true;
}
checkflag = "true";
return "Uncheck All";
} else {
for (i = 0; i < field.length; i++) {
field.checked = false;
}
checkflag = "false";
return "Check All";
}
}
</script>
<form>
<input type=checkbox value="Check All"
onClick="this.value=check(this.form.list[])">
<input type="checkbox" name="list[]" value="thisisdifferentforeach">
<input type="checkbox" name="list[]" value="thisisdifferentforeach">
<input type="checkbox" name="list[]" value="thisisdifferentforeach">
.
.
.
</form>

However, the passing of list[] to the check function done in the onClick of
the first checkbox gives an error. Help, pleeeeeeeeeeeeeeeeease!


You were told in the first response to your intial post:

<url: http://jibbering.com/faq/#FAQ4_25 />

4.25 My element is named myselect[] , how do I access it?

Form elements with any "illegal" characters can be accessed with
formref.elements["myselect[]"] - These characters are illegal in the standard
(x)HTML doctypes, so you should try to avoid them as browsers may perform
incorrectly though.

So your answer is:

Use check(this.form.elements['list[]']) instead of check(this.form.list[])

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 
S

szar

Vjekoslav Begovic said:
szar said:
Call me stupid but that didn't seem to work. Here's the script I'm using,
maybe it'll help:

<SCRIPT LANGUAGE="JavaScript">
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field.checked = true;
}
checkflag = "true";
return "Uncheck All";
} else {
for (i = 0; i < field.length; i++) {
field.checked = false;
}
checkflag = "false";
return "Check All";
}
}
</script>

<form>
<input type=checkbox value="Check All"
onClick="this.value=check(this.form.list)">
<input type="checkbox" name="list" value="thisisdifferentforeach">
<input type="checkbox" name="list" value="thisisdifferentforeach">
<input type="checkbox" name="list" value="thisisdifferentforeach">
.
.
.
</form>

Basically, clicking the top checkbox checks (or unchecks) all the ones
below. This works how it is but I need the checkbox names to be list[]
instead of list. If anyone could edit the above code to give me a better
idea I'd be greatful!!!
Thanks!



<input type=checkbox value="Check All"
onClick="this.value=check(this.form.elements['list[]'])">
<input type="checkbox" name="list[]" value="thisisdifferentforeach">
<input type="checkbox" name="list[]" value="thisisdifferentforeach">
<input type="checkbox" name="list[]" value="thisisdifferentforeach">


That did it! Thanks so much for your help VB!
 
C

Chris Riesbeck

I appreciate all the help but it still doesn't work. I think using elements
was on the right track but appearently I don't know enough about it to get
it to work. Here's what I want to happen:
<form>
<input type=checkbox value="Check All"
onClick="this.value=check(this.form.list[])">
<input type="checkbox" name="list[]" value="thisisdifferentforeach">
<input type="checkbox" name="list[]" value="thisisdifferentforeach">
<input type="checkbox" name="list[]" value="thisisdifferentforeach">

Change to

onClick="this.value=check(this.form['list[]'])">

so Javascript doesn't look for the array list.

But you're violating basic HTML doing this, so I
wouldn't trust this code across browsers. Names

...must begin with a letter ([A-Za-z]) and may be
followed by any number of letters, digits ([0-9]),
hyphens ("-"), underscores ("_"), colons (":"),
and periods (".").

http://www.w3.org/TR/html4/types.html
 

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

Forum statistics

Threads
474,085
Messages
2,570,597
Members
47,219
Latest member
Geraldine7

Latest Threads

Top