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.

// Make a MySQL Connection
 $conn = mysql_connect("", "", "")
      or die(mysql_error());
   mysql_select_db("",$conn) or die(mysql_error());

$my_list = "select emp_no, concat_ws(', ', lname, fname) as display_name
         from employee order by lname, fname";
         
$my_list_res = mysql_query($my_list) or die(mysql_error());

if (mysql_num_rows($my_list_res) < 1) {
         //no records
         echo "<p><em>Sorry, no records to select!</em></p>";
         
} else {
	// array that accepts the employee list - shd use $_SESSION???
	$hello_array[] = $my_list_res;
?>
<!-- major table starts here -->
<center>
<form name="slip" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> 

<?php
echo "<table border='1'>";
echo "<tr> <th>Last Name, First Name</th> <th>Check / UnCheck </th> </tr>";
// keeps getting the next row until there are no more to get


while($row = mysql_fetch_array( $get_list_res )) {
	// Print out the contents of each row into a table
	echo "<tr><td>"; 
	//echo $row['name'];
	echo $display_name = stripslashes($row['display_name']);
	echo "</td><td>"; 
	echo "<input type=\"checkbox\" name=\"dname[]\" value=\"row['emp_no']\">";
	echo "</td> </tr>"; 
} 
}
echo "</table>";
?>
</center>
<!-- Cancel and Next Buttons should be placed in a form -->
<br />
<br />
<center>

<input type="Submit" name="cancel" value="CANCEL!!!" action="my_meeting.php"> &nbsp; &nbsp; &nbsp; &nbsp; 
<input type="submit" name="nex" value="Next"> 
</form></center>

<?php
if(isset($_POST['nex'])){
	
		/**
		 * ensure all the selected checkboxes pass their ids to a new array
		 * 
		 */
		 
		 
		
		
		header('location:auntie.php');
		echo "<br />";                    
		exit();
	}else if (isset ($_POST['cancel'])){
		// return to uncle.php
		header('location:uncle.php');
		echo "<br />";                    
		exit();
			
	}
	

?>

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:

print_r($_POST['dname']);

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

Array(
  0 => 21,
  3 => 34,
  7 => 31
);

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

foreach($_POST['dname'] as $employeeId)
{
  // Do your magic stuff here
}

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

Great one jackakos,

Chrelad

A. I have to validate the selection made

this is the code:

<script type="text/javascript">
function chkChecks(){
isChecked=false
for(var i=0;i<document.forms["new_page"]["allowed[]"].length;i++){
if(document.forms["new_page"]["allowed[]"][i].checked){
isChecked=true
}
}
if(isChecked){
document.forms["new_page"].submit()
}
else{
alert('Please select a checkbox')
}
}
</script>

And

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..

$comma_checkedones = implode(",", $_POST["dname"]);

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 [1] => row [2] => row )
row
row
row

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.

try this:

foreach($_POST["dname"] as $key=>$val){
  echo "key: ". $key. " value: ". $val ."<br />\n";

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:

while($row = mysql_fetch_array( $get_list_res )) {
	// Print out the contents of each row into a table
	echo "<tr><td>"; 
	//echo $row['name'];
	//echo $_SESSION['ids'] = $_POST['eventtime'];
	echo $ids[] = $row['emp_no'];
	echo $display_name = stripslashes($row['display_name']);
	echo "</td><td>"; 
	echo "<input type=\"checkbox\" name=\"dname[]\" value=\"row['emp_no']\">";
	echo "</td> </tr>"; 
} 
}
echo "</table>";
?>
</center>
<!-- Cancel and Next Buttons should be placed in a form -->
<br />
<br />
<center>

<input type="Submit" name="cancel" value="CANCEL!!!"> &nbsp; &nbsp; &nbsp; &nbsp; 
<input type="submit" name="nex" value="Next"> 
</form></center>

If I remove the comments on
//echo $ids[] = $row;

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
if(isset($_POST['nex'])){
	
		/**
		 * ensure all the selected checkboxes pass their ids to a new array
		 * 
		 */
		 
		 print_r($_POST['dname']);
		 echo "<br />";
		 
		 foreach($_POST["dname"] as $key=>$val){
  		echo "key: ". $key. " value: ". $val ."<br />\n";
		 
		 
		
		// where to pass control to????
	//	header('location:.php');
		echo "<br />";                    
		exit(); }
	}else if (isset ($_POST['cancel'])){
		// return to my_meeting.php
		header('location:my_meeting.php');
		echo "<br />";                    
		exit();
			
	}
	
	

?>

I wonder why???

Can somebody please be of HELP?

Can somebody please be of HELP?

Hello jackakos,
post your whole code of that page...i will check it now...

Kindly find below the whole code as requested

// Make a MySQL Connection
 $conn = mysql_connect("", "", "")
      or die(mysql_error());
   mysql_select_db("",$conn) or die(mysql_error());

$my_list = "select emp_no, concat_ws(', ', lname, fname) as display_name
         from employee order by lname, fname";
         
$my_list_res = mysql_query($my_list) or die(mysql_error());

if (mysql_num_rows($my_list_res) < 1) {
         //no records
         echo "<p><em>Sorry, no records to select!</em></p>";
         
} else {
	// array that accepts the employee list - shd use $_SESSION???
	$hello_array[] = $my_list_res;
?>
<!-- major table starts here -->
<center>
<form name="slip" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> 

<?php
echo "<table border='1'>";
echo "<tr> <th>Last Name, First Name</th> <th>Check / UnCheck </th> </tr>";
// keeps getting the next row until there are no more to get


while($row = mysql_fetch_array( $get_list_res )) {
	// Print out the contents of each row into a table
	echo "<tr><td>"; 
	//echo $row['name'];
	//echo $_SESSION['ids'] = $_POST['eventtime'];
	
	echo $display_name = stripslashes($row['display_name']);
	$ids[] = $row['emp_no'];
	$_SESSION['ids'] = $ids;
	echo "</td><td>"; 
	echo "<input type=\"checkbox\" name=\"dname[]\" value=\"row['emp_no']\">";
	//echo "<input type=\"checkbox\" name=\"dname[]\" value=\"ids\">";
	echo "</td> </tr>"; 
} 
}
echo "</table>";
?>
</center>
<!-- Cancel and Next Buttons should be placed in a form -->
<br />
<br />
<center>

<input type="Submit" name="cancel" value="CANCEL!!!"> &nbsp; &nbsp; &nbsp; &nbsp; 
<input type="submit" name="nex" value="Next"> 
</form></center>

<?php


if(isset($_POST['nex'])){
	
		/**
		 * ensure all the selected checkboxes pass their ids to a new array
		 * 
		 */
		 
		 print_r($_POST['dname']);
		 echo "<br />";
		 echo "<br />";
		 foreach($_POST["dname"] as $key=>$val){
  		echo "key: ". $key. " value: ". $val ."<br />\n";
  	
		 $check_array[] = $val;
		 $_SESSION['check_array'] = $check_array;
		 
		 //echo "my checked boxes are: ". $check_array;		
		// where to pass control to????
	//	header('location:.php');
		echo "<br />";                    
		exit(); 
		}
	}else if (isset ($_POST['cancel'])){
		// return to my_meeting.php
		header('location:my_meeting.php');
		echo "<br />";                    
		exit();
			
	}
	 
	
	

?>

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...

<? // Make a MySQL Connection
 $conn = mysql_connect("localhost", "root", "1234")
      or die(mysql_error());
   mysql_select_db("iisspl",$conn) or die(mysql_error());

$my_list = "select fname,lname
         from is_users order by lname, fname";
         
$my_list_res = mysql_query($my_list) or die(mysql_error());

if (mysql_num_rows($my_list_res) < 1) {
         //no records
         echo "<p><em>Sorry, no records to select!</em></p>";
         
} else {
	// array that accepts the employee list - shd use $_SESSION???
	$hello_array[] = $my_list_res;
?>
<!-- major table starts here -->

<form name="slip" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" onSubmit="return chkaddad();"> 

<script language="javascript">
function chkaddad()
{
 isChecked1=false
for(var i=0;i<document.forms["slip"]["dname[]"].length;i++){
if(document.forms["slip"]["dname[]"][i].checked){
isChecked1=true
}
}
if(!isChecked1){
alert('Atleast select one check box ..');
return false;
}
 

}
</script>
<table border='1'>
<tr> <th>Last Name, First Name</th> <th>Check / UnCheck </th> </tr>
// keeps getting the next row until there are no more to get
<?php
while($row = mysql_fetch_array( $my_list_res )) {
	// Print out the contents of each row into a table
	
	//echo $row['name'];
	//echo $_SESSION['ids'] = $_POST['eventtime'];
	
	echo $display_name = stripslashes($row['fname']);
	$ids[] = $row['lname'];
	$_SESSION['uid'] = $ids;
	?>
<tr><td><? echo $display_name;?>
	</td><td> 
	
	<input name="dname[]" type="checkbox" value="<?=$row['fname']?>">
	
	</td> </tr> 
	<?
} 
}?>
</table>

</center>
<!-- Cancel and Next Buttons should be placed in a form -->
<br />
<br />

<input type="Submit" name="cancel" value="CANCEL!!!"> &nbsp; &nbsp; &nbsp; &nbsp; 
<input type="submit" name="nex" value="Next"  onClick="return chkaddad();"> 
</form>

<?php


if(isset($_POST['nex'])){
	
		/**
		 * ensure all the selected checkboxes pass their ids to a new array
		 * 
		 */
		 
		 print_r($_POST['dname']);
		 echo "<br />";
		 echo "<br />";
		 foreach($_POST["dname"] as $key=>$val){
  		echo "key: ". $key. " value: ". $val ."<br />\n";
  	
		 $check_array[] = $val;
		 $_SESSION['check_array'] = $check_array;
		 
		 //echo "my checked boxes are: ". $check_array;		
		// where to pass control to????
	//	header('location:.php');
		echo "<br />";                    
		exit(); 
		}
	}else if (isset ($_POST['cancel'])){
		// return to my_meeting.php
		header('location:my_meeting.php');
		echo "<br />";                    
		exit();
			
	}
?>

Thanks so much, it is working!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.