Click to See Complete Forum and Search --> : checkboxes,arrays & JS syntax....


vronsky
02-28-2003, 10:41 AM
Hello to all the community,I'm really brand new....

Pls :

I have a form that post data to a php script by checkboxes.
Iwould like to introduce a button that check all the boxes,but...

1 <head>
2 //here the JS script works fine
3 <head>
4 <body>
5 <form method="post" action="myscript.php">
6 <input type="checkbox" name="sendid[]" value="Jim">
7 <input type="checkbox" name="sendid[]" value="John">
8 <input type="checkbox" name="sendid[]" value="Mary">
9 <input type=button value="Check All" onClick="this.value=check(this.form.sendid[])">
10 <input type="submit" name="send_data" value="data">
11 </form>

Now,the values of checkboxes have to be like
sendid[] because I need to pass an array to my script to be counted() and then imploded(),but if the action of my "Check All" button call the function "check(this.form.sendid[]) i get a syntax error because JS does not like the [] .
Anyway,the JS script run well if I omit the [] but of course in this case the form is not going to pass an array to the PHP script.
Do anybody has idea about the way I can solve this problem??

Thank you very much.

Massimo

Phil Karras
02-28-2003, 11:11 AM
Here's a function to clear all checked boxes I used for a different question, change the ...checked = false to ...checked = true and it should then do what you want.

You will also need to change the names of the form and the checkboxes in my function to match yours & of course you'll want to change the name of the function:

function ClearChecks() {
var max = document.ckbox.checkboxname.length;
for (var i=0; i<max; i++) {
eval("document.ckbox.checkboxname["+i+"].checked = false");
}
}

vronsky
02-28-2003, 11:31 AM
Sorry Phil,but my problem is not in in the JS function

var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;}
checkflag = "true";
return "Uncheck All"; }
else {
for (i = 0; i < field.length; i++) {
field[i].checked = false; }
checkflag = "false";
return "Check All"; }
}

that works fine,but in the name of the checkboxes that ,being an array,and posting to a PHPscript has to be like
sendid[] with that [] that gives me a syntax error....
May be you now how to replace or to arrange the [] so
the browser can parse() it correctly???
Thank you..

Phil Karras
02-28-2003, 12:51 PM
OK, then that's not a JS question at all.

I have a test site that I have a PHP script on that shows what was posted to it.

Now, I know of no way to make the check boxes into an array that would be sent to the site.

On the other hand, you can use a hidden field to concatenate all the values and once on the php side use split to put them all into an array. Use "|" or something that will NOT be in any value to be the split char. Since that's now a php question you'll have to go elsewhere for how to do that if you don't know.

As to building the long string if you have problems ask again.

vronsky
03-03-2003, 03:20 AM
Thanks Phil,Thanks Dave...
the code
<input type=button value="Check All" onClick="this.value=check(this.form.elements["sendid[]"])">
gives still a syntax error,but if I omit the " like
<input type=button value="Check All" onClick=this.value=check(this.form.elements["sendid[]"])>
it look like working...
Now I'm going to test it better,but,not being a JS competent,I would like to know why...