JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 3rd, 2003, 11:19 AM
fakker fakker is offline
The calm b4 the storm
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Manchester, UK
Posts: 404 fakker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 17
Send a message via Yahoo to fakker
checkbox array problem...[ ]

hey,

I have some HTML which shows a list of checkboxes:

Code:
<input type="checkbox" name="answer_7[ ]" value="bn" class="textbox">bn<BR>
<input type="checkbox" name="answer_7[ ]" value="k" class="textbox">k<BR>
<input type="checkbox" name="answer_7[ ]" value="j" class="textbox">j<BR>
<input type="checkbox" name="answer_7[ ]" value="hy" class="textbox">hy<BR>


the reason they have name="answer_7[]" is because I am reading the answer in to an array using PHP once the answers have been submitted.

That all works fine.... but when I come to validate the items, I use the following code:

Code:
radio_error = "false";
for (var i=0;i<=3;i++) {
	if (eval("document.frm.answer_7[][" + i + "].checked == true")) {
radio_error = "true";
	}
}
if (radio_error != "true") {
		error_text += "- Question 7 must be completed\n";
		error_count = error_count + 1;
}


But when I run the validation, it returns the following error message:

"document.frm.answer_7.0" is null or not an object

Now, I guess this is becuase in the HTML the name of the checkbox is "answer_7[ ]" ... but when I put that as the answer as above it shows a syntax error.....

any help is much appreciated!!!
__________________
Matt 'Fakker' Facer

mattfacer.com

Reply With Quote
  #2  
Old March 3rd, 2003, 04:20 PM
stumpy's Avatar
stumpy stumpy is offline
May contain nuts.
Dev Articles Regular (2000 - 2499 posts)
 
Join Date: Aug 2002
Posts: 2,056 stumpy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 44 m 22 sec
Reputation Power: 0
I'm pretty sure you can't use [] in variables names - they are reserved for array's.

Reply With Quote
  #3  
Old March 3rd, 2003, 06:30 PM
fakker fakker is offline
The calm b4 the storm
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Manchester, UK
Posts: 404 fakker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 17
Send a message via Yahoo to fakker
yeah, thats my problem.... i have made the name of the ceckbox "name=something[]" so that it will be an array when the PHP calls it....

i just needed to know what (if any) solution there was to validating that code?!!

Reply With Quote
  #4  
Old March 3rd, 2003, 06:37 PM
stumpy's Avatar
stumpy stumpy is offline
May contain nuts.
Dev Articles Regular (2000 - 2499 posts)
 
Join Date: Aug 2002
Posts: 2,056 stumpy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 44 m 22 sec
Reputation Power: 0
With checkboxs, if many items are selected, they are passed back when requested as a string separated by commas. Simply use the split() function to turn them into an array.

Reply With Quote
  #5  
Old March 3rd, 2003, 06:43 PM
fakker fakker is offline
The calm b4 the storm
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Manchester, UK
Posts: 404 fakker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 17
Send a message via Yahoo to fakker
so can i remove the "answer_7[]" part?

does it automatically add the commas? cos I am doing that in PHP!!?!!

Reply With Quote
  #6  
Old March 3rd, 2003, 07:13 PM
stumpy's Avatar
stumpy stumpy is offline
May contain nuts.
Dev Articles Regular (2000 - 2499 posts)
 
Join Date: Aug 2002
Posts: 2,056 stumpy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 44 m 22 sec
Reputation Power: 0
yeah - remove the []'s.

Just do a print on what you get back from the checkbox collection to find out how it's delivered.

Reply With Quote
  #7  
Old March 3rd, 2003, 07:14 PM
fakker fakker is offline
The calm b4 the storm
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Manchester, UK
Posts: 404 fakker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 17
Send a message via Yahoo to fakker
cool, cheers dude, I will try that tomorrow morning!! thanks!

Reply With Quote
  #8  
