Checkbox in Php

What is the behaviour of a checkbox in Php and how can i use it to select an item to delete just like the inbox of an email.

Thanks for helping :smiley:

First - Please do not double post. It is irritating and we will see it the first time.

Second - why is a HTML question in the database forum? It would probably do better in the General forum.

Third - a check box allows for singular or multiple selections and is handled as a simple variable in the singular case or as an array for the multiple case
(Ex: <input type= “checkbox” name = “chBox[]” …)

Sorry for that, I didn’t notice the other one.

Thanks for the help. Now I realize that I have to use an array to solve my problem.

I have another problem, I used the function MAX to obtain the maximum value in php. this the code:

$query2 = “SELECT MAX(SY) FROM curriculum WHERE CourseId=$c”;
$result2 = @mysql_query($query2);
$row2 = mysql_fetch_array($result2,MYSQL_ASSOC);
$year = $row2[‘SY’];

It return this message: Undefined variable SY.

if I change the last line to:

$year = $row[‘MAX(SY)’];

The variable $year is empty, no value.

Thanks for any help.

OK you want to remove the @ from the front of mysql_query. That surpresses any errors and right now we need to see them. If there are errors we need to know what the errors are and provide a way of handling them. Try something like:
$query2 = “SELECT MAX(SY) AS max FROM curriculum WHERE CourseId=$c”; // notice the AS part
$result2 = mysql_query($query2);
if (!$result2) // if there is a problem with the query
echo 'Error: There was a problem with the second query. ';
echo 'The problem was ’ . mysql_error(); // remove this in the production version
die; // stops the program fromgoing further
$row2 = mysql_fetch_array($result2,MYSQL_ASSOC);
// a quicker method is to use mysql_result since you are only getting 1 thing
$year = $row2[‘max’]; // identify it by the AS name

What I did before I visit the post is that I create a loop and test every data in the SY column but I think this will not make it quiker.

I try your suggestion and it works.

Thanks a lot.

Please make sure you do not double post in future (as Lig said), its highly irritating…

Please consider this a friendly warning…

Topic moved to GD

You dont need an array for a checkbox.

if ($_POST[‘box’] == “On”) {
//it was checked

if ($_POST[‘box’] == “On”) {
//it was checked

This scripts is ok but it only applies to a single box. My problem is I have several boxes, one for each item. How could I know that I have selected that Item so that I could delete it.


This works for me:

for($i=1; $i<5; $i++) { reset($VolForm_WorkChoices); $HData .= "n" . (string)$i . ':'; while( $element = each( $VolForm_WorkChoices ) ) {if ( $element[ 'value' ] == $i ) {$HData .= "n<P class='indentedtext'>" . $element[ 'key' ] . "</P>";} } }
There’s a lot of stuff here that you don’t need, or probably don’t, but it should give you a pretty good idea of how it can be done. I just happened to be working on this when I read your post.

Here’s the setup for it on the input side:

<TD><SELECT name = VolForm_WorkChoices[Bookroom_Attendant]> <OPTION value=""> </OPTION> <OPTION value="1">1st</OPTION> <OPTION value="2">2nd</OPTION> <OPTION value="3">3rd</OPTION> <OPTION value="4">4th</OPTION> </SELECT></TD>
Granted this is a select example rather than a checkbox, but you can see the use of the array and how it can be parsed on the output side, in this case an email constructor, to give the information desired.

Here’s an example using a matrix of checkboxes allowing the user to select which days of the week they are available:

if (!empty($VolForm_DaysAvailable))
	$HData .= "<P class='fields'>Availability: ".'</P>';
	foreach ($VolForm_DaysAvailable as $Available)
	  $HData .= "<P class='indentedtext'>" . $Available .'</P>';
	$HData .= "n<P class='fields'> No availability information given.</P>";

And here’s the input side:

<TD width= 50><P class = query>Morning</P></TD>
<TD align=center><INPUT type=checkbox name=VolForm_DaysAvailable[] value="Sunday Morning"></TD>
<TD align=center><INPUT type=checkbox name=VolForm_DaysAvailable[] value="Monday Morning"></TD>

And it goes on from there with Tuesday Morning and Wednesday, etc.

Haven’t included any error trapping yet, but hopefully this version will help.

When using checkboxes to hold multiple values you have to remember that you are working with a 2 dimesional array. 1 array = $_POST, 2 array = checkbox
Basic Example:

if (isset($_POST[‘box’])) // see if anything was checked
$num = count($_POST[‘box’]; // find the number of items checked
(for ($i = 0; $i<$num; $i++) // loop through the elements
echo $_POST[‘box’] [$i]; // do something with the values

Option 1 Option 2 Option 3 Option 4 </form. [/php]

Thanks for the codes that you have shared, it all help me to solve my problem

Sponsor our Newsletter | Privacy Policy | Terms of Service