check box +select all & delete

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

check box +select all & delete

Post by Jim_Bo »

Hi,

I have setup a checkbox system where you can select all .. bu when you use the delete button .. nothing gets deleted?

here are parts of the code I guess are of relevance:

Code: Select all

echo "<form name="form" method="post" action="../index.php?pages=pmparse&id=$id&cmd=deleterecord">

<table width="95%" border="0" cellspacing="1" cellpadding="5">
  <tr>
    <td bgcolor="#FEB341" width="7%"><div align="center"><font class="txt"><input type="submit" name="Submit" value="Delete"><input type="checkbox" value="on" name="allbox" onclick="checkAll();"/></font></div></td>
    <td bgcolor="#FEB341" width="15%"><div align="center"><font class="txt">SENDER</font></div></td>
    <td bgcolor="#FEB341" width="49%"><div align="center"><font class="txt">SUBJECT</font></div></td>
    <td bgcolor="#FEB341" width="20%"><div align="center"><font class="txt">DATE</font></div></td>
    <td bgcolor="#FEB341" width="10%"><div align="center"><font class="txt">READ</font></div></td>"; 
// Define colors for the alternating rows 
$color1 = "#CCCCCC";  
$color2 = "#999999";

Code: Select all

echo "<tr>
    <td width="7%" bgcolor="$row_color"><div align="center"><font class="txt"><a href="../index.php?pages=pmparse&id=$id&cmd=deleterecord">Delete</a> <input type="checkbox" value="$id" name="$id"></font></div></td>
    <td width="15%" bgcolor="$row_color"><font class="txt">$sender</font></td>
    <td width="49%" bgcolor="$row_color"><font class="txt">$subject</font></td>
    <td width="20%" bgcolor="$row_color"><div align="center"><font class="txt">$date</font></div></td>
    <td width="10%" bgcolor="$row_color"><div align="center"><font class="txt"><a href="../index.php?pages=read&id=$id">Read $read</a></font></div></td>
  	</tr>";
    // Add 1 to the row count 
    $row_count++; 
&#125; 
// Close out your table. 
echo "</table></form>
<br>";
Hows does it reference to all the id's of the checked options ..?

I can delete a single record fine using the text "delete" option you can see in the loop above ..

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

rename all the checkboxes to id[].

then expect an array of id's that were checked to delete..
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

Not quite with you I guess ..

javascript:

Code: Select all

<script language="javascript">
function checkAll()&#123;
for (var i=0;i<document.forms&#1111;0].elements.length;i++)
&#123;
var e=document.forms&#1111;0].elements&#1111;i];
if ((e.name != 'allbox') && (e.type=='checkbox'))
&#123;
e.checked=document.forms&#1111;0].allbox.checked;
&#125;
&#125;
&#125;
</script>
The main check box ( out of the loop ):

[code<form name=\"form\" method=\"post\" action=\"../index.php?pages=pmparse&id=$id&cmd=deleterecord\">

<input type=\"submit\" name=\"Submit\" value=\"Delete\">

<input type=\"checkbox\" value=\"on\" name=\"allbox\" onclick=\"checkAll();\"/>

Checkbox within the loop:

Code: Select all

<input type="checkbox" value="$id" name="id&#1111;]">
Something like that? .. Altho it doesnt work .. :oops:

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

did you alter the script to know how to process an array when it recieves it?
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

I have now .. All working great now .. Wasnt sure on the array part ..

Thanks ..
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

Here are the basics of it for others who want the same situation ..

Javascript:

Code: Select all

<script language="javascript">
function checkAll()&#123;
for (var i=0;i<document.forms&#1111;0].elements.length;i++)
&#123;
var e=document.forms&#1111;0].elements&#1111;i];
if ((e.name != 'allbox') && (e.type=='checkbox'))
&#123;
e.checked=document.forms&#1111;0].allbox.checked;
&#125;
&#125;
&#125;
</script>
Data:

Code: Select all

<form name="form" method="post" action="../path to delete script">

<input type="submit" name="Submit" value="Delete">

<input type="checkbox" value="on" name="allbox" onclick="checkAll();"/>

<input type="checkbox" value="$id" name="id&#1111;]">

</form>
Delete Script:

Code: Select all

foreach ($_POST&#1111;'id'] as $id)
&#123;

$sql = mysql_query("DELETE FROM pm WHERE id = $id") or die (mysql_error());

if(!$sql)&#123;

echo ("<center>There has been an error deleting this email. Please contact the webmaster.</center>"); 

&#125;

echo "<br><center>Record deleted</center><br>";
Good Luck
Post Reply