Old March 4th, 2003, 04:10 AM
fakker fakker is offline
The calm b4 the storm
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Manchester, UK
Posts: 404 fakker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 17
Send a message via Yahoo to fakker
hey, I tried removing the [] to avoid the array.... but it was not adding the options... ie: if you checked options x, y and Z.... it was only adding "Z" to the DB.....

BUT..... I managed to find a solution...... here's how you can validate a named array checkbox eg: name="food[]"

Code:
document.formname["food[]"][i].checked


obviously you would need a loop to incriment the "i"....
but that now works perfectly!! serweet!!

Reply With Quote
  #9  
Old March 11th, 2003, 02:37 PM
adamm115 adamm115 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 3 adamm115 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I had a similar problem. If what I understand from your problem is that your wanting to pass multiple values (checkboxes) through to a processing script for instance?

If so () you need to use a multidimensional array. So for instance you have the following in a file:

Code:
<input type="checkbox" name="arrayname[]" value="a">
<input type="checkbox" name="arrayname[]" value="a">
<input type="checkbox" name="arrayname[]" value="a">


Then to access all the information in the array you'd do the following:

PHP Code:
foreach($_POST['arrayname'] as $key=>$value) {
echo 
"Checkbox $key$value";



Something along those lines anyways :P

You just use foreach() to loop through all the values in the array!

Hope this helps a bit!

*EDIT*
Well after re-reading the thread I have realised this probably isn't going to help your problem much so sorry!

Reply With Quote
  #10  
Old March 11th, 2003, 05:15 PM
stumpy's Avatar
stumpy stumpy is offline
May contain nuts.
Dev Articles Regular (2000 - 2499 posts)
 
Join Date: Aug 2002
Posts: 2,056 stumpy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 44 m 22 sec
Reputation Power: 0
I can't be sure, but i'd guess that form values are passed back the same on every platform.

With checkbox's - only the checked values are passed back.

Say your form has 4 check box's named "timeslot". Say you tick 2 boxs: "1PM", "4AM"

When you request the checkbox object with your severside script (in ASP it's request.form("timeslot")) you will get one string:
'1PM, 4AM' - without the quotes. So a comma separate list.

Reply With Quote
  #11  
Old December 31st, 2003, 04:05 PM
nahemah nahemah is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: southern California
Posts: 2 nahemah User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to nahemah Send a message via AIM to nahemah Send a message via Yahoo to nahemah
checkbox array

I'm not sure if anyone is still keeping an eye on this post, but I have the following PHP code, and upon submit, the form indicates an error on the echo line of the foreach() loop. If anyone knows why this is happening, please let me know.. it's a bit frustrating.

if ($_POST["skill"].checked) {
foreach($_POST["skill"] as $key=>$value) {
echo "Checkbox ".$key.": ".$value." <br />";
}
}

Reply With Quote
  #12  
Old December 31st, 2003, 05:04 PM
stumpy's Avatar
stumpy stumpy is offline
May contain nuts.
Dev Articles Regular (2000 - 2499 posts)
 
Join Date: Aug 2002
Posts: 2,056 stumpy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 44 m 22 sec
Reputation Power: 0
nahemah - you might have more luck getting a response if you post this in the PHP forum.

Reply With Quote
  #13  
Old January 1st, 2004, 03:25 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 17
Send a message via ICQ to dhouston
Actually, an answer here's appropriate too, I think, stumpy. Nahemah, you're mixing PHP and javascript. The ".checked" is a javascript snippet and the rest is PHP. Because the dot operator in PHP is an entirely different thing than it is in js, PHP's seeing ".checked" and doesn't know what you're trying to do. Change that line to something like "if(sizeof($_POST["skill"]) > 0){" or even just "if($_POST["skill"]){" and see if that does the trick for you.

Reply With Quote
  #14  
Old June 25th, 2004, 02:55 AM
nahemah nahemah is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: southern California
Posts: 2 nahemah User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to nahemah Send a message via AIM to nahemah Send a message via Yahoo to nahemah
finally got an answer..

I am pretty sure that I finally got this solved. I took the .checked out after I realized that I had mixed js with the php --silly me.. anyhow, i totally forgot that i even had an account, and that I had posted this until a friend did a web search for 'nahemah'...

so i got rid of javascript entirely... it seemed to fix a lot of my problems by going directly to the _POST array and pulling all set variables that had been passed in the form.. then after the 'foreach' it was a piece of cake.

i ended up writing a sieve to handle some parts of the processing, and did everything else in php.. sorry for muffing the js section of this, not sure what i was thinking posting it here.. guess i should do a little more poking around.

Reply With Quote
  #15  
Old June 25th, 2004, 07:43 AM
vetrivel vetrivel is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 1 vetrivel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Post to create one js file in javascript

i want to create one js file which one containing all validations(radio buttons and checkboxes) in javascript.
i want call from any html file to js file which i have to be created. The arguments only i give in html file.

using any creating instance of function in js file or what ever it may be.

i should change only in html file in calling functions. it should call according values in js file.
this should apply to any html file.

Anyone help me.

Thanks in advance

Reply With Quote
  #16  
Old March 25th, 2009, 10:13 PM
chewcheeboon chewcheeboon is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 1 chewcheeboon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 m 4 sec
Reputation Power: 0
thank for the idea

PHP Code:
<html >
<
head>
<
meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<
title>serweet!!
</
title>
<
script type="text/javascript"
function 
is_at_least_one_checkform_object,checkbox_name 

    
// Checking if at least one period button is selected. Or not. 
    

    
var total=0;
    for(var 
i=0form_object[checkbox_name].lengthi++)
    {
        if(
form_object[checkbox_name][i].checked)
        {
            return 
true;
        }
        
    }
    
    
alert("Pls be grateful. At least check one of the checkbox!") ;    
    
    return 
false
</script>

</head>

<body>
<form name="form1" method="post" action="<?=$PHP_SELF;?>" onsubmit='is_at_least_one_check(this,"scripts[]")'>
<table border='0' width='50%' cellspacing='0' cellpadding='0' >
<tr>
<td align=center bgcolor='#f1f1f1'>
<font face='verdana' size='2'><b>Thank to [Fakker] for the idea</b>
<input type="checkbox" name="scripts[]" value='thanks lot'>thanks a lot 
<input type="checkbox" name="scripts[]" value='i am grateful'>i am grateful 
<input type="checkbox" name="scripts[]" value='you are my saviour'>you are my saviour</td>
</tr> 

<tr bgcolor='#ffffff'><td align=center ><input type=submit value=Submit> <input type=reset value=Reset>
<?php 
if(is_array($_POST['scripts']))
{
?><br /> To [Fakker]
<?php 
    
foreach( $_POST['scripts'] as $selected_script )
    {
        
?><br /><?=$selected_script;?>
        <?php
    
}
}
?>
</td></tr> 
</table>
</form> 


</body>
</html> 

Reply With Quote
  #17  
Old January 17th, 2010, 10:33 PM
karthick_icosy karthick_icosy is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2010
Posts: 1 karthick_icosy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 m 6 sec
Reputation Power: 0
Smile

Hi Friend,
That's working fine thanks for your help.It will Reduce my work using Array.Once again Thanks a lot

Regards,
Karthick

Quote:
Originally Posted by fakker
hey, I tried removing the [] to avoid the array.... but it was not adding the options... ie: if you checked options x, y and Z.... it was only adding "Z" to the DB.....

BUT..... I managed to find a solution...... here's how you can validate a named array checkbox eg: name="food[]"

Code:
document.formname["food[]"][i].checked


obviously you would need a loop to incriment the "i"....
but that now works perfectly!! serweet!!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > checkbox array problem...[ ]


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap