|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
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!!! ![]() |
|
#2
|
||||
|
||||
|
I'm pretty sure you can't use [] in variables names - they are reserved for array's.
|
|
#3
|
|||
|
|||
|
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?!! |
|
#4
|
||||
|
||||
|
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.
|
|
#5
|
|||
|
|||
|
so can i remove the "answer_7[]" part?
does it automatically add the commas? cos I am doing that in PHP!!?!! |
|
#6
|
||||
|
||||
|
yeah - remove the []'s.
Just do a print on what you get back from the checkbox collection to find out how it's delivered. |
|
#7
|
|||
|
|||
|
cool, cheers dude, I will try that tomorrow morning!! thanks!
![]() |
|
#8
|
|||
|
|||
|
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!! |
|
#9
|
|||
|
|||
|
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:
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! |
|
#10
|
||||
|
||||
|
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. |
|
#11
|
|||
|
|||
|
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 />"; } } |
|
#12
|
||||
|
||||
|
nahemah - you might have more luck getting a response if you post this in the PHP forum.
|
|
#13
|
||||
|
||||
|
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.
|
|
#14
|
|||
|
|||
|
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. |
|
#15
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > checkbox array problem...[ ] |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |