User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 391,660 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,818 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 5486 | Replies: 7
Reply
Join Date: Nov 2006
Posts: 35
Reputation: boo_lolly is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
boo_lolly boo_lolly is offline Offline
Light Poster

html checkboxes...

  #1  
Nov 21st, 2006
hey all! i've got a CMS i'm working on and i have a couple of HTML tables that are populated via php loops communicating with the SQL database. got it? good.

at the end of each row in the HTML table there is a checkbox. i need to be able to click as many of them as i want, and then press "delete" and have them all be deleted, and then immediately returned to the exact same page.


each HTML table row is exactly like the SQL row. each row has an ID, so that is probably the way i'd identify each row (it's worked for me so far =P).

so can i get any advice, or direction. i'm almost positive i'm gonna have to store the value of the checkboxes in an array of somesort, somehow but i have NO idea where to even start. thanks!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2006
Posts: 10
Reputation: the_count is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
the_count's Avatar
the_count the_count is offline Offline
Newbie Poster

Re: html checkboxes...

  #2  
Nov 22nd, 2006
declare the checkboxes as array

e.g.

 
<html>
<body>
<form>
<input type = 'checkbox' name = 'mychk[]'>
<input type = 'checkbox' name = 'mychk[]'>
<input type = 'checkbox' name = 'mychk[]'>
<input type ='submit'>
</form>
</body>
</html>
 

now you have three check boxes with the same name

post it to a page

and then use

  1. foreach ( $_POST['mychk'] as $k=> $c)
  2. {
  3. if ($c == 'On')
  4. { pSQL = "DELETE FROM YOUR TABLE WHERE NAME = $k"
  5. //execute the sql
  6. }
  7. }
  8.  


Where k is the key here, or the index,

you can set the primary key as the key. or you can even use hidden text boxes to hide the corrosponding ID of the checkbox. however this is the main idea.


hope it will be of use.
Last edited by the_count : Nov 22nd, 2006 at 5:17 am.
The day will soon come.
Reply With Quote  
Join Date: Nov 2006
Posts: 35
Reputation: boo_lolly is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
boo_lolly boo_lolly is offline Offline
Light Poster

Re: html checkboxes...

  #3  
Nov 22nd, 2006
that IS very useful, thank you =) except the page will just reload and display the new results (after deletion of old results).
Reply With Quote  
Join Date: Nov 2006
Posts: 35
Reputation: boo_lolly is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
boo_lolly boo_lolly is offline Offline
Light Poster

Re: html checkboxes...

  #4  
Nov 22nd, 2006
does NOT work. what is wrong with my code before i shoot somebody!!!!!
[php]
/*****DEBUG INFO*******
echo "<pre>";
print_r($_POST['delete']);
echo implode($_POST['delete'], ", ");
echo "</pre>";
**********************/
foreach($_POST['delete'] as $k => $c)
{
// echo $k ." / ". $c ." || "; <-- prints the EXACT correct information for the sql query to be properly executed.
$sql = "DELETE FROM my_search_table WHERE uID = ". $c ."";
mysql_query($sql);
}
[/php]

it should work. but it doesn't. WHAT THE HE77!!!!!!
Reply With Quote  
Join Date: Nov 2006
Posts: 10
Reputation: the_count is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
the_count's Avatar
the_count the_count is offline Offline
Newbie Poster

Re: html checkboxes...

  #5  
Nov 23rd, 2006
actually $c is a boolean value either ON or OFF

so you need to use an id

so uID must have been somehow generated in your table

can you please show us how you generate the table (mean the php code that generates the table)

The day will soon come.
Reply With Quote  
Join Date: Nov 2006
Posts: 10
Reputation: the_count is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
the_count's Avatar
the_count the_count is offline Offline
Newbie Poster

Re: html checkboxes...

  #6  
Nov 23rd, 2006
try to put the index as your id

like this
<html>
<body>
<form>
<input type = 'checkbox' name = 'mychk[6]'>
<input type = 'checkbox' name = 'mychk[9]'>
<input type = 'checkbox' name = 'mychk[10]'>
<input type ='submit'>
</form>
</body>
</html>
 
 




now you can use the "$k"

maybe you will get use of our internet project

this is the checkbox part of it
























viewsection.php
this code contains a part which can accept more then one request using checkboxes same as your idea
  1.  
  2. <?php
  3. session_start();?>
  4.  
  5. <html>
  6. <head>
  7. <script language = "javascript">
  8. function sure (stid)
  9. {
  10. y = confirm ("ARE YOU SURE YOU WANT TO DELETE THIS STUDENT WITH ID " + stid + " FROM THE DATABASE?");
  11.  
  12. if (y)
  13. {
  14. w = window.open ("delstudent.php?stu=" + stid);
  15. w.opener.location = "viewsection.php";
  16.  
  17. }
  18. }
  19. function checkall ()
  20. {
  21. //alert("called");
  22. for (i = 0; i < document.forms[0].elements.length; i ++ )
  23. document.forms[0].elements[i].checked=1;
  24.  
  25. }
  26. </script>
  27.  
  28. </head>
  29.  
  30. <?
  31. $iid=$_SESSION['iid'];
  32. $s = $_SESSION["s"];
  33. $isroot = $_SESSION["theroot"];
  34. $pend = $_SESSION['pend'];
  35. $cntrec=0;
  36. if (!isset($iid))
  37. {
  38. header("Location:anauthorized.html");
  39. die("");
  40. }
  41.  
  42. include('DB_connectscript.php');
  43. include 'calc.php';
  44. //check if he actually can view this section or not (if he is not root ONLY)
  45. if (!$isroot)
  46. {
  47. $ret = mysql_query ("SELECT * FROM SECTION WHERE SNUM = ".$s." AND INSID = ".$iid);
  48. if(!mysql_num_rows($ret))
  49. {
  50. echo "<h1 align = 'center' > Tring to view others sections? </h1>";
  51. exit;
  52. }
  53. }
  54.  
  55. echo "<body background='back.jpg' >";
  56. echo "<form name = 'frm' method = 'POST' action = 'acceptrequest.php'>";
  57. if ($pend != "STUDENT"){
  58. $result = mysql_query("SELECT NAME, SID, EMAIL, MAJOR FROM ".$pend." WHERE SNUM = ".$s) or die(mysql_error());
  59. echo "<H2 align=center><font color='#0'>Section #".$s."<br>Pending Students</font></H2><BR><BR>";
  60. }
  61. else{
  62. echo "<H2 align=center><font color='#0'>Section #".$s."</font></H2><BR><BR>";
  63. $result = mysql_query("SELECT NAME, SID, EMAIL, GRADE, MAJOR, COMMENT, ATTN FROM ".$pend." WHERE SNUM = ".$s) or die(mysql_error());
  64. }
  65.  
  66. echo "<table border=1 align ='center'>\n";
  67. $cnt=0;
  68. if ($pend == "STUDENT")
  69. echo "<tr><th>Section #</th><th> Student Name </th><th>StudentID #</th><th>Email</th><th>Grade</th><th>Major</th><th> Instructor comments </th><th> Attendance</th><th>Option</th></tr>";
  70. else
  71. {echo "<tr><th>Section #</th><th> Student Name </th><th>StudentID #</th><th>Email</th><th>Major</th><th>Enrollment</th></tr>";
  72. $cnt=5;
  73. }
  74.  
  75. while ($myrow = mysql_fetch_row ($result))
  76. {
  77. echo "<tr align = 'center'>\n";
  78. echo "<td>".$s."</td>";
  79. printf ("<td>%s</td>\n", !$myrow[0]? "-" : $myrow[0]);
  80. echo "<td>".$myrow[1]."</td>\n";
  81. //echo "<td><a href = 'mailto:".$myrow[2]."'>".$myrow[2]."</a></td>\n";
  82. printf ("<td>%s</td>\n", !$myrow[2]? "-" : "<a href = 'mailto:".$myrow[2]."'>".$myrow[2]."</a>");
  83.  
  84. if ($pend == "STUDENT")
  85. {
  86. printf ("<td>%s</td>\n", !$myrow[3]? "-" : $myrow[3]);
  87. printf ("<td>%s</td>\n", !$myrow[4]? "-" : $myrow[4]);
  88. echo "<td>".$myrow[5]."<small> <a href = 'tmpcomment.php?sid=".$myrow[1]."'> edit comment? </a></small></td>\n";
  89. echo "<td>".$myrow[6]."<small> <a href = 'attn.php?sid=".$myrow[1]."&at=".$myrow[6]."'>edit attendance? </a></small></td>\n";
  90. echo "<td><img border = 0 src = 'delete.gif' onclick = 'sure(".$myrow[1].")'> </td>";
  91. }
  92. else
  93. {
  94. printf ("<td>%s</td>\n", !$myrow[3]? "-" : $myrow[3]);
  95. //echo "<td><small> <a href = 'acceptrequest.php?sid=".$myrow[1]."'> Accept Request? </a></small></td>\n";
  96. echo "<td><input type = 'checkbox' name = acc[".$myrow[1]."]></td>";
  97. $cntrec++;
  98. }
  99.  
  100. echo "</tr>\n\n";
  101. }
  102.  
  103. if ($pend == "STUDENT")
  104. { echo "</table><br><br>";
  105. echo "<p align = center><a href = 'addstudent1.php'> Add student </a></p>";
  106. echo "<p align = 'center'><a href = 'showgrade.php'> Show Assignment(s) Grade </a></p>";
  107. echo "<p align = 'center'><a href = 'addassif.php'> Add/Hand New Assignment </a></p>";
  108. echo "<p align = 'center'><a href = 'showpgrade.php'> Show Project(s) Grade </a></p>";
  109. echo "<p align = 'center'><a href = 'addproject.php'> Add/Submit New Project</a></p>";
  110. echo "<p align = 'center'><a href = 'showegrade.php'> Show Exam(s) Grade </a></p>";
  111. echo "<p align = 'center'><a href = 'addexam.php'> Add/Submit New Exam</a></p>";
  112. echo "<p align = 'center'><a href = 'uploadlecture.php'> Upload Lectures </a>";
  113.  
  114. echo "<br><p align='center'><a href = 'allstudents.php'><img border=0 src='backa.gif'></a></p>";
  115.  
  116. }
  117. else
  118. {
  119. if ($cntrec) {
  120.  
  121.  
  122. echo "<tr>";
  123. for ($i = 0; $i < $cnt ; $i++)
  124. echo "<td></td>";
  125. echo "<td><input type = 'submit' name = 'submit' value = 'Accept'><input type = 'button' name = 'ck' value = 'Check All' onClick = 'checkall()' ></td></tr>";
  126. }
  127. echo "</table><br><br></form>";
  128.  
  129. echo "<br><p align='center'><a href = 'request.php'><img border=0 src='backa.gif'></a></p>";
  130.  
  131. }
  132. ?>
  133.  
  134.  
  135.  
  136. </body></html>
  137.  
