Validate Selected Checkboxes
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 -->
<center>
<input type="Submit" name="cancel" value="CANCEL!!!" action="my_meeting.php">
<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 "";
exit();
}else if (isset ($_POST['cancel'])){
// return to uncle.php
header('location:uncle.php');
echo "";
exit();
}
?>
jackakos
Junior Poster in Training
50 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
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>
AndB. 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"]);
Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
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.
jackakos
Junior Poster in Training
50 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
try this:
foreach($_POST["dname"] as $key=>$val){
echo "key: ". $key. " value: ". $val ."\n";
Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
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 -->
<center>
<input type="Submit" name="cancel" value="CANCEL!!!">
<input type="submit" name="nex" value="Next">
</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
if(isset($_POST['nex'])){
/**
* ensure all the selected checkboxes pass their ids to a new array
*
*/
print_r($_POST['dname']);
echo "";
foreach($_POST["dname"] as $key=>$val){
echo "key: ". $key. " value: ". $val ."\n";
// where to pass control to????
// header('location:.php');
echo "";
exit(); }
}else if (isset ($_POST['cancel'])){
// return to my_meeting.php
header('location:my_meeting.php');
echo "";
exit();
}
?>
I wonder why???
jackakos
Junior Poster in Training
50 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
Can somebody please be of HELP?
jackakos
Junior Poster in Training
50 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
Can somebody please be of HELP?
Hello jackakos,
post your whole code of that page...i will check it now...
Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
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 -->
<center>
<input type="Submit" name="cancel" value="CANCEL!!!">
<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 "";
echo "";
foreach($_POST["dname"] as $key=>$val){
echo "key: ". $key. " value: ". $val ."\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 "";
exit();
}
}else if (isset ($_POST['cancel'])){
// return to my_meeting.php
header('location:my_meeting.php');
echo "";
exit();
}
?>
jackakos
Junior Poster in Training
50 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
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 -->
<input type="Submit" name="cancel" value="CANCEL!!!">
<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 "";
echo "";
foreach($_POST["dname"] as $key=>$val){
echo "key: ". $key. " value: ". $val ."\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 "";
exit();
}
}else if (isset ($_POST['cancel'])){
// return to my_meeting.php
header('location:my_meeting.php');
echo "";
exit();
}
?>
Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
Thanks so much, it is working!!!
jackakos
Junior Poster in Training
50 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0