Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using PHP and html checkboxes

Status
Not open for further replies.

Erikxxx

Programmer
May 5, 2003
49
0
0
GB
Hi all,

I've got a dynamic HTML form driven by a database with a group of checkboxes. I haven't got any problems taking the values from the checkboxes and inserting them in the database or populating the checkboxes with corresponding values from the database. My problem is that if a user un-select a checkbox I don't know how I can program my PHP to detect this. Now the checked values are passed in an array through form submit but I don't know how to deal with the unchecked, since also these values needs to be stored in the database. Also my dynamic html page doesn't have a constent number of checkboxes. These could vary from 1 to many.

My db table looks like this

objId Team value (checked/not checked)
1 1 0
1 2 0
1 3 1

2 1 1
2 6 0

Thanks
Erik
 
Even though you have a dynamic number of checkboxes on the page, you have code which decided what/how many checkboxes are displayed using some sort of constraints.

If this is correct then you can carry those constraints over to the processing page. Since you know the constraints you know what was displayed and what did not come back (unchecked).
 
Thanks for your tips
It took me a little while to understand the logic but now it's working perfectly well.

Thanks
Erik
 
Erik could u shoe me how to populate checbox values with data from mysql and one check box for each record in my query. I am intrsted in building a dynamic form .Thanks
 
Keith23

the thing to remember about standard checkboxes is that they do not submit through a form unless they are checked. i tend to use "enhanced" checkboxes which always return a value. the value is actually stored in a hidden text field and the checkbox just instantiates some javascript to changes the hidden text value.

Anyway - below is some code that will show you the rudiments of checkbox handling. just cut and paste it into a web page and run to see how it functions. Sorry it's so long - i wanted to make it comprehensive for you.

Code:
<head>
<style type="text/css">
body{ background-color:#FFFFCC; font-family:Verdana, Arial, Helvetica, sans-serif;}
fieldset {width:30%;}
.code 
{
	font-family:"Courier New", Courier, monospace;
	width: 80%;
	margin-left:30px;
	background-color:#FFFFFF;
}

</style>
</head>
<body>
<?
/*
@mysql_connect($database, $user, $password) 
  or die ("Can't connect to database server");
@mysql_select_db($database) 
  or die ("Can't connect to database");
$query = "Select name, adminrights from users";
$results = mysql_query($query);
*/

//let's dummy some data to use for debugging
$row = array
(
	array
		(	"id"=>1,
			"name"=>"Justin",
			"adminrights"=>1),
	array
		(	"id"=>2,
			"name"=>"Heidi",
			"adminrights"=>0)
);
$count = count($row);
?>
<div class="cdiv">
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<fieldset><legend>Test Form</legend>
<table >
<tr><td align="left">Name</td><td align="center">Admin Rights</td></tr>
<? 
//while ($row = mysql_fetch_assoc($results)):
for ($i=0; $i<$count; $i++): //comment this line out and remove the comment on the line above for use with the database
 $id ="";
 $name="";
 $adminrights=0;
 extract($row[$i]);  // this overwrites the values of the three vars above
//extract ($row); 	 // remove comments on this line and comment line above out if you are using with a database  
?>
	<tr>
		<td>
		   <input 
			  type="text" 
			  name="name[]" 
			  value="<?=$name?>"/>
			<input
				type="hidden"
				name="id[]"
				value="<?=$id?>" />		
		</td>
		<td>
		   <input
			  type="checkbox"
			  name="adminrights[<?=$id?>]"
			  value="1" 
			  <? echo $adminrights==1 ? "checked=checked":""; ?> />
		</td>
	</tr>
<? endfor; //	CHANGE TO ENDWHILE IF YOU ARE USING WITH DATABASE?>
	<tr>
		<td colspan="2">
			<input 
				type="submit"
				name="submit"
				value="Submit" />
		</td>
	</tr>
</table>
</fieldset>
</form>
</div>
<br/>
<div class="results">
<? 
if (isset($_POST['submit'])):
	echo "here are the values that the form received: <br/><div class=\"code\"><pre>";
	print_r($_POST); 
?>
</pre></div><br/>To use them in a database you would do something like this":
</div><br/>
<div class="code"> 
<pre>
	$count = count($_POST['name']);
	for ($i=0; $i<$count; $i++):
		$adminrights = isset($_POST['adminrights'][$i])?1:0;
		mysql_query ("	
			REPLACE
				into `users` 
			SET
				`id` = '".mysql_escape_string($_POST['id'][$i]) . "',
				`name` = '".mysql_escape_string($_POST['name'][$i]) . "',
				`adminrights` = $adminrights");		
	endfor;
</pre>
</div>
This would give you a set of sql calls like this <br/> <br/> <div class="code">
<?
	$count = count($_POST['name']);
	for ($i=0; $i<$count; $i++):
		$adminrights = isset($_POST['adminrights'][$i])?1:0;
		echo "	
			REPLACE
				into `users` 
			SET
				`id` = '".mysql_escape_string($_POST['id'][$i]) . "',
				`name` = '".mysql_escape_string($_POST['name'][$i]) . "',
				`adminrights` = $adminrights<br/>";		

	endfor;
echo "</div>";
endif;
?>
</body>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top