checkboxes problem

Reply

Join Date: Feb 2008
Posts: 90
Reputation: bornok15 is an unknown quantity at this point 
Solved Threads: 2
bornok15 bornok15 is offline Offline
Junior Poster in Training

checkboxes problem

 
0
  #1
Mar 7th, 2008
I want to write a code that will view all the records within a table and assign a checkbox for each record.. the check box will be used for deleting the records just like in emails. can anyone help me on this?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 483
Reputation: DangerDev has a spectacular aura about DangerDev has a spectacular aura about 
Solved Threads: 58
DangerDev's Avatar
DangerDev DangerDev is offline Offline
Posting Pro in Training

Re: checkboxes problem

 
0
  #2
Mar 7th, 2008
hi
try this example:
suppose you want following to be done:
[]record1
[]record2
here [] is check box
then do following to get above:
1.suppose you have two variable $rec1 and $rec2 which contains data to be printed along with check box,
$rec1="record1" ;
$rec2="record2";
2.prepare a $rspString like following:

$rspString="<form>"+$rec1+"<input type='checkbox' name='some_name' value="+$rec1+">
<br>"+$rec2+"<input type='checkbox' name='some_name2' value="+$rec2+">

</form>";
print this.
it will create check box with required variable value.
this is just a example. you can follow this example to generate required string for you.
Freedom in the Mind, Faith in the words.. Pride in our Souls...
Indian Developer
http://falaque.wordpress.com/
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 52
Reputation: JeniF is an unknown quantity at this point 
Solved Threads: 5
JeniF's Avatar
JeniF JeniF is offline Offline
Junior Poster in Training

Re: checkboxes problem

 
0
  #3
Mar 7th, 2008
Hi,
Even though it is a fairly advanced script you could try looking at this post:

http://www.daniweb.com/forums/thread111225.html
or you can do a search on checkboxes and find a few additional examples
I keep hitting "escape", but I'm still here!!!!
:}
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: checkboxes problem

 
0
  #4
Mar 7th, 2008
just assign a checkbox with a different name to each one with id value as the checkbox value. then use the foreach() statement to loop through the post results and regex the key to make sure it belongs to that set of input elements. after that take the value from the foreach statement and use it in a query to delete the record.

i know that sounds complicated and you probably don't understand it. if you need some example code let me know.
Last edited by kkeith29; Mar 7th, 2008 at 8:38 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 90
Reputation: bornok15 is an unknown quantity at this point 
Solved Threads: 2
bornok15 bornok15 is offline Offline
Junior Poster in Training

Re: checkboxes problem

 
0
  #5
Mar 7th, 2008
Its like in group of records where they all have a checkboxex assigned to them and you can check it to do something to it.. i hope you could give some simple example on this.. thanks
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 90
Reputation: bornok15 is an unknown quantity at this point 
Solved Threads: 2
bornok15 bornok15 is offline Offline
Junior Poster in Training

Re: checkboxes problem

 
0
  #6
Mar 7th, 2008
Btw this is how i do it right now. im just using a text link to delete the records.
  1. $action=$_REQUEST['action'];
  2. $id=$_REQUEST['id'];
  3.  
  4. if ($action==1){
  5.  
  6. $link=mysql_connect($hostname,$username,$password);
  7. $dbname=mysql_select_db($database);
  8.  
  9. echo "<table width=100% cellspacing=0>";
  10. echo "<tr><td bgcolor=darkblue><center><font face=verdana size=4 color=white>GV Local Site using PHP and MySQL</font></tr>";
  11.  
  12. $query = "SELECT * FROM employee";
  13.  
  14. echo "<table width=100% cellspacing=0 border=1><tr><td><center><B>ID<td><center><B>First<td><center><B>Last Name<td><center><B>Address<td><center><B>Delete Entry</tr>";
  15.  
  16. $result = mysql_query($query) or die ("Query failed: " . mysql_error() . "Actual query: " . $query);
  17.  
  18. //$result = mysql_query("select * from mytable");
  19. //while ($row = mysql_fetch_object($result)) {
  20.  
  21. while ($row = mysql_fetch_object($result)){
  22. echo "<tr><td>".$row->id;
  23. echo "<td>".$row->first;
  24. echo "<td>".$row->last;
  25. echo "<td>".$row->address;
  26. echo "<td><a href='ayan.php?action=2&id=$row->id'>Delete Record</a></tr>";
  27. }
  28. echo "</table><br><br><center><font face=arial size=4 color=white><b><a href='index.php'>Go back to Main</a></b></font>";
  29. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: checkboxes problem

 
0
  #7
Mar 7th, 2008
i am working on the code, i will post soon.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: checkboxes problem

 
0
  #8
Mar 7th, 2008
here is the code. i am not for sure if it will work properly with your setup because i don't know the rest of the code, but i have used a similar system in many of my admin centers i have created and it works nicely.

  1. <?php
  2.  
  3. $action = $_GET['action'];
  4. if (!empty($action)) {
  5. $con = mysql_connect($hostname,$username,$password);
  6. $db_con = mysql_select_db($database);
  7. switch($action) {
  8. case 1:
  9. $sql = "SELECT * FROM `employee`";
  10. $query = mysql_query($sql,$con) or die('Error: ' . mysql_error());
  11. $tr_data = '';
  12. $i = 0;
  13. while ($row = mysql_fetch_assoc($query)) {
  14. $tr_data .= '<tr>';
  15. $tr_data .= '<td>' . $row['id'] . '</td>';
  16. $tr_data .= '<td>' . $row['first'] . '</td>';
  17. $tr_data .= '<td>' . $row['last'] . '</td>';
  18. $tr_data .= '<td>' . $row['address'] . '</td>';
  19. $tr_data .= '<td><input type="checkbox" name="delete' . $i . '" value="' . $row['id'] . '" /></td>';
  20. $tr_data .= '</tr>';
  21. $i++;
  22. }
  23. $html =<<<HTML
  24. <table width="100%" cellspacing="0">
  25. <tr>
  26. <td bgcolor="#0000FF" align="center">
  27. <font face="verdana" size="4" color="#FFFFFF">GV Local Site using PHP and MySQL</font>
  28. </td>
  29. </tr>
  30. </table>
  31. <form action="ayan.php?action=2" method="post">
  32. <table width="100%" cellspacing="0" cellpadding="3" border="1">
  33. <tr>
  34. <th>ID</th>
  35. <th>First</th>
  36. <th>Last Name</th>
  37. <th>Address</th>
  38. <th>Delete</th>
  39. </tr>
  40. $tr_data
  41. </table>
  42. </form>
  43. <br />
  44. <br />
  45. <center>
  46. <font face="arial" size="4" color="#FFFFFF"><strong><a href="index.php">Go back to Main</a></strong></font>
  47. </center>
  48. HTML;
  49. break;
  50. case 2:
  51. foreach($_POST as $key => $value) {
  52. if (preg_match("/^delete+[0-9]$/",$key)) {
  53. $sql = "DELETE FROM `employee` WHERE `id` = '" . mysql_real_escape_string($value) . "'";
  54. $query = mysql_query($sql,$con) or die('Error: ' . mysql_error());
  55. }
  56. }
  57. $html = 'Employees Deleted Successfully';
  58. break;
  59. }
  60. }
  61.  
  62. echo $html;
  63.  
  64. ?>

let me know if there are any problems. i will help you fix them.
Last edited by kkeith29; Mar 8th, 2008 at 12:00 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 90
Reputation: bornok15 is an unknown quantity at this point 
Solved Threads: 2
bornok15 bornok15 is offline Offline
Junior Poster in Training

Re: checkboxes problem

 
0
  #9
Mar 8th, 2008
Can u explain how the $tr_data and the for each block works?
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: checkboxes problem

 
0
  #10
Mar 8th, 2008
what i did with the $tr_data is store the html into a variable instead of echoing like you had. this makes it so i can use the header() function to redirect, sorry its a force of habit. i can change if you want. the foreach() block starts off by reading the post data sent from the form where you checked the checkboxes. "foreach" of the array values it checks if the key (name element in html) is valid. if it is then it deletes the employee otherwise it skips it.

clear enough?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC