/    Sign up×
Community /Pin to ProfileBookmark

Checkbox Select-All script problems

I’m pulling my hair out trying to find a checkbox check-all toggle script that will pass an array of selected to my server side PHP script. I really want a master checkbox that serves as a select-all/deselect-all control.

I’ve looked at countless examples online, and the closest I can find is one below that doesn’t seem to submit anything very useful, the posted data is

[CODE]Array ( [all] => on [checkGroup] => fourth [Submit] => Submit ) [/CODE]

which doesn’t seem very useful when I want to process the results on the server side. I was expecting an array that I could use to build an SQL query.

[code=html]<script type=”text/javascript”>
function checkAll(checkname, exby) {
for (i = 0; i < checkname.length; i++)
checkname[i].checked = exby.checked? true:false
}
</script>

<form name=”form2″ method=”post” action=”?action=evaluate”>
<input type=”checkbox” name=”all” onClick=”checkAll(document.form2.checkGroup,this)”>Check/Uncheck All<br>
<input type=”checkbox” name=”checkGroup” value =”first”>First<br>
<input type=”checkbox” name=”checkGroup” value =”second”>Second<br>
<input type=”checkbox” name=”checkGroup” value =”third”>Third<br>
<input type=”checkbox” name=”checkGroup” value =”fourth”>Fourth<br>
<input type=”submit” name=”Submit” value=”Submit” style=”height:23px;font-weight:bold;padding-top:0px;”>

</form>[/code]

Can anyone help me find a suitable script, or help me modify this one?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@FangMay 28.2010 — Change the name values to an array:name="checkGroup[]"
server side you can read checkGroup as an array.
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;title&gt;&lt;/title&gt;

&lt;script type="text/javascript"&gt;
function checkAll(checkname, exby) {
var action = (exby.checked)? true : false;
var checkname = exby.form.elements[checkname];
for (i = 0; i &lt; checkname.length; i++) {
checkname[i].checked = action;
}
}
&lt;/script&gt;

&lt;style type="text/css"&gt;
* {margin:0;padding:0;}
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;?php
print_r ($_POST['checkGroup']);
?&gt;

&lt;form name="form2" method="post" action="?action=evaluate"&gt;
&lt;input type="checkbox" name="all" onclick="checkAll('checkGroup[]', this)"&gt;Check/Uncheck All&lt;br&gt;
&lt;input type="checkbox" name="checkGroup[]" value ="first"&gt;First&lt;br&gt;
&lt;input type="checkbox" name="checkGroup[]" value ="second"&gt;Second&lt;br&gt;
&lt;input type="checkbox" name="checkGroup[]" value ="third"&gt;Third&lt;br&gt;
&lt;input type="checkbox" name="checkGroup[]" value ="fourth"&gt;Fourth&lt;br&gt;
&lt;input type="submit" name="Submit" value="Submit" style="height:23px;font-weight:bold;padding-top:0px;"&gt;

&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@KorMay 28.2010 — Change the name values to an array:name="checkGroup[]"
server side you can read checkGroup as an array.[/QUOTE]

I know that it works for PHP, but is it applicable in other server-side languages as well?
Copy linkTweet thisAlerts:
@FangMay 28.2010 — Afaik it is only applicable to PHP
Copy linkTweet thisAlerts:
@scaiferwauthorMay 28.2010 — Thank you very much Fang. That did the trick.

My eventual full working solution for those who wish to see it;

[code=php]<?php

$pagetitle = "Checkbox List Test";
include("includes/standard-doc-head.inc.php");



if (isset($_GET['action'])) {
switch (strtolower($_REQUEST['action'])) {
case evaluate:
evaluateForm();

break;
default;
showForm();

break;
}
} else {
showForm();

} // end if (isset($_GET['action']))


function showForm() {
?>
<h3>Test Checkbox List</h3>


<hr /><h4>Form 1</h4>

<script type="text/javascript">
function checkAll(checkname, exby) {
var action = (exby.checked)? true : false;
var checkname = exby.form.elements[checkname];
for (i = 0; i < checkname.length; i++) {
checkname[i].checked = action;
}
}
</script>

<form name="form1" method="post" action="?action=evaluate">
<input type="checkbox" name="all" onclick="checkAll('checkGroup[]', this)">Check/Uncheck All<br />
<input type="checkbox" name="checkGroup[]" value ="value_first">First<br />
<input type="checkbox" name="checkGroup[]" value ="value_second">Second<br />
<input type="checkbox" name="checkGroup[]" value ="value_third">Third<br />
<input type="checkbox" name="checkGroup[]" value ="value_fourth">Fourth<br />
<input type="submit" name="Submit" value="Submit" style="height:23px;font-weight:bold;padding-top:0px;">
</form>

<?php
} // end showForm()


function evaluateForm() {

# print_r($_POST); // debug value

echo "<p>Checked Values:</p>";
$group=$_POST['checkGroup'];
foreach ($group as $value) {
echo "$value<br />";
}

} // end evaluateForm()


// close page structure
include("includes/standard-doc-foot.inc.php");

?>[/code]
×

Success!

Help @scaiferw 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.18,
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,
)...