943,928 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 4086
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 16th, 2008
0

Validate Selected Checkboxes

Expand Post »
I have a form in which there is a table that displays a list from the MySQL with checkboxes to each displayed name. I have to make a selection out from the list by checking some the checkboxes all may be all the checkboxes in some circumstances.

A. I have to validate the selection made

B. I have to store the value of the selected checkboxes into an array

I need assistance as I do not know how and what to validate against for the checkboxes.

php Syntax (Toggle Plain Text)
  1. // Make a MySQL Connection
  2. $conn = mysql_connect("", "", "")
  3. or die(mysql_error());
  4. mysql_select_db("",$conn) or die(mysql_error());
  5.  
  6. $my_list = "select emp_no, concat_ws(', ', lname, fname) as display_name
  7. from employee order by lname, fname";
  8.  
  9. $my_list_res = mysql_query($my_list) or die(mysql_error());
  10.  
  11. if (mysql_num_rows($my_list_res) < 1) {
  12. //no records
  13. echo "<p><em>Sorry, no records to select!</em></p>";
  14.  
  15. } else {
  16. // array that accepts the employee list - shd use $_SESSION???
  17. $hello_array[] = $my_list_res;
  18. ?>
  19. <!-- major table starts here -->
  20. <center>
  21. <form name="slip" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
  22.  
  23. <?php
  24. echo "<table border='1'>";
  25. echo "<tr> <th>Last Name, First Name</th> <th>Check / UnCheck </th> </tr>";
  26. // keeps getting the next row until there are no more to get
  27.  
  28.  
  29. while($row = mysql_fetch_array( $get_list_res )) {
  30. // Print out the contents of each row into a table
  31. echo "<tr><td>";
  32. //echo $row['name'];
  33. echo $display_name = stripslashes($row['display_name']);
  34. echo "</td><td>";
  35. echo "<input type=\"checkbox\" name=\"dname[]\" value=\"row['emp_no']\">";
  36. echo "</td> </tr>";
  37. }
  38. }
  39. echo "</table>";
  40. ?>
  41. </center>
  42. <!-- Cancel and Next Buttons should be placed in a form -->
  43. <br />
  44. <br />
  45. <center>
  46.  
  47. <input type="Submit" name="cancel" value="CANCEL!!!" action="my_meeting.php"> &nbsp; &nbsp; &nbsp; &nbsp;
  48. <input type="submit" name="nex" value="Next">
  49. </form></center>
  50.  
  51. <?php
  52. if(isset($_POST['nex'])){
  53.  
  54. /**
  55. * ensure all the selected checkboxes pass their ids to a new array
  56. *
  57. */
  58.  
  59.  
  60.  
  61.  
  62. header('location:auntie.php');
  63. echo "<br />";
  64. exit();
  65. }else if (isset ($_POST['cancel'])){
  66. // return to uncle.php
  67. header('location:uncle.php');
  68. echo "<br />";
  69. exit();
  70.  
  71. }
  72.  
  73.  
  74. ?>
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
jackakos is offline Offline
50 posts
since Sep 2008
Oct 17th, 2008
0

Re: Validate Selected Checkboxes

It may help to see what is being passed in the POST array when you click the next button. I would throw the following code in where you have your comment for creating a new array with the checked ID's:

php Syntax (Toggle Plain Text)
  1. print_r($_POST['dname']);

That will get you something like this after you submit the form with a couple of employees checked:

php Syntax (Toggle Plain Text)
  1. Array(
  2. 0 => 21,
  3. 3 => 34,
  4. 7 => 31
  5. );

Now, I'm totally guessing and making these numbers up, but you should have the array of checked values right in your $_POST['dname'] array. Then you can loop through them and validate each ID or whatever it is you want to do with 'em.

php Syntax (Toggle Plain Text)
  1. foreach($_POST['dname'] as $employeeId)
  2. {
  3. // Do your magic stuff here
  4. }

Hope that helps, let me know if I totally misunderstood your question

Great one jackakos,

Chrelad
Reputation Points: 35
Solved Threads: 5
Light Poster
chrelad is offline Offline
33 posts
since Nov 2007
Oct 17th, 2008
0

