By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,299 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,299 IT Pros & Developers. It's quick & easy.

Arrays as form elemtents

P: n/a
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.
Jul 20 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
"szar" <no**@nowhere.net> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
"szar" <no**@nowhere.net> wrote in message
news:zl***************@newssvr24.news.prodigy.com. ..
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.
Jul 20 '05 #3

P: n/a

"Richard Cornford" <ri*****@litotes.demon.co.uk> wrote in message
news:bj**********@sparta.btinternet.com...
"szar" <no**@nowhere.net> wrote in message
news:zl***************@newssvr24.news.prodigy.com. ..
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[i].checked = true;
}
checkflag = "true";
return "Uncheck All";
} else {
for (i = 0; i < field.length; i++) {
field[i].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!
Jul 20 '05 #4

P: n/a
this helpfull ?

<SCRIPT LANGUAGE="JavaScript">
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;
}
checkflag = "true";
return "Uncheck All";
} else {
for (i = 0; i < field.length; i++) {
field[i].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" <lr*@hotpop.com> wrote in message
news:4q**********@hotpop.com...
"szar" <no**@nowhere.net> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'

Jul 20 '05 #5

P: n/a
added 3rd option
<SCRIPT LANGUAGE="JavaScript">
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;
}
checkflag = "true";
return "Uncheck All";
} else {
for (i = 0; i < field.length; i++) {
field[i].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" <lr*@hotpop.com> wrote in message
news:4q**********@hotpop.com...
"szar" <no**@nowhere.net> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'

Jul 20 '05 #6

P: n/a
> var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].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

Jul 20 '05 #7

P: n/a

"Douglas Crockford" <no****@laserlink.net> wrote in message
news:bj**********@sun-news.laserlink.net...
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].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.
Jul 20 '05 #8

P: n/a

"ziemon" <ne**@ziemon.nl> wrote in message
news:bj**********@news1.tilbu1.nb.home.nl...
added 3rd option
<SCRIPT LANGUAGE="JavaScript">
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;
}
checkflag = "true";
return "Uncheck All";
} else {
for (i = 0; i < field.length; i++) {
field[i].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" <lr*@hotpop.com> wrote in message
news:4q**********@hotpop.com...
"szar" <no**@nowhere.net> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'



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[i].checked = true;
}
checkflag = "true";
return "Uncheck All";
} else {
for (i = 0; i < field.length; i++) {
field[i].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!
Jul 20 '05 #9

P: n/a
"szar" <no**@nowhere.net> wrote
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[i].checked = true;
}
checkflag = "true";
return "Uncheck All";
} else {
for (i = 0; i < field.length; i++) {
field[i].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">
Jul 20 '05 #10

P: n/a
szar wrote:
"ziemon" <ne**@ziemon.nl> wrote in message
news:bj**********@news1.tilbu1.nb.home.nl...
added 3rd option
<SCRIPT LANGUAGE="JavaScript">
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;
}
checkflag = "true";
return "Uncheck All";
} else {
for (i = 0; i < field.length; i++) {
field[i].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" <lr*@hotpop.com> wrote in message
news:4q**********@hotpop.com...
"szar" <no**@nowhere.net> writes:

> 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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'



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[i].checked = true;
}
checkflag = "true";
return "Uncheck All";
} else {
for (i = 0; i < field.length; i++) {
field[i].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 <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_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-deve...upgrade_2.html
Jul 20 '05 #11

P: n/a

"Vjekoslav Begovic" <vj*******@inet.hr> wrote in message
news:bj**********@sunce.iskon.hr...
"szar" <no**@nowhere.net> wrote
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[i].checked = true;
}
checkflag = "true";
return "Uncheck All";
} else {
for (i = 0; i < field.length; i++) {
field[i].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!
Jul 20 '05 #12

P: n/a
In article <0s**********@newssvr24.news.prodigy.com>,
"szar" <no**@nowhere.net> wrote:

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
Jul 20 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.