/    Sign up×
Community /Pin to ProfileBookmark

PHP and JavaScript multiple delete check box with a select all button

I have a table that display some messages and the first row its a checkbox. The name of the checkbox is like “checkbox[]” so , when i get the post data i can delete them all from the database. This is working fine, the problem is that im trying to make a select /unselect all for the checkbox and its not working no mather what i try. Anyone have experiencie with these??

Here is my code:

[CODE]<form name=”formmsg” id=”formmsg” method=”post” action=”” class=”tabladiv”>
<table id=”tablamensajes” class=”tabladiv”><tr><td><div class=”cabezerabox”>&nbsp;&nbsp;</div></td><td style=”width:100px”><div class=”cabezerabox”>Received</div></td><td style=”width:400px”><div class=”cabezerabox”>Mensaje</div></td><td><div class=”cabezerabox”>Sender</td></tr>
<tr><td class=”check”><input type=”checkbox” name=”chkIdMsg[<? echo $i;?>]” value=”<? echo $mensa[$i][‘idMsg’];?>”></td><td class=”mensajes”><? echo $mensa[$i][‘fecha’];?></td><td id=”<? echo $id;?>” class=”filas” onclick=”mostrarOcultar(this)”><a href=”#” onclick=”function(); return false;”><? echo $resumen;?>…</a></td><td class=”mensajes”><a href=”#” onclick=”function(); return false;”><img src=”images/who.png” height=”30px” width=”30px”></img></a></td></tr>
<tr id=”<? echo $id;?>” class=”filasocultas”><td colspan=”4″ class=”mensajeentero”><? echo nl2br($mensa[$i][‘mensaje’]);?></td></tr>
<tr><td colspan=”4″> &nbsp;&nbsp;&nbsp;&nbsp;<img src=”images/arrow.png”></img>&nbsp;&nbsp;<a href=”#” onclick=”marcarTodos()”>Select All&nbsp;&nbsp;/Unselect All</a><a href=”#” onclick=”document.formmsg.submit();”><img src=”images/drop.png”>Delete Selected</img></a></td></tr>
</table></form></div>[/CODE]

And now the function to check all

[CODE]function marcarTodos(){
var objCheckBoxes = document.forms[“formmsg”].elements[“chkIdMsg[]”];
if(!objCheckBoxes){
alert(“no hay”);
return;
}
var countCheckBoxes = objCheckBoxes.length;
for(i=0;i<countCheckBoxes;i++){
objCheckBoxes[i].click();
}
}[/CODE]

Hope someone can help.Thanks

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@pitikoJan 20.2009 — javascript: page1.html

[CODE]
<script LANGUAGE="JavaScript">
function seleciona() {
if (F1.master.checked==true)
for (i=0; i<document.F1.elements.length;i++)
{
document.F1.elements[i].checked=true
}
if (F1.master.checked==false)
for (i=0; i<document.F1.elements.length;i++)
{
document.F1.elements[i].checked=false
}
}
</script>

<form name="F1" method="post" action="page.php">
<input type="checkbox" name="master" onClick="seleciona()"> // For select one checkbox
<input type="checkbox" name="selecionar[]" value="<?php echo $cod_id;?>"> // For select all checkbox
</form>
[/code]


page.php

[CODE]
$id = $_POST['selecionar'];
if ($id != null) {
For($i=0;$i < count($id);$i++)
{
mysql_query("DELETE from table WHERE cod_id = '$id[$i]'");
}[/CODE]
Copy linkTweet thisAlerts:
@web_bertJan 20.2009 — The delete should be done in one query:

[CODE]
$id = $_POST['selecionar'];
$ids = implode(',',$id);
if ($id != null) {

mysql_query("DELETE from table WHERE cod_id IN ($ids)");
}
[/CODE]
Copy linkTweet thisAlerts:
@chifliiiiiauthorJan 20.2009 — Mmmmm its not what i need. You are selecting all checkboxes with another checkbox. Im using a link.

Also i need to user name=checkbox[] on every checkbox, so when the form is submited i get a $_post array with all the values of all the checkboxes. The only way i managed to make the Select/unselect to work was at the beggining when i used the same name on the checkboxes. So my js was

var chkbox= document.getElementsByName("chkmsg");

for(var=i ; i< chkbox.length ; i++){

chkbox.click();

}
Copy linkTweet thisAlerts:
@chifliiiiiauthorJan 21.2009 — Just to make it more clear , here is a pic of my actual table [URL=http://imageshack.us][IMG]http://img266.imageshack.us/img266/107/checkgc0.jpg[/IMG][/URL]

The name of the checkbox are chkIdMsg[$i] , so then i can collect the ids i want to delete like this:
[CODE] if(isset($_POST['chkIdMsg'])){

if(!empty($_POST['chkIdMsg'])){
$aLista=$_POST['chkIdMsg'];
$sql="DELETE FROM mensajes where idMsg IN (".implode(',',$aLista).")";

$result = mysql_query($sql,$conn);

}

}[/CODE]

tha bit is working. The problem is that i dont know hot to get all the checkboxs with javascript when the names are all like chkIdMsg[0] chkIdMsg[1] chkIdMsg[2]
Copy linkTweet thisAlerts:
@chifliiiiiauthorJan 21.2009 — OMG Pitiko real sorry, i missunderstood your code. I re-read it today again and i saw the difference. All is working fine, tahnks a lot to both of you. Here is my code :
[CODE]function marcarTodos(){
for (i=0; i<document.formmsg.elements.length;i++)
{
if(document.formmsg.elements[i].checked==false){
document.formmsg.elements[i].checked=true
}else if(document.formmsg.elements[i].checked==true){
document.formmsg.elements[i].checked=false
}
}

}[/CODE]
×

Success!

Help @chifliiiii 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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...