Re: Validate Selected Checkboxes

Quote ...
A. I have to validate the selection made
this is the code:
javascript Syntax (Toggle Plain Text)
  1. <script type="text/javascript">
  2. function chkChecks(){
  3. isChecked=false
  4. for(var i=0;i<document.forms["new_page"]["allowed[]"].length;i++){
  5. if(document.forms["new_page"]["allowed[]"][i].checked){
  6. isChecked=true
  7. }
  8. }
  9. if(isChecked){
  10. document.forms["new_page"].submit()
  11. }
  12. else{
  13. alert('Please select a checkbox')
  14. }
  15. }
  16. </script>
And
Quote ...
B. I have to store the value of the selected checkboxes into an array
For this:
this single line will get all checked ones with comma sepperated,then you can insert them in your database..
PHP Syntax (Toggle Plain Text)
  1. $comma_checkedones = implode(",", $_POST["dname"]);
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Oct 20th, 2008
0

Re: Validate Selected Checkboxes

Hi Chrelad and Shanti, Thanx for your inputs they have been helpful but haven't found the solution yet.

This is what the output was, when I tried Chrelad's

Array ( [0] => row['emp_no'] [1] => row['emp_no'] [2] => row['emp_no'] )
row['emp_no']
row['emp_no']
row['emp_no']

I was hoping to get the values of the 'emp_no'

What I have to do with the finally accumulated array is to use the 'emp_no' query the database and then pick their calendar for a specific date and then search for a specified time. This search should inform who is busy and who is free at the said time.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
jackakos is offline Offline
50 posts
since Sep 2008
Oct 21st, 2008
0

Re: Validate Selected Checkboxes

try this:
PHP Syntax (Toggle Plain Text)
  1. foreach($_POST["dname"] as $key=>$val){
  2. echo "key: ". $key. " value: ". $val ."<br />\n";
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Oct 21st, 2008
0

Re: Validate Selected Checkboxes

I updated the code with the update above, I seem not to get the values of the 'emp_no' - it has gotten me confused, may be I am not doing something right. this is how the code looks now:

connection and query:
php Syntax (Toggle Plain Text)
  1. while($row = mysql_fetch_array( $get_list_res )) {
  2. // Print out the contents of each row into a table
  3. echo "<tr><td>";
  4. //echo $row['name'];
  5. //echo $_SESSION['ids'] = $_POST['eventtime'];
  6. echo $ids[] = $row['emp_no'];
  7. echo $display_name = stripslashes($row['display_name']);
  8. echo "</td><td>";
  9. echo "<input type=\"checkbox\" name=\"dname[]\" value=\"row['emp_no']\">";
  10. echo "</td> </tr>";
  11. }
  12. }
  13. echo "</table>";
  14. ?>
  15. </center>
  16. <!-- Cancel and Next Buttons should be placed in a form -->
  17. <br />
  18. <br />
  19. <center>
  20.  
  21. <input type="Submit" name="cancel" value="CANCEL!!!"> &nbsp; &nbsp; &nbsp; &nbsp;
  22. <input type="submit" name="nex" value="Next">
  23. </form></center>
If I remove the comments on
//echo $ids[] = $row['emp_no'];

I can see the values of the 'emp_no' next to the names as

Last Name, First Name Check / UnCheck
205admin, data
201Atkins, John
202Floyd, Mike
203Johnson, Ian
204White, Jane

below is what I have for processing the checkboxes.
it is giving an output without the 'emp_no values.

php Syntax (Toggle Plain Text)
  1. <?php
  2. if(isset($_POST['nex'])){
  3.  
  4. /**
  5. * ensure all the selected checkboxes pass their ids to a new array
  6. *
  7. */
  8.  
  9. print_r($_POST['dname']);
  10. echo "<br />";
  11.  
  12. foreach($_POST["dname"] as $key=>$val){
  13. echo "key: ". $key. " value: ". $val ."<br />\n";
  14.  
  15.  
  16.  
  17. // where to pass control to????
  18. // header('location:.php');
  19. echo "<br />";
  20. exit(); }
  21. }else if (isset ($_POST['cancel'])){
  22. // return to my_meeting.php
  23. header('location:my_meeting.php');
  24. echo "<br />";
  25. exit();
  26.  
  27. }
  28.  
  29.  
  30.  
  31. ?>


I wonder why???
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
jackakos is offline Offline
50 posts
since Sep 2008
Oct 22nd, 2008
0

Re: Validate Selected Checkboxes

Can somebody please be of HELP?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
jackakos is offline Offline
50 posts
since Sep 2008
Oct 23rd, 2008
0

Re: Validate Selected Checkboxes

Click to Expand / Collapse  Quote originally posted by jackakos ...
Can somebody please be of HELP?
Hello jackakos,
post your whole code of that page...i will check it now...
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Oct 23rd, 2008
0

Re: Validate Selected Checkboxes

Kindly find below the whole code as requested


php Syntax (Toggle Plain Text)
  1.  
  2. // Make a MySQL Connection
  3. $conn = mysql_connect("", "", "")
  4. or die(mysql_error());
  5. mysql_select_db("",$conn) or die(mysql_error());
  6.  
  7. $my_list = "select emp_no, concat_ws(', ', lname, fname) as display_name
  8. from employee order by lname, fname";
  9.  
  10. $my_list_res = mysql_query($my_list) or die(mysql_error());
  11.  
  12. if (mysql_num_rows($my_list_res) < 1) {
  13. //no records
  14. echo "<p><em>Sorry, no records to select!</em></p>";
  15.  
  16. } else {
  17. // array that accepts the employee list - shd use $_SESSION???
  18. $hello_array[] = $my_list_res;
  19. ?>
  20. <!-- major table starts here -->
  21. <center>
  22. <form name="slip" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
  23.  
  24. <?php
  25. echo "<table border='1'>";
  26. echo "<tr> <th>Last Name, First Name</th> <th>Check / UnCheck </th> </tr>";
  27. // keeps getting the next row until there are no more to get
  28.  
  29.  
  30. while($row = mysql_fetch_array( $get_list_res )) {
  31. // Print out the contents of each row into a table
  32. echo "<tr><td>";
  33. //echo $row['name'];
  34. //echo $_SESSION['ids'] = $_POST['eventtime'];
  35.  
  36. echo $display_name = stripslashes($row['display_name']);
  37. $ids[] = $row['emp_no'];
  38. $_SESSION['ids'] = $ids;
  39. echo "</td><td>";
  40. echo "<input type=\"checkbox\" name=\"dname[]\" value=\"row['emp_no']\">";
  41. //echo "<input type=\"checkbox\" name=\"dname[]\" value=\"ids\">";
  42. echo "</td> </tr>";
  43. }
  44. }
  45. echo "</table>";
  46. ?>
  47. </center>
  48. <!-- Cancel and Next Buttons should be placed in a form -->
  49. <br />
  50. <br />
  51. <center>
  52.  
  53. <input type="Submit" name="cancel" value="CANCEL!!!"> &nbsp; &nbsp; &nbsp; &nbsp;
  54. <input type="submit" name="nex" value="Next">
  55. </form></center>
  56.  
  57. <?php
  58.  
  59.  
  60. if(isset($_POST['nex'])){
  61.  
  62. /**
  63. * ensure all the selected checkboxes pass their ids to a new array
  64. *
  65. */
  66.  
  67. print_r($_POST['dname']);
  68. echo "<br />";
  69. echo "<br />";
  70. foreach($_POST["dname"] as $key=>$val){
  71. echo "key: ". $key. " value: ". $val ."<br />\n";
  72.  
  73. $check_array[] = $val;
  74. $_SESSION['check_array'] = $check_array;
  75.  
  76. //echo "my checked boxes are: ". $check_array;
  77. // where to pass control to????
  78. // header('location:.php');
  79. echo "<br />";
  80. exit();
  81. }
  82. }else if (isset ($_POST['cancel'])){
  83. // return to my_meeting.php
  84. header('location:my_meeting.php');
  85. echo "<br />";
  86. exit();
  87.  
  88. }
  89.  
  90.  
  91.  
  92.  
  93. ?>
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
jackakos is offline Offline
50 posts
since Sep 2008
Oct 23rd, 2008
0

Re: Validate Selected Checkboxes

i checked your code...
why are you using checkbox code in echo statement...thats why the problem raises...
use it as html code..
here is update code...that database belongs to my table..please change your database values accordingly...
php Syntax (Toggle Plain Text)
  1. <? // Make a MySQL Connection
  2. $conn = mysql_connect("localhost", "root", "1234")
  3. or die(mysql_error());
  4. mysql_select_db("iisspl",$conn) or die(mysql_error());
  5.  
  6. $my_list = "select fname,lname
  7. from is_users order by lname, fname";
  8.  
  9. $my_list_res = mysql_query($my_list) or die(mysql_error());
  10.  
  11. if (mysql_num_rows($my_list_res) < 1) {
  12. //no records
  13. echo "<p><em>Sorry, no records to select!</em></p>";
  14.  
  15. } else {
  16. // array that accepts the employee list - shd use $_SESSION???
  17. $hello_array[] = $my_list_res;
  18. ?>
  19. <!-- major table starts here -->
  20.  
  21. <form name="slip" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" onSubmit="return chkaddad();">
  22.  
  23. <script language="javascript">
  24. function chkaddad()
  25. {
  26. isChecked1=false
  27. for(var i=0;i<document.forms["slip"]["dname[]"].length;i++){
  28. if(document.forms["slip"]["dname[]"][i].checked){
  29. isChecked1=true
  30. }
  31. }
  32. if(!isChecked1){
  33. alert('Atleast select one check box ..');
  34. return false;
  35. }
  36.  
  37.  
  38. }
  39. </script>
  40. <table border='1'>
  41. <tr> <th>Last Name, First Name</th> <th>Check / UnCheck </th> </tr>
  42. // keeps getting the next row until there are no more to get
  43. <?php
  44. while($row = mysql_fetch_array( $my_list_res )) {
  45. // Print out the contents of each row into a table
  46.  
  47. //echo $row['name'];
  48. //echo $_SESSION['ids'] = $_POST['eventtime'];
  49.  
  50. echo $display_name = stripslashes($row['fname']);
  51. $ids[] = $row['lname'];
  52. $_SESSION['uid'] = $ids;
  53. ?>
  54. <tr><td><? echo $display_name;?>
  55. </td><td>
  56.  
  57. <input name="dname[]" type="checkbox" value="<?=$row['fname']?>">
  58.  
  59. </td> </tr>
  60. <?
  61. }
  62. }?>
  63. </table>
  64.  
  65. </center>
  66. <!-- Cancel and Next Buttons should be placed in a form -->
  67. <br />
  68. <br />
  69.  
  70. <input type="Submit" name="cancel" value="CANCEL!!!"> &nbsp; &nbsp; &nbsp; &nbsp;
  71. <input type="submit" name="nex" value="Next" onClick="return chkaddad();">
  72. </form>
  73.  
  74. <?php
  75.  
  76.  
  77. if(isset($_POST['nex'])){
  78.  
  79. /**
  80. * ensure all the selected checkboxes pass their ids to a new array
  81. *
  82. */
  83.  
  84. print_r($_POST['dname']);
  85. echo "<br />";
  86. echo "<br />";
  87. foreach($_POST["dname"] as $key=>$val){
  88. echo "key: ". $key. " value: ". $val ."<br />\n";
  89.  
  90. $check_array[] = $val;
  91. $_SESSION['check_array'] = $check_array;
  92.  
  93. //echo "my checked boxes are: ". $check_array;
  94. // where to pass control to????
  95. // header('location:.php');
  96. echo "<br />";
  97. exit();
  98. }
  99. }else if (isset ($_POST['cancel'])){
  100. // return to my_meeting.php
  101. header('location:my_meeting.php');
  102. echo "<br />";
  103. exit();
  104.  
  105. }
  106. ?>
Last edited by Shanti C; Oct 23rd, 2008 at 6:43 am.
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: how create subdomain using php
Next Thread in PHP Forum Timeline: semicolon delimited importable text file don't load into my table





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC