473,395 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
1 4092
Markus
6,050 Expert 4TB
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

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

Similar topics

2
by: Bart Verbeke | last post by:
Hi, I have a form with 2 radio buttons (ACCEPT/REJECT). When the page is initially loaded, no textbox should be visible. When a user clicks the REJECT radio button, a textbox should...
4
by: Piotr | last post by:
how can I read (in alert for example) array index number of checked checkbox? I have: <input type="checkbox" id="id_number" name="check" value="1" onclick="show()"/> <input type="checkbox"...
0
by: L Mehl | last post by:
Hello -- My form's cmdCancel button requires 3 clicks before the code in Cancel_Click fires. Each click prior to that success moves the highlight to the next field in the Tab order . I...
2
by: Alan Silver | last post by:
Hello, I have a repeater with a list of products in it. Each item in the repeater has a checkbox, allowing the product to be selected. When a link button is clicked, I want to loop through the...
5
by: tsnyder | last post by:
I need to have an option button that allows editing to a field only when it is checked.
0
by: rn5a | last post by:
All the rows in a DataGrid are accompanied by a CheckBox. When a user checks the rows & clicks a Button, the checked rows get deleted. For e.g. assume that the DataGrid displays 10 rows. A user...
10
by: Bishman | last post by:
Hi, I need to programmatically allow or disallow the selection of multiple checkboxes on a form . At certain times I only want to allow a single selection, at other times I may wish to allow...
2
by: arizal | last post by:
Hi, I am having some problem trying to think how to write a code for deleting the entry which is loaded from the database. I have a while loop which pulls the userID, name etc from the database and...
0
by: cyberdawg999 | last post by:
Greetings all in ASP land I have overcome one obstacle that took me 2 weeks to overcome and I did it!!!!! I am so elated!! thank you to all who invested their time and energy towards helping me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.