/    Sign up×
Community /Pin to ProfileBookmark

checkboxes,arrays & JS syntax….

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
[B]sendid[][/B] 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 [B]“check(this.form.sendid[])[/B] i get a syntax error because JS does not like the [B][][/B] .
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

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Phil_KarrasFeb 28.2003 — Here's a function to clear all checked boxes I used for a different question, change the [b]...checked = false[/b] to [b]...checked = true[/b] 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:
<i>
</i> function ClearChecks() {
var max = document.ckbox.checkboxname.length;
for (var i=0; i&lt;max; i++) {
eval("document.ckbox.checkboxname["+i+"].checked = false");
}
}
Copy linkTweet thisAlerts:
@vronskyauthorFeb 28.2003 — 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

[B]sendid[][/B] with that [B][][/B] that gives me a syntax error....

May be you now how to replace or to arrange the [B][][/B] so

the browser can parse() it correctly???

Thank you..
Copy linkTweet thisAlerts:
@Phil_KarrasFeb 28.2003 — 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.
Copy linkTweet thisAlerts:
@vronskyauthorMar 03.2003 — Thanks Phil,Thanks Dave...

the code

[B]<input type=button value="Check All" onClick="this.value=check(this.form.elements["sendid[]"])"> [/B]

gives still a syntax error,but if I omit the [B]"[/B] like

[B]<input type=button value="Check All" onClick=this.value=check(this.form.elements["sendid[]"])> [/B]

it look like working...

Now I'm going to test it better,but,not being a JS competent,I would like to know why...
×

Success!

Help @vronsky spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...