Home : General : Internet Technologies :

General: Internet Technologies: PHP: Dynamic Checkboxes/Javascript Puzzle: Edit Log

Here is the list of edits for this post
PHP: Dynamic Checkboxes/Javascript Puzzle
Good morning.

This thread is somewhat related to another thread I posted in the Database SQL forum, but this particular problem I have is related to dynamic checkboxes and javascript, more so than the database issue I was experiencing:

http://www.gossamer-threads.com/...i?post=252399#252399

Anyway, I have searched PHPBuilder, DBForums, DevShed, and Google and found some good resources, like:

http://www.phpbuilder.com/...ral/2001101/0326.php

http://www.experts-exchange.com/.../XML/Q_20623429.html

PROBLEM:

In the search form described in the above linked GT thread, I have 15 dynamically generated checkboxes, which I have conveniently named AttributeID[], like 1[]. Now when I change the field name to attributeCode, like hc[], the "Any" checkbox that clears the other selections works fine, but when I use the AttributeID[] construct, the checkboxes do not clear and I get the following javascript error:

Code:
Line: 245
Char: 2
Error: Expected ';'
Code: 0
URL: http://127.0.0.1:8102/search.php?flag=advance

Line 245
sInput.checked = false;

Here are the JS codes I am using:

Code:
// Form checkbox utilities
// Primarily used for Search Profiles Form
function uncheckAll(obj,field,sChkBox) {
var aCheckBoxes = document.forms[0].elements[field];
if (aCheckBoxes.length!=-1) {
for (i = 0; i < aCheckBoxes.length; i++)
aCheckBoxes.checked = false ;
}
checkAnyState(obj,sChkBox);
}
function uncheckSingle(formname,field) {
var aCheckBoxes = formname.elements[field];
if(aCheckBoxes.length!=-1) {
for (i = 0; i < aCheckBoxes.length; i++){
if(aCheckBoxes.value != 50){
aCheckBoxes.checked = false ;
}
}
}
}
function checkAnyState(obj,sChkBox) {
var sInput = eval("document.forms[0]."+ sChkBox);
if(obj.checked == true)
sInput.checked = false;
}

And example of the dynamic checkboxes:

Code:
<B>Body Type:</B><br>
<input type="checkbox" name="1[]" value="1" uncheckSingle(this.form,'1_w');"> Slim/Slender
<br>
<input type="checkbox" name="1[]" value="2" uncheckSingle(this.form,'1_w');"> Normal Weight Range
<br>
<input type="checkbox" name="1[]" value="3" uncheckSingle(this.form,'1_w');"> A few extra pounds
<br>
<input type="checkbox" name="1[]" value="4" uncheckSingle(this.form,'1_w');"> Well-rounded
<br>
<input type="checkbox" name="1[]" value="5" uncheckSingle(this.form,'1_w');"> Solidly built
<br>
<input type="checkbox" name="1[]" value="6" uncheckSingle(this.form,'1_w');"> Other
<br>
<input type="checkbox" name="1_w" value="0" onClick="uncheckAll(this,'1[]','1_w[1]');"> Any

What I'd like to do is maintain using AttributeID[] rather than AttributeCode, since the query works fine right now with the using the following codes:

Code:
if ((isset($_REQUEST['totalattributes'])) and ($_REQUEST['totalattributes'] > 0)) {
for ($i=1;$i<=$_REQUEST['totalattributes'];$i++) {
if (!empty($_REQUEST[$i])) {
if (!is_array($_REQUEST[$i])) {
$_REQUEST[$i] = array($_REQUEST[$i]);
}
$query .= " AND (ua.AttributeID = $i) AND (ua.value IN (";
foreach ($_REQUEST[$i] as $value) {
if (!empty($value)) {
$query .= "$value,";
}
}
$query .= "0))";
}
}
}

Or am I forced to use a text string (AttributeCode) for the checkbox field name and then use explode in the query to get the values into the foreach loop. The issue I have is that I have no clue how to dynamically translate the AttributeCode[] values into an array...the $i variable works better than taking the AttributeID[] and translating those parameters.

Any suggestions would be greatly appreciated.

Thanks in advance.
========================================
Buh Bye!

Cheers,
Me

Last edited by:

Stealth: Sep 18, 2003, 9:14 AM

Edit Log: