Is it possible to create a "Select All" check-box in a PHP program without using Javascript?![]()
Is it possible to create a "Select All" check-box in a PHP program without using Javascript?![]()
PHP operates on the server. I have no idea what you mean by a "Select All" check-box, but I'm guessing no. Only Javascript can interact with the user when the webpage is active.
You could use PHP to generate a checkbox (the HTML element) and even add some Javascript commands to it, but the PHP itself won't create any sort of "selecting" action.
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
What about adding an additional checkbox labeled "all"?
If "all" is selected along with any other checkboxes the "all" checkbox will override the other checked off boxes.
I am not entirely sure this is what you are looking for though.
To choose the lesser of two evils is still to choose evil. My personal site
Hi james
I didn't get what you said.Can you please write the code?
p.s.: What I mean is that I want to create 5 checkboxes beside 5 files . Then there show be a a checkbox below the 5 files called "Select All" ,which one being selected ,selects /checks the 5 checkboxes. Hope you all get me now
Unfortunately you would need javascript.
To choose the lesser of two evils is still to choose evil. My personal site
It's a very simple script to make. Here's a working example:
HTML Code:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <!-- Page Title --> <title>Select All Checkboxes Example</title> <!-- Meta Block --> <meta content="text/html; charset=iso-8859-1" http-equiv="content-type" /> <meta content="Select All Checkboxes Example" name="description" /> <meta content="Select All, Checkboxes, Example" name="keywords" /> <meta content="all,index,follow" name="robots" /> <meta content="noodp" name="msnbot" /> <meta content="global" name="distribution" /> <!-- Javascript Scripts --> <script type="text/javascript"> //<![CDATA[ function selectAllFiles(c) { for (i = 1; i <= 5; i++) { document.getElementById('chkFile' + i).checked = c; } } //]]> </script> </head> <body> <div id="page"> <p> <input id="chkFile1" type="checkbox" title="File 1" />File 1</p> <p> <input id="chkFile2" type="checkbox" title="File 2" />File 2</p> <p> <input id="chkFile3" type="checkbox" title="File 3" />File 3</p> <p> <input id="chkFile4" type="checkbox" title="File 4" />File 4</p> <p> <input id="chkFile5" type="checkbox" title="File 5" />File 5</p> <p> <input id="chkAllFiles" type="checkbox" title="All Files" onchange="selectAllFiles(this.checked);" />All Files</p> </div> </body> </html>
If you don't mind reloading the page, you could have a button on it that would check all. It would be the submit button of a separate form that would get or post to the page itself data telling it to render all the boxes as checked. You could also have a check none button that would also be a separate form and work in a similar manner.
But any other data would have to be preserved somehow. If there were no other data or if all other data was already in session and not subject to change on that page, that would be fine. Otherwise, it wouldn't work well without javascript anyway.
What's the big problem with javascript? You can use it, and if there's none available on the user's end, you can make the check all checkbox unavailable.
Last edited by jscheuer1; 06-16-2012 at 02:58 PM. Reason: add proviso
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
Here's a working version in PHP.
default.php:
PHP Code:<?php
function checked() {
if(isset($_POST['chkAllFiles'])) {
return 'checked="checked" ';
}
else
{
return '';
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- Page Title -->
<title>Select All Checkboxes Example</title>
<!-- Meta Block -->
<meta content="text/html; charset=iso-8859-1" http-equiv="content-type" />
<meta content="Select All Checkboxes Example" name="description" />
<meta content="Select All, Checkboxes, Example" name="keywords" />
<meta content="all,index,follow" name="robots" />
<meta content="noodp" name="msnbot" />
<meta content="global" name="distribution" />
</head>
<body>
<div id="page">
<form action="default.php" method="post">
<p>
<input name="chkFile[]" type="checkbox" title="File 1" <?php echo checked(); ?>/>File 1</p>
<p>
<input name="chkFile[]" type="checkbox" title="File 2" <?php echo checked(); ?>/>File 2</p>
<p>
<input name="chkFile[]" type="checkbox" title="File 3" <?php echo checked(); ?>/>File 3</p>
<p>
<input name="chkFile[]" type="checkbox" title="File 4" <?php echo checked(); ?>/>File 4</p>
<p>
<input name="chkFile[]" type="checkbox" title="File 5" <?php echo checked(); ?>/>File 5</p>
<p>
<input name="chkAllFiles" type="checkbox" title="All Files" <?php echo checked(); ?> />
<input name="btnGo" type="submit" value="All Files" /></p>
</form>
</div>
</body>
</html>
megha (06-17-2012)
Yes, but now the form is virtually useless for any other purpose.
This will do sort of the same thing, and when it's done, including optionally changing one or more checkboxes after checking them all or clearing them all, the actual form may be submitted to another page (processing.php in this example):
But there's still the problem of, what about other data if any in the main form? If there is any, like -say, username - that won't survive the reloads that check and uncheck the boxes unless it's in and drawn from the session and cannot be changed on this form.PHP Code:<?php
$checked = isset($_POST['chkAllFiles'])? 'checked' : '';
?>
<!DOCTYPE html>
<html>
<head>
<!-- Page Title -->
<title>Select All Checkboxes Example</title>
<!-- Meta Block -->
<meta content="text/html; charset=utf-8" http-equiv="content-type" />
<meta content="Select All Checkboxes Example" name="description" />
<meta content="Select All, Checkboxes, Example" name="keywords" />
</head>
<body>
<div id="page">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input name="chkAllFiles" type="submit" value="All Files"/>
</form>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input name="unchkAllFiles" type="submit" value="No Files"/>
</form>
<form action="processing.php" method="post">
<p>
<input name="chkFile[]" type="checkbox" title="File 1" <?php echo $checked; ?>/>File 1</p>
<p>
<input name="chkFile[]" type="checkbox" title="File 2" <?php echo $checked; ?>/>File 2</p>
<p>
<input name="chkFile[]" type="checkbox" title="File 3" <?php echo $checked; ?>/>File 3</p>
<p>
<input name="chkFile[]" type="checkbox" title="File 4" <?php echo $checked; ?>/>File 4</p>
<p>
<input name="chkFile[]" type="checkbox" title="File 5" <?php echo $checked; ?>/>File 5</p>
<p>
<input type="submit" value="Submit" /></p>
</form>
</div>
</body>
</html>
Last edited by jscheuer1; 06-16-2012 at 05:46 PM. Reason: add info on processing.php
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
So there's no options in PHP for AutoPostBack, like in ASP.NET? Or anyrunat:"server"equivalent?
This is possible in ASP.NET without using Javascript, but the OP was asking specifically for PHP.
If you can use ASP.NET, you can do this without using JavaScript, as it has AutoPostBack features added in.
default.aspx:
default.aspx.vb:HTML Code:<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <!-- Page Title --> <title>Select All Checkboxes Example</title> <!-- Meta Block --> <meta content="text/html; charset=iso-8859-1" http-equiv="content-type" /> <meta content="Select All Checkboxes Example" name="description" /> <meta content="Select All, Checkboxes, Example" name="keywords" /> <meta content="all,index,follow" name="robots" /> <meta content="noodp" name="msnbot" /> <meta content="global" name="distribution" /> </head> <body> <form id="Form1" runat="server"> <asp:ScriptManager ID="AJAXHandler" runat="server" EnablePartialRendering="true"> </asp:ScriptManager> <div id="page"> <asp:UpdatePanel ID="AjaxForm" runat="server"> <ContentTemplate> <asp:CheckBoxList ID="chkFiles" runat="server"> <asp:ListItem Text="File 1" /> <asp:ListItem Text="File 2" /> <asp:ListItem Text="File 3" /> <asp:ListItem Text="File 4" /> <asp:ListItem Text="File 5" /> </asp:CheckBoxList> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="chkAllFiles" EventName="CheckedChanged" /> </Triggers> </asp:UpdatePanel> <asp:CheckBox ID="chkAllFiles" Text="All Files" runat="server" AutoPostBack="true" /> </div> </form> </body> </html>
Code:Option Explicit On Option Strict On Partial Class _Default Inherits System.Web.UI.Page Protected Sub chkAllFiles_CheckedChanged(sender As Object, e As System.EventArgs) Handles chkAllFiles.CheckedChanged For Each chk As ListItem In chkFiles.Items chk.Selected = chkAllFiles.Checked Next End Sub End Class
Last edited by jscheuer1; 06-16-2012 at 07:38 PM. Reason: post merge
Bookmarks