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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkboxes, Javascript and passing to PHP

Status
Not open for further replies.

ID10T16

Technical User
Sep 22, 2000
160
US
I've scoured all over the net to find out any help I can to try to get this to work. Currently I have a list of checkboxes like this...

***
<table width="610">
<tr>
<td width="25%">
<Form name="advanced" align="left" action="<?php
echo $_SERVER['PHP_SELF'];
?>">
<input type="checkbox" name="list[]" value="bedroom"/>
Bedroom Suites<br/>
<input type="checkbox" name="list[]" value="upholstery"/>
Upholstery<br/>
<input type="checkbox" name="list[]" value="dining"/>
Dining Room<br/>
<input type="checkbox" name="list[]" value="entertainment"/>
Entertainment<br/>
<input type="checkbox" name="list[]" value="office"/>
Office<br/>
</td>
<td width="75%">
<input type="checkbox" name="list[]" value="occasionals"/>
Occasionals<br/>
<input type="checkbox" name="list[]" value="youth"/>
Youth<br/>
<input type="checkbox" name="list[]" value="futons"/>
Futons/Bunkbeds<br/>
<input type="checkbox" name="list[]" value="waterbeds"/>
Waterbeds<br/>
<input type="checkbox" name="list[]" value="misc"/>
MISC.<br/>
</td>
</tr>
<input type="button" value="Check All" onClick="this.value=check(document.advanced.list)" method="get">
</td></tr>
***

I was told that the name needed to have the brackets at the end so PHP could increment the names and thus have different variables instead of one array.

Here is the submit form button...

<input type="submit" method="post" onClick="this.value=checkbox_checker(document.advanced.list)" name="phpselector" value="Search" />

And here is the javascript that I'm using that is pulled from file...
***
var checkflag = "false";
function check(field)
{
if (checkflag == "false")
{
for (w = 0; w < field[w].length; w++)
{
field[w].checked = true;
}
checkflag = "true";
return "Uncheck All";
}
else
{
for (w = 0; w < field[w].length; w++)
{
field[w].checked = false;
}
checkflag = "false";
return "Check All";
}
}

function checkbox_checker(fielda)
{
// set var tabletest to false
var tabletest = false;
// Loop from zero to the one minus the number of radio button selections
for ( q = 0; q < fielda[q].length; q++)
{
// If a radio button has been selected it will return true
// (If not it will return false)
if (fielda[q].checked)
tabletest = true;
}
if (!tabletest)
{
// If there were no selections made display an alert box
alert("Please Select a department to search in")
return false;
}
return "Verified";
}
***


As I'm sure you guessed, this is for a furniture website.

This is part of the advanced search form. Basically, I want customers to be able to check one or all of the departments (actually tables) that they want to search in, and then type in whatever search criteria they want. Each checkbox corresponds to a table name which will be passed via the URL to the same page that will then be used in the query to find results.

It all works fine if I take the brackets off the list[] name in the form and leave the javascript as something like field.length (instead of field[w].length), but it seems like it all is listed under the same name.

When I use the brackets, javascript apparently doesn't like that.

I'm a bit rusty on my javascript, and green with PHP. The code you see above is my latest brain fart at an attempt to trial and error my way to a solution. I have since determined I may require the assistance and expertise of some higher powers.

Thank you for any help or direction anyone can offer.
 
If you want to use Javascript to access the form elements, I'd recommend getting rid of the square brackets. Those aren't valid characters in form element names anyway. Whoever told you to create the HTML this was didn't give you good information.

Your question probably belongs in the Javascript forum more than the PHP one, but you'll get the same answer there.

forum216

Lee
 
trollacious:
Using the pseudo-array names for field names is documented in the PHP manual here: There's another place where it's documented, too, but I can never find it when I need it.

ID10T16
The very next question on the above page ( will tell you how to work around this. Assign each HTML field element an ID attribute (<input type=....id="someid"...>). You can then use the ID attribute to access a form field just as you do its name.


Want the best answers? Ask the best questions! TANSTAAFL!
 
@ID10T16

an easier way than assigning id's for a simple on/off toggle is the following javascript.

Code:
<script type="text/javascript">
function togglecbs(val) {
    var frm = document.getElementByName("advanced");
    for (var i = 0; i<frm.elements.length; i++) {
        if ((frm.elements[i].name.indexOf('list') > -1)) {
            frm.elements[i].checked = val;
        }
    }
}
</script>

create a checkbox that is NOT called "list[]" that you check to check all the other checkboxes and vice versa. something like this:
Code:
<input type="checkbox" name="all" value="select all" onclick="togglecbs(this.checked);"/>&nbsp;(Un)Check All<br/>
 
According to W3C,

ID and NAME tokens 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 (".")

If the PHP manual suggests using square brackets in names for form elements, then they are not following W3C standards for HTML 4.0


Lee
 
@lee

your url reference to w3c may be a bit misleading. I think you may need to dig further into the spec.

so far as I know, the name attribute of (eg) the <input> tag is not actually a name token but a CDATA input type. square brackets are allowed in CDATA (as are many other characters).

check out:

and contrast with

hth
Justin
 
Every reference you listed says EXACTLY the same as what I have quoted in my last post. In fact one URL you provided is the same as the one I provided, and the quote is copied and pasted from the segment on CDATA.

What I'm reading is that the name (or any other CDATA entity) must begin with a letter A-Z or a-z, and can be followed by any of these letters, any digit 0-9, or the punctuation symbols hyphen (-), underscore (_), colon :)), or period (.).

I hope you're not misinterpreting the representation in the CDATA quote with the square brackets enclosing the digits and letters as meaning the brackets are acceptable characters, since in none of the instances where those brackets are used to signify a range would a square bracket fit the definition of "letter" or "digit"

Lee
 
trollacious:
The CDATA token type is completly different from the NAME and ID token types. You can see this from the typography of the spec document. The NAME and ID token types are on the same indentation level as CDATA, implying they are independent definitions, not dependent definitions of specific cases.

It is also incorrect to think that an element's "name" attribute must be of token type NAME. I direct your attention to the definition of the META element ( In that element the "name" attribute is specifically defined to be of type NAME.

In all form elements, the "name" attribute is of type CDATA.

And CDATA tokens are not as constrained as NAME or ID tokens. The brackets are perfectly legal characters in the names of form elements.


Want the best answers? Ask the best questions! TANSTAAFL!
 
Okay, I see my error now. Sorry about that. I had scrolled my browser window down to the point I didn't see that CDATA and NAME types were the same level.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top