• Sign in to Neowin Faster!

    Create an account on Neowin to contribute and support the site.

  • 0
Sign in to follow this  

[PHP] Checkbox Return Value

Question

mcs2k1    0

Can anyone teach me how to return a value of a checkbox even when it is not checked?

I want to return the values that is checked and the values that is not checked, here:

[ ] One

[/] Two

[ ] Three

[/] Four

[/] Five

and will return:

Checked:

Two, Four, Five

Unchecked:

One, Three

Thank you very much, :)

Share this post


Link to post
Share on other sites

9 answers to this question

Recommended Posts

  • 0
metal_dragen    30

You can't get a value from an unchecked checkbox because no value gets passed. If you want to know that it is unchecked, you have to check for the absence of it when the form is submitted.

Share this post


Link to post
Share on other sites
  • 0
+theblazingangel    137

php:

if (isset($_POST['checkbox_1'])) { $checkboxArray['c1'] = true; } else { $checkboxArray['c1'] = false; }

Share this post


Link to post
Share on other sites
  • 0
Mathachew    149

One trick I've seen some develops do is to have a hidden field with a false value, while the equivalent checkbox (using the same field name) has a true value. If it's true, it's checked, if it's false, unchecked. I never liked using this.

Share this post


Link to post
Share on other sites
  • 0
mcs2k1    0

Thank you so much guys, your replies gave me ideas on how to do it. Actually what I really want to do is to add a value to the database if it is checked and remove it if it is unchecked (supposedly it is already in the database).

like this:

first run,

[ ] One

[ ] Two

[ ] Three

[ ] Four

[ ] Five

database contents: none

then, i'll check the values,

[ ] One

[/] Two

[ ] Three

[/] Four

[/] Five

submit the form

then, Two, Four and Five will be stored to the database.

2nd run,

Two, Four and Five will be automatically checked

[ ] One

[/] Two

[ ] Three

[/] Four

[/] Five

then, i'll uncheck Two

[ ] One

[ ] Two

[ ] Three

[/] Four

[/] Five

submit the form

then, Two will be removed from the database, but Four and Five will remain.

-----

I can do this by truncating the table every time before entering the values,

	if (isset($_POST['curr_check'])) {
		mysql_query("TRUNCATE TABLE `subjtaken_{$stud}`") or die("Could not execute query : " . mysql_error()); // deletes all values from subjtaken table

		if ($box!=null){
			foreach ($box as $f) { // outputs array containing checkbox value
				$value = stripslashes($f);
				$query = "INSERT INTO subjtaken_{$stud} (SubjCode, Remark, Comment) VALUES ($value)";
				mysql_query($query) or die("Could not execute query : " . mysql_error());
			}
		}

but are there other ways on doing this without truncating the table? i don't want to truncate the table because i want to save the contents of the database.

Share this post


Link to post
Share on other sites
  • 0
nvme    3

you can select all items that were previously set to checked in the database. then loop through your selected checkboxes and the list you got from the db. if the current list of checked checkboxes excludes an item that was found in the db, add it to one list so you can remove them from the db. then do pretty much the same thing to record all newly checked items not found in the db so they can be added.

it may sound a little complicated but it works.

if you do choose to do it the above way, consider using array names in your html (name all the checkboxes the same thing and include square brackets at the end. ex: checkboxes[]). php will automatically see them as an array then ($_POST['checkboxes'][0], $_POST['checkboxes'][1], ...) and it'll make it easier to loop through them.

Share this post


Link to post
Share on other sites
  • 0
mcs2k1    0

here's the code, it's working but it uses the truncate method:

<?php

include("connect.php");
include("minifunctions.php");
?>
<html>
<head>
<title></title>
<link href="jomapa.css" rel="stylesheet" type="text/css" />
</head>
<body>
<center>
<?php
$stud = (int)$_POST['stud'];

if (isset($_POST['curr_index']) OR isset($_POST['curr_check'])) {
	$num = mysql_num_rows(mysql_query("SELECT * FROM students WHERE StudNum={$stud}"));
	if ($num > 0 ) { //proceed if student number exists

		$fName = mysql_result(mysql_query("SELECT * FROM students WHERE StudNum={$stud}"),0,"FirstName");
		$mName = mysql_result(mysql_query("SELECT * FROM students WHERE StudNum={$stud}"),0,"MiddleName");
		$lName = mysql_result(mysql_query("SELECT * FROM students WHERE StudNum={$stud}"),0,"LastName");
		$dept = strtolower(mysql_result(mysql_query("SELECT * FROM students WHERE StudNum={$stud}"),0,"DeptCode"));
		$deptName = mysql_result(mysql_query("SELECT * FROM departments WHERE DeptCode=\"{$dept}\""),0,"DeptName");
		$curr = strtolower(mysql_result(mysql_query("SELECT * FROM students WHERE StudNum={$stud}"),0,"CurrCode"));
		$currName = mysql_result(mysql_query("SELECT * FROM curriculums WHERE CurrCode=\"{$curr}\""),0,"CurrName");
		$currSY = mysql_result(mysql_query("SELECT * FROM curriculums WHERE CurrCode=\"{$curr}\""),0,"CurrSY");
		$box = $_POST["box"];

	}
	else {
		echo "<b>No Match Found</b>";
	}
}
if ((isset($_POST['curr_index']) OR isset($_POST['curr_check'])) AND $num > 0) {
	echo "{$stud}: {$lName}, {$fName} {$mName} ", strtoupper($dept), " {$curr} <br /><br />";
	curr_header($deptName, $currName, $currSY);

	echo "<form method=\"post\" action=\"$PHP_SELF\">";	

	if (isset($_POST['curr_check'])) {
		mysql_query("TRUNCATE TABLE `subjtaken_{$stud}`") or die("Could not execute query : " . mysql_error()); // deletes all values from subjtaken table

		if ($box!=null){
			foreach ($box as $f) { // outputs array containing checkbox value
				$value = stripslashes($f);
				$query = "INSERT INTO subjtaken_{$stud} (SubjCode, Remark, Comment) VALUES ($value)";
				mysql_query($query) or die("Could not execute query : " . mysql_error());
			}
		}

	}

	$query="SELECT SubjYear, SubjSem FROM curr_{$dept}_{$curr} WHERE SubjYear IS NOT NULL AND SubjSem IS NOT NULL GROUP BY SubjYear, SubjSem";
	$result=mysql_query($query);
	$count=0;

	while(mysql_fetch_row($result))	{ // display tables
		$year=mysql_result($result,$count,"SubjYear");
		  $sem=mysql_result($result,$count,"SubjSem");
		curr_disp_check($year,$sem,$dept,$curr,$stud);
		$count++;
	}
echo "<input type=\"hidden\" value=\"$stud\" name=\"stud\">"; // hold value for variable stud
echo "<input type=\"submit\" value=\"Submit\" name=\"curr_check\"> <input type=\"reset\" name=\"reset\" value=\"Reset\">";
echo "</form>";
}
?>
</form>
</center>
</body>
</html>
<?php
function curr_disp_check($year,$sem,$dept,$curr,$stud) {

	$query="SELECT SubjCode FROM subjtaken_{$stud} WHERE Remark=1";
	$result=mysql_query($query);
	$count=0;

	while(mysql_fetch_row($result)) {
	$SubjCode=mysql_result($result,$count,"SubjCode");
	$prereq[]="$SubjCode";
	$count++;
	}

	//Start of Header

	echo "<table class=\"Table1\" cellspacing=\"0\" cellpadding=\"0\" width=\"750px\">";
   	echo "<tr>";
	echo "<td colspan=\"2\" class=\"headerTD\" width=\"90px\"><center>";
	switch ($year) {
	case 1: echo "FIRST YEAR";
			break;
	case 2: echo "SECOND YEAR";
			break;
	case 3: echo "THIRD YEAR";
			break;
	case 4: echo "FOURTH YEAR";
			break;
	case 5:	echo "FIFTH YEAR";
			break;
	}
	echo "</center></td>";
	echo "<td class=\"headerTD\" width=\"225px\">";
	switch ($sem) {
	case 1: echo "FIRST SEMESTER";
			break;
	case 2: echo "SECOND SEMESTER";
			break;
	case 3: echo "SUMMER";
			break;
	}
	echo "</td>";
	echo "<td class=\"headerTD\" width=\"30px\"><center>Lec</center></td>";
	echo "<td class=\"headerTD\" width=\"30px\"><center>Lab</center></td>";
	echo "<td class=\"headerTD\" width=\"40px\"><center>Units</center></td>";
	echo "<td class=\"headerTD\" width=\"210px\">Prerequisites</td>";
	echo "<td class=\"headerTD\" width=\"140px\">Old Course Number</td>";
	echo "</tr>";

	//Start of Contents

	$query="SELECT curr_{$dept}_{$curr}.*,subjtaken_{$stud}.Remark FROM abrantes_thesis.subjtaken_{$stud} RIGHT JOIN abrantes_thesis.curr_{$dept}_{$curr} ON (subjtaken_{$stud}.SubjCode = curr_{$dept}_{$curr}.SubjCode) WHERE (SubjYear=$year AND SubjSem=$sem) ORDER BY SubjOrder";
	$result=mysql_query($query);
	$count=0;

	while(mysql_fetch_row($result)) {
	$SubjCode=mysql_result($result,$count,"SubjCode");
	$SubjName=mysql_result($result,$count,"SubjName");
	$UnitLec=mysql_result($result,$count,"UnitLec");
	$UnitLab=mysql_result($result,$count,"UnitLab");
	$PreReq1=mysql_result($result,$count,"PreReq1");
	$PreReq2=mysql_result($result,$count,"PreReq2");
	$PreReq3=mysql_result($result,$count,"PreReq3");
	$PreReq4=mysql_result($result,$count,"PreReq4");
	$OldCourseNum=mysql_result($result,$count,"OldCourseNum");
	$SubjType=mysql_result($result,$count,"SubjType");
	$Remark=mysql_result($result,$count,"Remark");

	echo "<tr ";
	if ($Remark != 1){
		echo "onmouseover=className=\"navon\" onmouseout=className=\"navoff\">";
	}
	else {
		echo "onmouseover=className=\"navon\" onmouseout=className=\"subjtaken\" class=\"subjtaken\">";
	}
	echo "<td class=\"mainTD\" width=\"25px\" ><input type=\"checkbox\""; //combobox
	echo " value=\"'{$SubjCode}', '1', NULL\""; //combobox value
	echo " name=\"box[]\""; //combobox name
		if ($Remark != 1){ // ifelse statement for determining if checkbox will be marked as checked or not
			echo ">";
				}
		else {
			echo "checked>";
		}
	echo "</td>";
	echo "<td class=\"mainTD\" width=\"90px\"><div class=\"subjcode\">$SubjCode</div></td>";
	echo "<td class=\"mainTD\" width=\"225px\"><div class=\"subjname\">$SubjName</div></td>";
	echo "<td class=\"mainTD\" width=\"30px\"><center>$UnitLec</center></td>";
	echo "<td class=\"mainTD\" width=\"30px\"><center>$UnitLab</center></td>";
	echo "<td class=\"mainTD\" width=\"40px\"><center>";
	echo $UnitLec+$UnitLab;
	echo "</center></td>";
	echo "<td class=\"mainTD\" width=\"210px\">";
	$pr=array($PreReq1,$PreReq2,$PreReq3,$PreReq4,NULL); 
	for($i=0;$i<4;$i++){ 
		if ($pr[$i] != NULL){
			echo "<font color=";
				if ($prereq != FALSE AND in_array($pr[$i],$prereq)){
					echo "\"black\">";
				}
				else {
					echo "\"red\">";
				}
			echo "$pr[$i]";
			echo "</font>";
		if($pr[$i+1] != NULL){
				echo ", ";
			}
		}
	}
	echo "</td>";
	echo "<td class=\"mainTD\" width=\"140px\">$OldCourseNum</td>";
	$count++;
	}
	echo "</tr>";

	//Start of Sum

	echo "<tr>";
	echo "<td></td>";
	echo "<td></td>";
	echo "<td></td>";
 	echo "<td class=\"sumTD\" width=\"30px\"><center>";
	echo mysql_result(mysql_query("SELECT sum(UnitLec) AS LecTotal FROM curr_{$dept}_{$curr} WHERE subjtype IS NULL AND (subjyear=$year AND subjsem=$sem)"),0);
	echo "</center></td>";
	echo "<td class=\"sumTD\" width=\"30px\"><center>";
	echo mysql_result(mysql_query("SELECT sum(UnitLab) AS LabTotal FROM curr_{$dept}_{$curr} WHERE subjtype IS NULL AND (subjyear=$year AND subjsem=$sem)"),0);
	echo "</center></td>";
	echo "<td class=\"sumTD\" width=\"40px\"><center>";
	echo mysql_result($result=mysql_query("SELECT sum(UnitLec) + sum(UnitLab) AS UnitTotal FROM curr_{$dept}_{$curr} WHERE subjtype IS NULL AND (subjyear=$year AND subjsem=$sem)"),0);
	echo "</center></td>";
	echo "<td></td>";
	echo "<td></td>";
	echo "</table>";
}
?>

Share this post


Link to post
Share on other sites
  • 0
Trix.Neo    1

In a similar situation I simply delete all the records associated with the primary instance say a user and his forum groups and then re-enter those that have been checked, so that would just require to queries if you do it right?

Share this post


Link to post
Share on other sites
  • 0
mcs2k1    0
One trick I've seen some develops do is to have a hidden field with a false value, while the equivalent checkbox (using the same field name) has a true value. If it's true, it's checked, if it's false, unchecked. I never liked using this.

This solved my problem, =) Thank you very much, ;)

Share this post


Link to post
Share on other sites
  • 0
FlibbyFlobby    0

Surely the simplest solution is to always update the values in the database to be 0 (or whatever your using to denote off) when the request method is POST, then loop through and set the ones that are checked in the $_POST array after. That way you have the same end results. It's the simplest solution really.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.