...

View Full Version : Multiple Checkboxes



JBLC
08-18-2007, 10:46 PM
Hello

I hope someone can give me some advice on the following code.

The script is called register_ops.php. The script is nothing more than a register to assign the date that soemone is in work within a database table. The script display's a list of names with a checkbox next to each. The user can then check the checkbox(s) (to confirm that someone is in work that day) and then submit the entire form which then updates each record that has been checked with the current date. I have stripped out all of the mysql connect "type stuff" to keep the code shorter and everything else works fine except that I cannot get the table column to update those records that have been checked.

Currently the table from which the list of names is generated and the column updated (to record the date) is the same. Called opsv10.

If anyone can give me some help, I would really appreciate it as I have been trying to work this one out for the last 5 days!


<?php

if (isset($_POST['submitted'])) {

foreach ($_POST as $key) {
$id=$key[ops_id];

$query = "UPDATE opsv10 SET on_site= NOW() where ops_id = $id";

}

} else {

// Number of records to show per page:
$display = 10;

// Determine how many pages there are.
if (isset($_GET['np'])) { // Already been determined.

$num_pages = $_GET['np'];

} else { // Need to determine.

$c = addslashes($_SESSION['company']);

// Count the number of records
$query = "SELECT COUNT(*) AS Num FROM opsv10 WHERE company = '$c'";
$result = mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_NUM);
$num_records = $row[0];

// Calculate the number of pages.
if ($num_records > $display) { // More than 1 page.
$num_pages = ceil ($num_records/$display);
} else {
$num_pages = 1;
}

} // End of np IF.

// Determine where in the database to start returning results.
if (isset($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}

// Make the query.
$query = "SELECT ops_id, first_name, last_name, position FROM opsv10 WHERE company = '$c' ORDER BY last_name ASC LIMIT $start, $display";
$result = mysql_query ($query); // Run the query.
?>



<div id="contractor_table_holder">

<form action="register_ops.php" method="post">
<?php
// Table header.
echo '
<table align="center" cellspacing="0" cellpadding="8" bgcolor="">
<tr>
<td align="center"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><b></b></font></td>
<td align="center"><img src="program_images/spacer_gif.gif" border="0" /></td>
<td align="center"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><b>Name</b></font></td>
<td align="center"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><b>Position</b></font></td>
</tr>
';
// Fetch and print all the records.
$bg = ''; // Set the background color.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id = $row['ops_id'];
$bg = ($bg=='' ? '' : ''); // Switch the background color.
echo '<tr bgcolor="' . $bg . '">
<td style="border-top: 1px solid #000000;" align="center"><input name="checkbox[]" type="checkbox" value="$id" /></td>
<td style="border-top: 1px solid #000000;" align="center"><img src="program_images/spacer_gif.gif" border="0" /></td>
<td style="border-top: 1px solid #000000;" align="left"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2" color="#FFFFFF"><strong>' . $row['first_name'] . ' ' . $row['last_name'] . '</strong></font></td>
<td style="border-top: 1px solid #000000;" align="left"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2" color="#FFFFFF"><strong>' . $row['position'] . '</strong></font></td>
<td><td>

</tr>
';
}

echo '</table>';
?>
<input name="submit" type="button" value="send"/>
<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name =".$id.[ops_id]" value =".$id.">
</form>



</div>
<?php

mysql_free_result ($result); // Free up the resources.
mysql_close(); // Close the database connection.
?>


<div id="cont_list_pages">
<?php
// Make the links to other pages, if necessary.
if ($num_pages > 1) {

echo '<p>';
// Determine what page the script is on.
$current_page = ($start/$display) + 1;

// If it's not the first page, make a Previous button.
if ($current_page != 1) {
echo '<a href="register_ops.php?s=' . ($start - $display) . '&np=' . $num_pages . '"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><<<&nbsp;&nbsp;</font></a> ';
}

// Make all the numbered pages.
for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $current_page) {
echo '<font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><a href="register_ops.php?s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2">' . $i . '</font></a> ';
} else {
echo $i . ' ';
}
}

// If it's not the last page, make a Next button.
if ($current_page != $num_pages) {
echo '<a href="register_ops.php?s=' . ($start + $display) . '&np=' . $num_pages . '"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2">&nbsp;&nbsp;>>></a></font>';
}

echo '</p>';
}
} // End of links section.
?>
</div>
</div>
</div>
</div>
</div>
<?php // Include the HTML footer.
include ('./includes/footer.html');
?>

Inigoesdr
08-19-2007, 12:05 AM
Change value="$id" to value="' . $id . '", then change the code to update it to this:

foreach ($_POST['checkbox'] as $key => $value) {
$query = "UPDATE opsv10 SET on_site= NOW() where ops_id = $value";
}
You could make this all one query if you want to by imploding the array with ' OR `ops_id` = ', or something similar.

JBLC
08-19-2007, 12:56 AM
Hello Inigoesdr

Tried your changes but still no luck. It appears that the submit button is not "working". Could there be another error in the code?

Thanks
JC

Inigoesdr
08-19-2007, 12:59 AM
It's possible. I really didn't look that much. Edit your post and put the code inside of
tags so we can see it formatted.


Edit: Your submit button is called "submit" and the $_POST variable you're using is called "submitted", change the $_POST variable.

JBLC
08-19-2007, 01:04 AM
OK code tags added.


<?php

if (isset($_POST['submitted'])) {

foreach ($_POST as $key) {
$id=$key[ops_id];

$query = "UPDATE opsv10 SET on_site= NOW() where ops_id = $id";

}

} else {

// Number of records to show per page:
$display = 10;

// Determine how many pages there are.
if (isset($_GET['np'])) { // Already been determined.

$num_pages = $_GET['np'];

} else { // Need to determine.

$c = addslashes($_SESSION['company']);

// Count the number of records
$query = "SELECT COUNT(*) AS Num FROM opsv10 WHERE company = '$c'";
$result = mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_NUM);
$num_records = $row[0];

// Calculate the number of pages.
if ($num_records > $display) { // More than 1 page.
$num_pages = ceil ($num_records/$display);
} else {
$num_pages = 1;
}

} // End of np IF.

// Determine where in the database to start returning results.
if (isset($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}

// Make the query.
$query = "SELECT ops_id, first_name, last_name, position FROM opsv10 WHERE company = '$c' ORDER BY last_name ASC LIMIT $start, $display";
$result = mysql_query ($query); // Run the query.
?>


<div id="contractor_table_holder">

<form action="register_ops.php" method="post">

<?php
// Table header.
echo '
<table align="center" cellspacing="0" cellpadding="8" bgcolor="">
<tr>
<td align="center"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><b></b></font></td>
<td align="center"><img src="program_images/spacer_gif.gif" border="0" /></td>
<td align="center"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><b>Name</b></font></td>
<td align="center"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><b>Position</b></font></td>
</tr>
';
// Fetch and print all the records.
$bg = ''; // Set the background color.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id = $row['ops_id'];
$bg = ($bg=='' ? '' : ''); // Switch the background color.
echo '<tr bgcolor="' . $bg . '">
<td style="border-top: 1px solid #000000;" align="center"><input name="checkbox[]" type="checkbox" value="$id" /></td>
<td style="border-top: 1px solid #000000;" align="center"><img src="program_images/spacer_gif.gif" border="0" /></td>
<td style="border-top: 1px solid #000000;" align="left"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2" color="#FFFFFF"><strong>' . $row['first_name'] . ' ' . $row['last_name'] . '</strong></font></td>
<td style="border-top: 1px solid #000000;" align="left"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2" color="#FFFFFF"><strong>' . $row['position'] . '</strong></font></td>
<td><td>

</tr>
';
}

echo '</table>';
?>
<input name="submit" type="button" value="send"/>
<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name =".$id.[ops_id]" value =".$id.">
</form>



</div>

<?php

mysql_free_result ($result); // Free up the resources.
mysql_close(); // Close the database connection.
?> [/CODE]