The day will soon come.
Reply With Quote  
Join Date: Nov 2006
Posts: 35
Reputation: boo_lolly is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
boo_lolly boo_lolly is offline Offline
Light Poster

Re: html checkboxes...

  #7  
Nov 27th, 2006
here's what my HTML checkbox looks like...
<TD align=center><input type=checkbox name=delete[] value=". $row['uID'] .">

here's my foreach loop...
[php]
foreach($_POST['delete'] as $k => $c){
//echo $k ." / ". $c ." || "; //<-- prints the EXACT correct information for the sql query to be properly executed.
$sql = "DELETE * FROM my_search_table WHERE uID = ". $c ."";
mysql_query($sql);
}
[/php] when i uncomment the debugging line and comment out the other two lines, and click a couple of checkboxes, then press delete, this is the output it gives me...
0 / 3ZFc7FmpYiGpyTybHKXix14teZsQlQ || 1 / FVWxsWALHwtzWjKX23NsMXwbjImaqh || 2 / pSWURh1EsraukzZwYHwiF4DHJnaB3B ||

so, what i see is that it's passing the correct information to the foreach loop ($c is the uID), but for some reason or another, my SQL query won't process the request, and i can't figure out why. i'm thinkin the problem resides in my SQL query... can anybody help?
Last edited by boo_lolly : Nov 27th, 2006 at 11:12 am.
Reply With Quote  
Join Date: Nov 2006
Posts: 35
Reputation: boo_lolly is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
boo_lolly boo_lolly is offline Offline
Light Poster

Re: html checkboxes...

  #8  
Nov 27th, 2006
IT WORKS IT FINALLY WORKS!!!!! it was the '*' and the single quotes in the SQL query that were causing all the problems!!!! IT WORKS!!!!
[php]
$_POST['delete'];
if($_POST['delete'] != NULL){
foreach($_POST['delete'] as $k => $c){
//echo $k ." / ". $c ." || "; //<-- prints the EXACT correct information for the sql query to be properly executed.
$sql = "DELETE FROM my_search_table WHERE uID = '". $c ."'";
mysql_query($sql) OR die ("The query:<br>" . $sql . "<br>Caused the following error:<br>" . mysql_error());
}
}
[/php]

RIGHTEOUS!!!!!! thank you guys so much!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 1:41 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC