Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JavaScript and PHP

Status
Not open for further replies.

ksoong

Programmer
Oct 3, 2000
57
CA
Hi everyone. i'll also be posting this in the javascript forum.

I have a bunch of check boxes in my html. the name list[] was given so that the PHP script it would be directed to could grad the values out of the checkbox using an array.

<form name=myform>
<input type=checkbox name=list[] value=&quot;1&quot;>1<br>
<input type=checkbox name=list[] value=&quot;2&quot;>2<br>
<input type=checkbox name=list[] value=&quot;3&quot;>3<br>
<input type=checkbox name=list[] value=&quot;4&quot;>4<br>
<input type=checkbox name=list[] value=&quot;5&quot;>5<br>
</form>

as you can see the name is list[]

But how can i reference the list[] checkbox using javascript? if anyone has experience doing this.

here is my code so far which i've experiented with.

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!-- Begin

var field=document.myform.elements[0];
alert(document.myform.elements[&quot;list[]&quot;][0].name);
// tried document.myform.list[][0].name
// tried document.myform.'list[]'[0].name
// tried document.myform.elements[0][0].name
// End -->
</script>


any help would be appreciated thanks.


 
Get rid of the square brackets, it is confusing the browser. Just call the collection list, then list[0].value etc will work.



;-)
 
bangers-- unfortunately that will not work for PHP, even though it works for Javascript.

The only real solution I can see here is to explicitly set your array, instead of &quot;letting it index itself&quot;:

Code:
<form name=myform method=post action=&quot;phpinfo.php&quot;>
<input type=checkbox name=list[1] value=&quot;1&quot;>1<br>
<input type=checkbox name=list[2] value=&quot;2&quot;>2<br>
<input type=checkbox name=list[3] value=&quot;3&quot;>3<br>
<input type=checkbox name=list[4] value=&quot;4&quot;>4<br>
<input type=checkbox name=list[5] value=&quot;5&quot;>5<br>
<input type=&quot;submit&quot; onclick=&quot;clickit()&quot;>
</form>

This will produce an array that PHP can deal with, while the syntax I give you here for Javascript will allow Javascript to treat each one as a unique name
Code:
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>

function clickit(){
alert(document.myform('list[1]').name);
alert(document.myform('list[2]').value);
//etc...
}
//  End -->
</script>
This syntax makes Javascript delimit the form element name with a different character than the element's own index delimiter. Using single quotes is generally the way to tell a programming language not to try to interpret what is inside them, but to simply treat it as a string.

The reason I direct the form to phpinfo.php is so that you can create a page called phpinfo.php with:
Code:
<?php
phpinfo();
?>

When you submit your form to this page, you will see your array of checkbox variables (in the PHP Variables section). This time the array will not be dynamic: if you check the first, third, and fifth checkbox you will have variables $list[1], $list[3], and $list[5], instead of $list[0], $list[1], and $list[2].

Catch my drift?

P.S. Also, I would steer clear of using form element names like &quot;list&quot;, because it sounds to me like it could be a reserved word, either in Javascript or PHP. It shouldn't technically be a problem, but I find it good practice to make my form element names and variable names as unique and descriptive as possible, thereby avoiding the likelihood of namespace conflicts.

HTH ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top