simonphoenix
asked on
Double function for a checkbox
I use PHP to create a list with checkboxes from a mySQL database that all have the same name. I want to post the values when submitting, and this works fine if the name of the checkboxes is e.g. list[]. In context: <input type=checkbox name=list[] onClick=countBox(this.form ) value=$results[email]>.
Then I wanted to count the number of checked boxes, but that required using a static name like (name=check). The script handling the counting is this:
function countBox(form){
var total = 0;
var max = form.check.length;
for (var idx = 0; idx < max; idx++) {
if (eval(document.playerliste .check[idx ].checked) == true) {
total += 1;}}
document.playerliste.count er.value = total;}
Working sample at: http://www.multimedia.au.dk/~jsim/php/playerlist(min).php
The question is how to post the checked data (email) and still be able to count the number of checked boxes? The javascript (countBox) use document.playerliste.check [idx].chec ked. If it would only be possible to sustitute .check[idx].checked with something like .liste[][idx].checked everything would be a.o.k. But I don't know how to handle the mixture of javascript and php arrays (or whatever liste[] is).
Thanks in advance, Jesper.
Then I wanted to count the number of checked boxes, but that required using a static name like (name=check). The script handling the counting is this:
function countBox(form){
var total = 0;
var max = form.check.length;
for (var idx = 0; idx < max; idx++) {
if (eval(document.playerliste
total += 1;}}
document.playerliste.count
Working sample at: http://www.multimedia.au.dk/~jsim/php/playerlist(min).php
The question is how to post the checked data (email) and still be able to count the number of checked boxes? The javascript (countBox) use document.playerliste.check
Thanks in advance, Jesper.
ASKER
I know about the gap between server-side and client-side in the two scripts. I could make an onSubmit function checking the checked boxes, but the user/coach is supposed to select a certain amount of players and therefore I want him to see the total count WHILE selecting. I'm trying to reduce the amount of annoyance (extra clicking), not increasing it.
Instead, would it be possible to make a function with images instead of checkboxes? Like first click changes image to a "checked box", second unchecks it, and still be able to post the "checked" data?
Instead, would it be possible to make a function with images instead of checkboxes? Like first click changes image to a "checked box", second unchecks it, and still be able to post the "checked" data?
I am assuming you are using the checkboxes to indicate the players the coach has selected. In this case, go ahead and name them all the same like you have, just give them a unique value representing the player id in the database.
Then you can check the count of the checkboxes easily like you know and the post of the checkboxes into the page will contain an array where the values are the selected players.
You can use these values to make the updates you need to the database, or whatever else you need to do.
davlun
Then you can check the count of the checkboxes easily like you know and the post of the checkboxes into the page will contain an array where the values are the selected players.
You can use these values to make the updates you need to the database, or whatever else you need to do.
davlun
I do this quite often, it's perhaps not the most efficient way of doing it, but it works! Here's a rough example (I've not tested it, so you might need to make small corrections):
$sql = "SELECT userID, username FROM users ORDER BY username";
$result = mysql_query($sql, $db_connection);
echo "<form action=\"show_users.php\" method=\"post\">";
$index = 0;
while ($row = mysql_fetch_array($result) ) {
echo "<input type=\"checkbox\" name=\"user[$index]\" onClick=\"javascriptFuncti on(this)\" ><input type=\"hidden\" name=\"userID[$index]\" value=\"" . $row['userID'] . "\">" . $row['username'];
$index++;
}
echo "<input type=\"submit\" value=\"Next Page\"><input type=\"hidden\" name=\"total_records\" value=\"" . mysql_num_rows($result) . "\"></form>";
// This next part should be on the receiving page (which I've named in my code as "show_users.php":
// concatenate into SQL
for ($index = 0; $index < $total_records; $index++) {
if (isset($user[$index])) {
$sql .= "OR userID = \"" . $userID . "\"";
}
}
// Add first part of SQL statement, remove first "OR" via substring function and add "ORDER BY";
$sql = "SELECT email FROM users WHERE " . substr($sql, 3) . " ORDER BY username";
// query your database
$result = mysql_query($sql, $db_connection);
// show results
while ($row = mysql_fetch_array($result) ) {
echo $row['email'] . "\n<br>\n";
}
############
# Summary #
############
I think this allows you to do what you need. You could add a javascript function with an "onSubmit" to ensure you selected at least one checkbox (which is how I would do it), or you could do an "if (mysql_num_rows($result) == 0)" on the receiving page.
I hope all this helps! d:-)
M@
$sql = "SELECT userID, username FROM users ORDER BY username";
$result = mysql_query($sql, $db_connection);
echo "<form action=\"show_users.php\" method=\"post\">";
$index = 0;
while ($row = mysql_fetch_array($result)
echo "<input type=\"checkbox\" name=\"user[$index]\" onClick=\"javascriptFuncti
$index++;
}
echo "<input type=\"submit\" value=\"Next Page\"><input type=\"hidden\" name=\"total_records\" value=\"" . mysql_num_rows($result) . "\"></form>";
// This next part should be on the receiving page (which I've named in my code as "show_users.php":
// concatenate into SQL
for ($index = 0; $index < $total_records; $index++) {
if (isset($user[$index])) {
$sql .= "OR userID = \"" . $userID . "\"";
}
}
// Add first part of SQL statement, remove first "OR" via substring function and add "ORDER BY";
$sql = "SELECT email FROM users WHERE " . substr($sql, 3) . " ORDER BY username";
// query your database
$result = mysql_query($sql, $db_connection);
// show results
while ($row = mysql_fetch_array($result)
echo $row['email'] . "\n<br>\n";
}
############
# Summary #
############
I think this allows you to do what you need. You could add a javascript function with an "onSubmit" to ensure you selected at least one checkbox (which is how I would do it), or you could do an "if (mysql_num_rows($result) == 0)" on the receiving page.
I hope all this helps! d:-)
M@
The following snippet works for the client-side, whatever you need to do on the server seems to be clear, counting the number of players and getting their mailaddress is simply dont with the $list() array you get when the form's been sent.
If I got you right, youjust want a visual feedback for the user while she/he's choosing a playerlist. You won't get around to create a onSubmit() funtion if wou have a minimum of players, but you can use the NUM_PLAYERS variable and prevent sending the form if the value'S to low/high.
<html>
<head>
<script language="text/javascript" type="text/javascript">
var NUM_PLAYERS = 0;
var MIN_PLAYERS = 2;
function chkPlayer(eltForm) {
if (NUM_PLAYERS < MIN_PLAYERS) alert(MIN_PLAYERS + " minimum needed")
return (NUM_PLAYERS >= MIN_PLAYERS);
}
function countPlayer(elt) {
NUM_PLAYERS += (elt.checked) ? 1 : -1;
document.getElementById("p layers").i nnerHTML=" Players: <b>" +NUM_PLAYERS+ "</b>";
}
</script>
</head>
<body>
<form onsubmit="return chkPlayer(this)" action="#yourscript#">
<input type="checkbox" name="list[]" onClick="countPlayer(this) " value="me@localhost">me<br />
<input type="checkbox" name="list[]" onClick="countPlayer(this) " value="you@localhost">you< br/>
<input type="checkbox" name="list[]" onClick="countPlayer(this) " value="hime@localhost">him <br/>
<input type="checkbox" name="list[]" onClick="countPlayer(this) " value="her@localhost">her< br/>
<input type="submit">
</form>
<div id="players"></div>
</body>
</html>
Hope this helps
CirTap
If I got you right, youjust want a visual feedback for the user while she/he's choosing a playerlist. You won't get around to create a onSubmit() funtion if wou have a minimum of players, but you can use the NUM_PLAYERS variable and prevent sending the form if the value'S to low/high.
<html>
<head>
<script language="text/javascript"
var NUM_PLAYERS = 0;
var MIN_PLAYERS = 2;
function chkPlayer(eltForm) {
if (NUM_PLAYERS < MIN_PLAYERS) alert(MIN_PLAYERS + " minimum needed")
return (NUM_PLAYERS >= MIN_PLAYERS);
}
function countPlayer(elt) {
NUM_PLAYERS += (elt.checked) ? 1 : -1;
document.getElementById("p
}
</script>
</head>
<body>
<form onsubmit="return chkPlayer(this)" action="#yourscript#">
<input type="checkbox" name="list[]" onClick="countPlayer(this)
<input type="checkbox" name="list[]" onClick="countPlayer(this)
<input type="checkbox" name="list[]" onClick="countPlayer(this)
<input type="checkbox" name="list[]" onClick="countPlayer(this)
<input type="submit">
</form>
<div id="players"></div>
</body>
</html>
Hope this helps
CirTap
typo: should be
... getting their mailaddress is simply done with the $list[] array ...
and with "contextual feedback" change the above to
function countPlayer(elt) {
NUM_PLAYERS += (elt.checked) ? 1 : -1;
elt.form.btnSubmit.disable d = (NUM_PLAYERS < MIN_PLAYERS);
document.getElementById("p layers").i nnerHTML=" Players: <b>" +NUM_PLAYERS+ "</b>";
}
<!-- give it a name -->
<input type="submit" id="btnSubmit" disabled="disabled">
Code worx for IS5 & NS6, should work in NS4 if getElementId() line is changed for this ancestor
CirTap
... getting their mailaddress is simply done with the $list[] array ...
and with "contextual feedback" change the above to
function countPlayer(elt) {
NUM_PLAYERS += (elt.checked) ? 1 : -1;
elt.form.btnSubmit.disable
document.getElementById("p
}
<!-- give it a name -->
<input type="submit" id="btnSubmit" disabled="disabled">
Code worx for IS5 & NS6, should work in NS4 if getElementId() line is changed for this ancestor
CirTap
I needed to make client-side changes to a select input.
<select name="sto[]" disabled="disabled" size="5">
Here is the way I was able to make changes to this variable in JavaScript, even with the "[]" tacked on.
First of all, I was calling a function from a different input:
onClick="MsgToSwitch(MsgAd dForm)"
Then, here some of the script for that funcion:
<script language="JavaScript">
function MsgToSwitch(tForm)
{
x = tForm["sto[]"].length;
while (x >= 0)
{
tForm["sto[]"].options[x] = null;
x = x - 1;
}
if (tForm.mtype[0].checked)
{
tForm["sto[]"].disabled = true;
} else
tForm["sto[]"].disabled = false;
...
</script>
What you do is just use the form object that you pass in (in this case, tForm) as an associative array with the input object (in double quotes) as the index.
This is really more a JavaScript answer than a PHP answer, but it seems you were trying to do JavaScript to begin with, so I hope this helps.
-- Rob
<select name="sto[]" disabled="disabled" size="5">
Here is the way I was able to make changes to this variable in JavaScript, even with the "[]" tacked on.
First of all, I was calling a function from a different input:
onClick="MsgToSwitch(MsgAd
Then, here some of the script for that funcion:
<script language="JavaScript">
function MsgToSwitch(tForm)
{
x = tForm["sto[]"].length;
while (x >= 0)
{
tForm["sto[]"].options[x] = null;
x = x - 1;
}
if (tForm.mtype[0].checked)
{
tForm["sto[]"].disabled = true;
} else
tForm["sto[]"].disabled = false;
...
</script>
What you do is just use the form object that you pass in (in this case, tForm) as an associative array with the input object (in double quotes) as the index.
This is really more a JavaScript answer than a PHP answer, but it seems you were trying to do JavaScript to begin with, so I hope this helps.
-- Rob
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Didn't help or what?
However, how about doing an onSubmit() function which did the counting of checkboxes, then passed it as a GET variable on the URL to PHP?
Or, can't PHP determine the number checked on the server side by iterating through the checkbox values it gets? I mean by keeping an enumeration of the possible checkbox names, and looping through them while incrementing a counter.