Jump to content

[SOLVED] Select All Checkboxes


Grant Holmes

Recommended Posts

I've been doing some searching and found two posts that answer this question, but I don't believe they do for the type of page I have. At least I can't make them work. The related links:

Select All Checkbox topic from Dec 26

Select All Checkbox topic from Dec 27

 

I'm returning a page of results from my table. The end result is a multiple field table with record/rows. Each returned record is "Inactive" in that the "ACTIVE" field is set to 0 (zero) when I create the record. We want to be able to easily SELECT ALL to "Activate" or "DeActivate" by clicking a button, then Save Changes and have ALL those records updated. Someone may SELECT ALL, the uncheck some on their own too, or vice-versa; Do a DE-SELECT ALL.

 

So I need to add: SELECT ALL; DeSELECT ALL; Save changes. Only the "ACTIVE" checkbox is affected.

 

My current code:

<?php include_once("security/SECsecurity.php"); ?>
<?php $DOCROOT = $_SERVER['DOCUMENT_ROOT'] ; ?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>My web page</TITLE>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="GOOGLEBOT" CONTENT="NOARCHIVE">
    <META NAME="ROBOTS" CONTENT="NONE">
    <LINK REL="stylesheet" TYPE="text/css" HREF="text.css">
<script src="JS/sorttable.js"; ?></script>
</HEAD>
<BODY>
<a name="top"></a>
<?php
include("dbinfo.inc.php");
mysql_connect(mysql,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
          
$query="SELECT * FROM birthdays WHERE Event='request' AND Active='0'";
$result=mysql_query($query);

$num=mysql_numrows($result); 

mysql_close();

echo "<A name=\"top\"></A><b><center><H1>Title here \"<I>Inactive Requests</I>\":</H1>";
?>

<center><div style='width:100%; background-color:silver; text-align:right'>
  <?php SECShowAdminLink(); ?>
   
  <?php SECShowLogoutLink(); ?>  <A href="data.php">Main Data Page</A>  
</div>
<center><div style='width:100%; background-color:#ffe4c4; text-align:center'><BR>This page is showing <I>INACTIVE</I> Requests</B><BR><A href="requests.php">view <I>ALL</I> </A>   <A href="requests_active.php">view active only</A></div></center>
<table border="1" cellspacing="2" cellpadding="3" class="sortable">
<tr> 
<TH valign="top">From:</TH>
<TH valign="top">City</TH>	
<TH valign="top">State</TH>			
<TH valign="top">Zip</TH>			
<TH valign="top">Country</TH>				
        <TH valign="top">Artist Name</TH>
        <TH valign="top">Song Title</TH>
        <TH valign="top">Submitted</TH>			
        <TH valign="top">Status</TH>	
        <TH valign="top"> </TH>		
</tr>

<?php
$i=0;
while ($i < $num) {
$Active=mysql_result($result,$i,"Active");
$firstname=mysql_result($result,$i,"Contact_Info_FirstName");
$lastname=mysql_result($result,$i,"Contact_Info_LastName");
$city=mysql_result($result,$i,"Contact_Info_City");
$state=mysql_result($result,$i,"Contact_Info_State");
$zip=mysql_result($result,$i,"Contact_Info_ZipCode");
$country=mysql_result($result,$i,"Contact_Info_Country");
$email=mysql_result($result,$i,"Contact_Info_Email");
$Bfirstname=mysql_result($result,$i,"Birthday_Info_FirstName");
$Blastname=mysql_result($result,$i,"Birthday_Info_LastName");
$Bcity=mysql_result($result,$i,"Birthday_Info_City");
$Bstate=mysql_result($result,$i,"Birthday_Info_State");
$Bzip=mysql_result($result,$i,"Birthday_Info_ZipCode");
$Bcountry=mysql_result($result,$i,"Birthday_Info_Country");
$Date=mysql_result($result,$i,"DateEntered");
$id=mysql_result($result,$i,"id"); 

?>
</form>
<tr> 
            <td valign="top"><input type="hidden" name="ud_id" value="<? echo $id; ?>">
            <STRONG><?php echo "$firstname $lastname"; ?></STRONG><BR><a href="mailto:<?php echo "$email"; ?>"><?php echo "$email"; ?></a></TD>
            <TD valign="top"><?php echo "$city"; ?></TD>
            <TD valign="top"><?php echo "$state"; ?></TD>	
            <TD valign="top"><?php echo "$zip"; ?></TD>			   	   
            <TD valign="top"><?php echo "$country"; ?></TD>	   		   
            <TD valign="top"> <?php echo "$Bfirstname"; ?></TD>
            <TD valign="top"> <?php echo "$Blastname"; ?></TD>
            <TD valign="top"><?php echo "$Date"; ?></TD>				   
            <TD valign="top"> <B>Active?: 
<?php
// Set the variable $foo from the database or from form data.
$checked = $Active ? "checked" : "";
echo "<input type=\"checkbox\" name=\"foo\" $checked>";
?>
 </td>
            <TD valign="top"><CENTER> <a href='requests_edit.php?id=<? echo "$id"; ?>'>edit</a><BR><A href="#top"><IMG src="images/uparrow.gif" border="0" hspace="2" vspace="4" /></A><A href="#bottom"><IMG src="images/downarrow.gif" border="0" hspace="4" vspace="4" /></A></CENTER></TD>
</TD>
</tr>
<?php
++$i;
} 
echo "</table><P><A href=\"#top\">Back to top</A></P></B><BR>";
?>

<BR><BR><BR>
           <?php include "include/footer.php"; ?>	
	  <a name="bottom"></a>
</BODY>
</html>

 

I am weak in PHP and Javascript, so please be very specific where to put or use suggestions? Thanks!!

Link to comment
Share on other sites

You need something like this. (BTW, as to how to incorporate this, I think you can figure it out)

 

<script>
function checkAll(boo){
for (b=0;b<boo.length;b++) boo[b].checked=true;
}
function deSelAll(blah){
for (b=0;b<blah.length;b++) blah[b].checked=false;
}
</script>

<form name='eh'>
Checkbox 1: <input type='checkbox' name='check' />
Checkbox 2: <input type='checkbox' name='check' />
Checkbox 3: <input type='checkbox' name='check' />
Checkbox 4: <input type='checkbox' name='check' />
Checkbox 5: <input type='checkbox' name='check' />
<input type='button' value='Check All' onClick='checkAll(document.eh.check)' />
<input type='button' value='Deselect All' onClick='deSelAll(document.eh.check)' />
<input type='submit' name='submit' value='submit' />
</form>

Link to comment
Share on other sites

Ken,

 

thanks for the reply(s).

 

I found this basic answer in the other posts, but what I didn't understand was the Form and the:

Checkbox 1: <input type='checkbox' name='check' />

Checkbox 2: <input type='checkbox' name='check' />

Checkbox 3: <input type='checkbox' name='check' />

 

I will never know how many checkboxes I'll have, as the page is dynamic. There could be 4 or 27.

Link to comment
Share on other sites

You put that JavaScript anywhere as long as it's above the Check All and Deselect All buttons.

 

As for the PHP, you can try:

 

<?php
$number=10;
for ($b=0;$b<$number;$b++){
echo "Checkbox $b: <input type='checkbox' name='check' /><br />";
}
?>

Just edit $number for whatever you want.

Link to comment
Share on other sites

So I understand, what will "number" control? I just need this to happen once, say at the top of the page, where it would be most convenient.

 

Edit; I just put the PHP code in my page and it appears that I got a checkbox for every field on my page. A WHOLE bunch of them.

 

If my page returns 5 rows/records/results I'd need one "Select All" button, one "DeSelect All" Button, and one "Save changes button. The Select/deselect would effect the one checkbox at the end of each row.

 

I don't see anywhere on the page where the buttons above showed up.

Link to comment
Share on other sites

THIS oughta be fun. Neither of us understands the other.  :D

 

I'll try a restate: ON the page, I won't know how many records will be returned, depends on how many are submitted, and or were left unchecked/inactive from before. So on a dead day, I could get three records; a busy day 32, whatever.

 

I THINK I need three buttons;

one button to check all the displayed records

one button to UNcheck all the displayed records (someone changes their mind)

one button to save the checkbox status(es) to the table for any/all affected records.

 

I only need these buttons to show up once at the top of the display table, (or possibly the bottom to, but no mater) so could be in a TR above the results.

That help???

Link to comment
Share on other sites

Okay well the buttons are there on the second post, all except the submit button, which I edited in.

 

As for submitting the stuff, you'll need to put that PHP code yourself or ask for help in the PHP section.

 

Lastly, as for records, how are people submitting the records for you to return? In any event, this is also a PHP-related question.

Link to comment
Share on other sites

K,

 

I REALLY appreciate your help. The code you gave me, I got to put 10 boxes in the top row of my table, and the Buttons DO select THOSE check boxes, but they don't check the boxes in my table beside the records.

 

:(

 

Should I take this back to PHP yet, or is there still an issue we can deal with here?

Link to comment
Share on other sites

sorry. I put that button code in my first table row. It put your 10 boxes there with the buttons. (I've not tried my submit button yet!!!!)

 

The select/deselect buttons work on the 10 boxes from your code, but I need them to select/deselect the boxes in the table (or the records returned. AND I don't need those 10 boxes.

Link to comment
Share on other sites

I've populate a table with results submitted by visitors through a form. The record is populated with a "0" in the Active field when created.

 

Once made Active, the results will show on a public page on the site, showing others the results, in this case, requests. I want control over what is viewed publically for all the obvious reasons.

 

On this page called "Inactive Records" I show any/all those 'new' or inactive records. (Active records are shown else where). The code you see that I posted allows me to edit each record separately (using the edit link), but it takes too long for multiple entries. For other stuff like name/address/whatever, I don't care.

 

But for the simple act of making the records active or inactive, I want to just be able to hit the buttons already discussed. So, say I hit "Select all". Then the checkbox already in my code for all the records that are shown at that point in time (dynamic and varies) would become checked (in each row/record).

 

In practice, It might be easier to check 50 records with the button and uncheck 3 that I don't want to view. Then once I'm satisfied with my selections, hit one button that says "Submit" and all the record/rows that were checked would be updated in the table.

 

That help?  ( dropped in a somewhat modified screen shot that may help you see what I'm heading for)

 

[attachment deleted by admin]

Link to comment
Share on other sites

hmmm... I'm not sure. My screen shot was to explain what I needed, not the current state of the code. Right now the code I've modified per above instructions gives me this (see attached). I was trying to clarify what I needed.

 

I have the database already and the php code already. I just need the code modified to do this.

 

[attachment deleted by admin]

Link to comment
Share on other sites

I've gotten a bit of help off-forum and am VERY close.

 

I am now able to CHECK/UnCHECK all the checkboxes for whatever rows/records are returned. My last problem is saving the changes. I have a submit button, When I click it, it appears to be updating the record and the page refreshes, but the record(s) are not changed. Any help?

 

Here's my current code:

<?php include_once("security/SECsecurity.php"); ?>
<?php $DOCROOT = $_SERVER['DOCUMENT_ROOT'] ; ?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Martini In The Morning  Inactive Requests</TITLE>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="GOOGLEBOT" CONTENT="NOARCHIVE">
    <META NAME="ROBOTS" CONTENT="NONE">
    <LINK REL="stylesheet" TYPE="text/css" HREF="martini.css">
<script src="JS/sorttable.js"; ?></script>
<script type="text/javascript">
function checkall(class, uncheck){
inputs=document.getElementsByTagName("input") //Identify every DIV
for (input=0; input<inputs.length; input++){ //Go through the array
	if(inputs[input].className==class){ //Pick out the DIVs with the CLASS of "shadow"
		if(uncheck){inputs[input].checked=false}else{inputs[input].checked=true}
	}
}
}
</script>
</HEAD>
<BODY>
<a name="top"></a>
<?php
include("dbinfo.inc.php");
mysql_connect(mysql,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
          
$query="SELECT * FROM birthdays WHERE Event='test' AND Active='0'";
$result=mysql_query($query);

$num=mysql_numrows($result); 

mysql_close();

echo "<A name=\"top\"></A><b><center><H1>Martini In The Morning \"<I>Inactive Requests</I>\":</H1>";
?>

<center><div style='width:100%; background-color:silver; text-align:right'>
  <?php SECShowAdminLink(); ?>
   
  <?php SECShowLogoutLink(); ?>  <A href="data.php">Main Data Page</A>  
</div>
<center><div style='width:100%; background-color:#ffe4c4; text-align:center'><BR>This page is showing <I>INACTIVE</I> Requests</B><BR><A href="requests.php">view <I>ALL</I> </A>   <A href="requests_active.php">view active only</A></div></center>

<div style='width:100%; background-color:#deb887; text-align:right'><form method="post"><a href="#" onclick="checkall('group1'); return false;">Check All</a>  |  <a href="#" onclick="checkall('group1', 1); return false;">Uncheck All</a>  |  <input type="submit" name="submit" value="Update" onclick="return confirm('Are you sure?')"></div>
<table border="1" cellspacing="2" cellpadding="3" class="sortable"> 
<tr> 
	<TH valign="top">From:</TH>
	<TH valign="top">City</TH>	
	<TH valign="top">State</TH>			
	<TH valign="top">Zip</TH>			
	<TH valign="top">Country</TH>				
         <TH valign="top">Artist Name</TH>
         <TH valign="top">Song Title</TH>
         <TH valign="top">Submitted</TH>			
         <TH valign="top">Status</TH>	
         <TH valign="top"> </TH>		
</tr>

<?php
$i=0;
while ($i < $num) {
$Active=mysql_result($result,$i,"Active");
$firstname=mysql_result($result,$i,"Contact_Info_FirstName");
$lastname=mysql_result($result,$i,"Contact_Info_LastName");
$city=mysql_result($result,$i,"Contact_Info_City");
$state=mysql_result($result,$i,"Contact_Info_State");
$zip=mysql_result($result,$i,"Contact_Info_ZipCode");
$country=mysql_result($result,$i,"Contact_Info_Country");
$email=mysql_result($result,$i,"Contact_Info_Email");
$Bfirstname=mysql_result($result,$i,"Birthday_Info_FirstName");
$Blastname=mysql_result($result,$i,"Birthday_Info_LastName");
$Bcity=mysql_result($result,$i,"Birthday_Info_City");
$Bstate=mysql_result($result,$i,"Birthday_Info_State");
$Bzip=mysql_result($result,$i,"Birthday_Info_ZipCode");
$Bcountry=mysql_result($result,$i,"Birthday_Info_Country");
$Date=mysql_result($result,$i,"DateEntered");
$id=mysql_result($result,$i,"id"); 

?>
</form>
<tr> 
            <td valign="top"><input type="hidden" name="ud_id" value="<? echo $id; ?>">
            <STRONG><?php echo "$firstname $lastname"; ?></STRONG><BR><a href="mailto:<?php echo "$email"; ?>"><?php echo "$email"; ?></a></TD>
            <TD valign="top"><?php echo "$city"; ?></TD>
            <TD valign="top"><?php echo "$state"; ?></TD>	
            <TD valign="top"><?php echo "$zip"; ?></TD>			   	   
            <TD valign="top"><?php echo "$country"; ?></TD>	   		   
            <TD valign="top"> <?php echo "$Bfirstname"; ?></TD>
            <TD valign="top"> <?php echo "$Blastname"; ?></TD>
            <TD valign="top"><?php echo "$Date"; ?></TD>				   
            <TD valign="top"> <B>Active?: 
<?php
// Set the variable $foo from the database or from form data.
$checked = $Active ? "checked" : "";
echo "<input type=\"checkbox\" class=\"group1\" name=\"foo\" $checked>";
?>
 </td>
            <TD valign="top"><CENTER> <a href='requests_edit.php?id=<? echo "$id"; ?>'>edit</a><BR><A href="#top"><IMG src="images/uparrow.gif" border="0" hspace="2" vspace="4" /></A><A href="#bottom"><IMG src="images/downarrow.gif" border="0" hspace="4" vspace="4" /></A></CENTER></TD>
</TD>
</tr>
<?php
++$i;
} 
echo "</table>";
?>
<div style='width:100%; background-color:#deb887; text-align:right'><form method="post"><a href="#" onclick="checkall('group1'); return false;">Check All</a>  |  <a href="#" onclick="checkall('group1', 1); return false;">Uncheck All</a>  |  <input type="submit" name="submit" value="Update" onclick="return confirm('Are you sure?')"></div>
<P><A href=\"#top\">Back to top</A></P></B><BR>
<BR><BR><BR>
           <?php include "include/footer.php"; ?>	
	  <a name="bottom"></a>
</BODY>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.