By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,435 Members | 2,939 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,435 IT Pros & Developers. It's quick & easy.

checked(checkbox) row or rows to be deleted when button(delete) is clicked

P: 27
Hi all,
I am new to php,my question is that in this following code i am retrieving many rows from the database which is working good.i want to delete a particular row(s) which is checked(checkbox) and this should be done when the deletecontact button is clicked and also should be deleted int the database also

Somebody help me out on this code

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function SetAllCheckBoxes(FormName, FieldName, CheckValue)
  5. {
  6.     if(!document.forms[FormName])
  7.         return;
  8.     var objCheckBoxes = document.forms[FormName].elements[FieldName];
  9.     if(!objCheckBoxes)
  10.         return;
  11.     var countCheckBoxes = objCheckBoxes.length;
  12.     if(!countCheckBoxes)
  13.         objCheckBoxes.checked = CheckValue;
  14.     else
  15.         // set the check value for all check boxes
  16. for(var i = 0; i < countCheckBoxes; i++)
  17.  
  18.             objCheckBoxes[i].checked = CheckValue;
  19. }
  20. </script>
  21. </head>
  22. <body>
  23.  
  24. <?php include "menu.php";?>
  25.  
  26. <center><b>CONTACT INFO</b></center><br>
  27.  
  28.   <form method="post" onSubmit="<?=$PHP_SELF?>">
  29.  
  30.  <table border="0" cellpadding="0" cellspacing="0">
  31.  
  32.   <tr>
  33.     <td>
  34.         <table  border="0" cellpadding="0" cellspacing="0">
  35.             <tr>
  36.                 <td style="padding-left:20px;padding-bottom:20px;"><a href="createcontact.php" style="text-decoration:none"><input type="button" name="new" value="CREATE CONTACT"  style="width:170px;background-image:url(pink.gif)"></a></td>
  37.                 <td style="padding-left:20px;padding-bottom:20px;"><a href="createlead.php" style="text-decoration:none"><input type="button" name="new" value="CREATE LEAD"  style="width:170px;background-image:url(pink.gif)"></a></td>
  38.             </tr>
  39.  
  40.             <tr>
  41.                 <td style="padding-left:20px;"><a href="createaccount.php" style="text-decoration:none"> <input type="button" name="new" value="CREATE ACCOUNT"  style="width:170px;background-image:url(pink.gif)"></a></td>
  42.                 <td style="padding-left:20px;"> <a href="createopportunity.php" style="text-decoration:none"> <input type="button" name="new" value="CREATE OPPORTUNITY"  style="width:170px;background-image:url(pink.gif)"></a></td>
  43.             </tr>
  44.         </table>
  45.     </td>
  46.  
  47.     <td style="padding-left:120px;">
  48.         <table  cellpadding="8" cellspacing="0" with frame="box">        
  49.         <tr>
  50.             <td style="padding-left:10px;padding-bottom:20px;">LAST NAME</td>
  51.             <td style="padding-left:60px;padding-bottom:20px;"><input type="text" name="lname"></td>
  52.         </tr>
  53.  
  54.         <tr>
  55.             <td style="padding-left:10px"><input type="submit" name="search" value="SEARCH"  style="width:80px;background-image:url(pink.gif)" ></td>
  56.             <td style="padding-left:30px"><input type="reset" name="clear" value="CLEAR"  style="width:80px;background-image:url(pink.gif)" ></td>
  57.         </tr>
  58.         </table>
  59.     </td>
  60.   </tr>
  61. </table>
  62. <br/><br/>
  63.  
  64. <b><center>CONTACT LIST </center></b>
  65.  
  66. <br/>
  67.  
  68.  <?php
  69.      include 'connect.php';
  70.      $a= $_POST[lname];
  71.  
  72.  if(!$a==NULL)
  73. {
  74.     $res = mysql_query("SELECT * FROM createcontact where lname LIKE '%$a%' ");
  75. ?>   
  76.  
  77.    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  78.     <input type="button"  style="width:130px;background-image:url(pink.gif)" onclick="SetAllCheckBoxes('contacts', 'myCheckbox', true);" value="SELECT ALL">
  79.  
  80. <input type="button" style="width:130px;background-image:url(pink.gif)"onclick="SetAllCheckBoxes('contacts', 'myCheckbox', false);" value="SELECT NONE">
  81.   <input type="button" name="delete" value="DELETE CONTACT"  style="width:130px;background-image:url(pink.gif)">
  82.  
  83.    </form>
  84.     <table border="1" align="center" cellpadding="0" cellspacing="0" width="900" style="border:1px solid #ffaacc;">        <tr>
  85.         <td width="50" align="center"><form name="contacts" onLoad="return false;"><input type="checkbox" name="myCheckbox"></td>
  86.         <td>NAME</td>
  87.         <td>TITLE</td>
  88.         <td>A/C NAME</td>
  89.         <td>EMAIL</td>
  90.         <td>PHONE NO</td>
  91.         <td>TEAM</td>
  92.         <td>USER</td>
  93.     </tr>
  94.  
  95.     <?
  96.     while($row = mysql_fetch_array($res))
  97.    {
  98.  
  99.     echo '<tr>';
  100.     echo '<td width="50" align="center"><form name="contacts" onLoad="return false;"><input type="checkbox" name="myCheckbox"></td>';
  101.     echo '<td >'.$row['lname'].'</td>';
  102.     echo '<td >'.$row['title'].'</td>';
  103.     echo '<td >'.$row['acname'].'</td>';
  104.     echo '<td >'.$row['email'].'</td>';
  105.     echo '<td >'.$row['phone'].'</td>';
  106.     echo '<td >'.$row['team'].'</td>';
  107.     echo '<td >'.$row['assignedto'].'</td>';
  108.     echo '</tr>';
  109.  
  110.    }
  111.  
  112.    echo "</table>";
  113. }
  114. else
  115. {
  116.  
  117.     $res = mysql_query("SELECT * FROM createcontact ");
  118. ?>
  119.  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  120.     <input type="button"  style="width:130px;background-image:url(pink.gif)" onclick="SetAllCheckBoxes('contacts', 'myCheckbox', true);" value="SELECT ALL">
  121.  
  122. <input type="button" style="width:130px;background-image:url(pink.gif)"onclick="SetAllCheckBoxes('contacts', 'myCheckbox', false);" value="SELECT NONE">
  123.   <input type="button" name="delete" value="DELETE CONTACT"  style="width:130px;background-image:url(pink.gif)">
  124. </form>
  125.  
  126.  
  127.  <table border="1" align="center" cellpadding="0" cellspacing="0" width="900" style="border:1px solid #ffaacc;">        <tr>
  128.  
  129.  <td width="50" align="center"><form name="contacts" onLoad="return false;"><input type="checkbox"  name="myCheckbox"></td>
  130.  
  131.         <td>NAME</td>
  132.         <td>TITLE</td>
  133.         <td>A/C NAME</td>
  134.         <td>EMAIL</td>
  135.         <td>PHONE NO</td>
  136.         <td>TEAM</td>
  137.         <td>USER</td>
  138.     </tr>
  139.  
  140. </body> 
  141.     <?
  142.     while($row = mysql_fetch_array($res))
  143.    {
  144.  
  145.     echo "<tr>";
  146.     echo '<td width="50" align="center"><form name="contacts" onLoad="return false;"><input type="checkbox" name="myCheckbox"></td>';
  147.     echo "<td>".$row['lname']."</td>";
  148.     echo "<td>".$row['title']."</td>";
  149.     echo "<td>".$row['acname']."</td>";
  150.     echo "<td>".$row['email']."</td>";
  151.     echo "<td>".$row['phone']."</td>";
  152.     echo "<td>".$row['team']."</td>";
  153.     echo "<td>".$row['assignedto']."</td>";
  154.     echo "</tr>";
  155.  
  156.  
  157.    }
  158.  
  159.     echo"</table>";?>
  160.  
  161. <?
  162.     }    
  163. ?>
  164.  
  165. </form>
  166. </body>
  167. </html>
  168.  
  169.  
  170.  
Mar 26 '09 #1
Share this Question
Share on Google+
1 Reply


Markus
Expert 5K+
P: 6,050
If you have multiple checkboxes on the same subject, you need to pass them as an array. You do this by appending brackets to the end of all the checkboxes name's. <input type="checkbox" name="delete_items[]" value="1" />

-- Assumes you're using POST --

Then, in PHP, you can traverse the array of selected checkboxes (if it exists) and perform actions on them accordingly.

Expand|Select|Wrap|Line Numbers
  1.  
  2. if ( count ( $_POST['delete_items'] ) > 0 )
  3. {
  4.     foreach ( $_POST['delete_items'] as $key => $val )
  5.     {
  6.         echo "{$key} -> {$val}<br />";
  7.     }
  8. }
  9. else
  10. {
  11.     echo "No checkboxes passed.";
  12. }
  13.  
Mar 26 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.