| | |
Validate Selected Checkboxes
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
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.
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)
// 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"> <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:
That will get you something like this after you submit the form with a couple of employees checked:
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.
Hope that helps, let me know if I totally misunderstood your question
Great one jackakos,
Chrelad
php Syntax (Toggle Plain Text)
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)
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['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)
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
javascript Syntax (Toggle Plain Text)
<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>
•
•
•
•
B. I have to store the value of the selected checkboxes into an array
this single line will get all checked ones with comma sepperated,then you can insert them in your database..
PHP Syntax (Toggle Plain Text)
$comma_checkedones = implode(",", $_POST["dname"]);
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
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.
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.
try this:
PHP Syntax (Toggle Plain Text)
foreach($_POST["dname"] as $key=>$val){ echo "key: ". $key. " value: ". $val ."<br />\n";
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
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:
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.
I wonder why???
connection and query:
php Syntax (Toggle Plain Text)
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!!!"> <input type="submit" name="nex" value="Next"> </form></center>
//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)
<?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???
Kindly find below the whole code as requested
php Syntax (Toggle Plain Text)
// 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!!!"> <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...
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)
<? // 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!!!"> <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(); } ?>
Last edited by Shanti Chepuru; Oct 23rd, 2008 at 6:43 am.
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
![]() |
Other Threads in the PHP Forum
- Previous Thread: how create subdomain using php
- Next Thread: semicolon delimited importable text file don't load into my table
| Thread Tools | Search this Thread |
ajax apache api array arrays beginner binary broken cache cakephp checkbox class cms code confirm cron curl customizableitems database date display dynamic echo email error external file files folder form forms forum function functions google header headmethod howtowriteathesis href htaccess html iframe image include insert integration ip java javascript joomla limit link login loop mail malfunction menu method mlm multiple mysql neutrality oop paypal pdf php phpmysql play problem query question radio random recursion regex remote root script search select server sessions sms soap source space sql syntax system table tutorial update upload url validator variable video web xml youtube