<div id="cont_list_pages">
[CODE]<?php
// Make the links to other pages, if necessary.
if ($num_pages > 1) {

echo '<p>';
// Determine what page the script is on.
$current_page = ($start/$display) + 1;

// If it's not the first page, make a Previous button.
if ($current_page != 1) {
echo '<a href="register_ops.php?s=' . ($start - $display) . '&np=' . $num_pages . '"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><<<&nbsp;&nbsp;</font></a> ';
}

// Make all the numbered pages.
for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $current_page) {
echo '<font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><a href="register_ops.php?s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2">' . $i . '</font></a> ';
} else {
echo $i . ' ';
}
}

// If it's not the last page, make a Next button.
if ($current_page != $num_pages) {
echo '<a href="register_ops.php?s=' . ($start + $display) . '&np=' . $num_pages . '"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2">&nbsp;&nbsp;>>></a></font>';
}

echo '</p>';
}
} // End of links section.
?>
</div>
</div>
</div>
</div>
</div>

<?php // Include the HTML footer.
include ('./includes/footer.html');
?>

Inigoesdr
08-19-2007, 01:09 AM
Please re-read my previous post, and the sticky threads for this forum.

JBLC
08-19-2007, 01:13 AM
I have corrected the post - sorry but I doubled up on the [code] and [php]. It makes some sense now.

JBLC
08-19-2007, 01:15 AM
The $_POST submitted refers to the hidden input.

Inigoesdr
08-19-2007, 01:20 AM
You can put the
tags around everything, including the HTML.

You didn't make those changes I posted in my first post either

JBLC
08-19-2007, 11:25 AM
OK, the original script with the changes you recommended.


<?php

if (isset($_POST['submitted'])) {

foreach ($_POST['checkbox'] as $key => $value) {
$query = "UPDATE opsv10 SET on_site= NOW() where ops_id = $value";
}
} else {

// Number of records to show per page:
$display = 10;

// Determine how many pages there are.
if (isset($_GET['np'])) { // Already been determined.

$num_pages = $_GET['np'];

} else { // Need to determine.

$c = addslashes($_SESSION['company']);

// Count the number of records
$query = "SELECT COUNT(*) AS Num FROM opsv10 WHERE company = '$c'";
$result = mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_NUM);
$num_records = $row[0];

// Calculate the number of pages.
if ($num_records > $display) { // More than 1 page.
$num_pages = ceil ($num_records/$display);
} else {
$num_pages = 1;
}

} // End of np IF.

// Determine where in the database to start returning results.
if (isset($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}

// Make the query.
$query = "SELECT ops_id, first_name, last_name, position FROM opsv10 WHERE company = '$c' ORDER BY last_name ASC LIMIT $start, $display";
$result = mysql_query ($query); // Run the query.
?>



<div id="contractor_table_holder">

<form action="register_ops.php" method="post">
<?php
// Table header.
echo '

<table align="center" cellspacing="0" cellpadding="8" bgcolor="">
<tr>
<td align="center"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><b></b></font></td>
<td align="center"><img src="program_images/spacer_gif.gif" border="0" /></td>
<td align="center"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><b>Name</b></font></td>
<td align="center"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><b>Position</b></font></td>
</tr>
';
// Fetch and print all the records.
$bg = ''; // Set the background color.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id = $row['ops_id'];
$bg = ($bg=='' ? '' : ''); // Switch the background color.
echo '<tr bgcolor="' . $bg . '">
<td style="border-top: 1px solid #000000;" align="center"><input name="checkbox[]" type="checkbox" value="' . $id . '" /></td>
<td style="border-top: 1px solid #000000;" align="center"><img src="program_images/spacer_gif.gif" border="0" /></td>
<td style="border-top: 1px solid #000000;" align="left"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2" color="#FFFFFF"><strong>' . $row['first_name'] . ' ' . $row['last_name'] . '</strong></font></td>
<td style="border-top: 1px solid #000000;" align="left"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2" color="#FFFFFF"><strong>' . $row['position'] . '</strong></font></td>
<td><td>

</tr>
';
}

