Gossamer Forum
Home : General : Internet Technologies :

PHP: Dynamic Checkboxes/Javascript Puzzle

Quote Reply
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
Quote Reply
Re: [Stealth] PHP: Dynamic Checkboxes/Javascript Puzzle In reply to
Can you even use arrays such as $1? I though digits and stuff like that were reserved? i.e.

$1[]
$2[]
$3[]
$4[]

...etc

May be wrong... (doubt this has anything to do with your problem, but i thought I would just point it out).

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Stealth] PHP: Dynamic Checkboxes/Javascript Puzzle In reply to
While this post is *way* over my head I couldn't help but notice...

if(obj.checked == true) sInput.checked = false;


shouldn't there be brackets { } involved?
Quote Reply
Re: [Andy] PHP: Dynamic Checkboxes/Javascript Puzzle In reply to
Thanks, Andy....

I do think that using digits is the problem, but even if I add a character in front the form name value, I'd still have problems getting those values in the search query statement. I did try:

attribute_1[]

and while the checkbox clearing worked, I could not figure out how to "explode" those values correctly in the SQL statement.
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Re: [Watts] PHP: Dynamic Checkboxes/Javascript Puzzle In reply to
Thanks, Watts.

Good thought...although in javascript, it's not necessary to enclose conditional statements in brackets. So, syntaxically speaking, the codes are correct, and do work when I don't use numbers in the form checkbox name parameters.

Just figuring how I can use the number characters in the checkbox name parameters or figure out how to re-code the SQL statement to function the same as it does now, but work with form names like hc[]....
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Re: [Stealth] PHP: Dynamic Checkboxes/Javascript Puzzle In reply to
Quote:
attribute_1[]

and while the checkbox clearing worked, I could not figure out how to "explode" those values correctly in the SQL statement.

That should work. I have done stuff like this before;

$_1[]
$_2[]
$_3[]

..etc.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] PHP: Dynamic Checkboxes/Javascript Puzzle In reply to
Okay...may be one piece of information I neglected to add is that the search query is conditional, meaning that it checks to see if those checkboxes are checked and then pulls the values out of the array and puts them into the IN function in the SQL statement.

How have you "done it" before? I understand how to put a numberical field value into a printable array and a foreach loop....did you use explode or preg_match?
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Re: [Stealth] PHP: Dynamic Checkboxes/Javascript Puzzle In reply to
>>>How have you "done it" before? I understand how to put a numberical field value into a printable array and a foreach loop....did you use explode or preg_match? <<<

I did it the long way. Not sure about the field structure compared to yours.Mine just needed all the values put in as one string, so I used a foreach loop....

Code:
$joined = join(",",$_1); // where $_1 is the array...

Then, just a normal SQL query;

INSERT INTO tbl_Name ($Username,$joined)

That any help to you? Unsure

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!