echo '</table>';
?>
<input name="submit" type="button" value="send" />
//<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name ="<?php $id ?>" value ="<?php $id ?>" />
</form>



</div>
<?php

mysql_free_result ($result); // Free up the resources.
mysql_close(); // Close the database connection.
?>


<div id="cont_list_pages">
<?php
// Make the links to other pages, if necessary.
if ($num_pages > 1) {

echo '<p>';
// Determine what page the script is on.
$current_page = ($start/$display) + 1;

// If it's not the first page, make a Previous button.
if ($current_page != 1) {
echo '<a href="register_ops.php?s=' . ($start - $display) . '&np=' . $num_pages . '"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><<<&nbsp;&nbsp;</font></a> ';
}

// Make all the numbered pages.
for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $current_page) {
echo '<font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><a href="register_ops.php?s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2">' . $i . '</font></a> ';
} else {
echo $i . ' ';
}
}

// If it's not the last page, make a Next button.
if ($current_page != $num_pages) {
echo '<a href="register_ops.php?s=' . ($start + $display) . '&np=' . $num_pages . '"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2">&nbsp;&nbsp;>>></a></font>';
}

echo '</p>';
}
} // End of links section.
?>
</div>
</div>
</div>
</div>
</div>
<?php // Include the HTML footer.
include ('./includes/footer.html');
?>

Inigoesdr
08-20-2007, 03:22 AM
Add
mysql_query($query) or die('Query: ' . $query . '<br />Error: ' . mysql_error()); after the $query at the top and post any errors you get.

JBLC
08-20-2007, 08:29 PM
Inigoesdr

Used the above code but no errors are reported. I think the problem is that the submit button is not posting anything. I tried changing submitted to submit (as you suggested earlier) but still nothing!

There must be a more general error in the actual form as the button isn't sending - what I can't get my head around is the format of the script is no different to countless other forms I have done with no problems at all????

JBLC

Len Whistler
08-20-2007, 09:27 PM
<?php

if (isset($_POST['submitted'])) {

-------------------------------

<form action="register_ops.php" method="post">

------------------------------

<input name="submit" type="button" value="send" />
//<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name ="<?php $id ?>" value ="<?php $id ?>" />
</form>

This is the code you have so far, one of the lines is commented out and it matches the syntax of if (isset($_POST['submitted'])). Use the common word of submit instead of submitted to keep it simple, make these changes in the register_ops.php file also.

Also consider using <form method="post" action="<?php echo $PHP_SELF;?>"> instead of <form action="register_ops.php" method="post"> to do the processing. I find one page is better than using two pages to do the POST processing in most cases.

JBLC
08-20-2007, 09:54 PM
Inigoesdr

Just to let you know, I have found the solution to the problem. The original query was changed to:


$query = 'UPDATE opsv10 SET on_site= NOW() where ops_id IN (' . implode(',', $_POST['checkbox']) . ')';

and all works fine. Thank you very much for your help and please refer to the private message that I sent you.

Regards
JBLC

JBLC
08-20-2007, 09:56 PM
Lenoard

Thanks for the input. The last post from Larry Ullman solved the problem.

Appreciate your input though.
JBLC (JC)

Inigoesdr
08-20-2007, 10:48 PM
<?php

if (isset($_POST['submitted'])) {

-------------------------------

<form action="register_ops.php" method="post">

------------------------------

<input name="submit" type="button" value="send" />
//<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name ="<?php $id ?>" value ="<?php $id ?>" />
</form>

This is the code you have so far, one of the lines is commented out and it matches the syntax of if (isset($_POST['submitted'])). Use the common word of submit instead of submitted to keep it simple, make these changes in the register_ops.php file also.

Also consider using <form method="post" action="<?php echo $PHP_SELF;?>"> instead of <form action="register_ops.php" method="post"> to do the processing. I find one page is better than using two pages to do the POST processing in most cases.

That // comment isn't recognized in HTML, so it should still have been submitted.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum