Tick all checkboxes

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I have a set of 4 checkboxes and I want to have a button that will check all  
the boxes. I have looked on Google but can only find examples of it being in  
Javascript. Is there any way of doing it in a php file. If not, how do I go  
about using a Javascript file? Do I have to make a reference to it in the  



Re: Tick all checkboxes

Paul Morrison wrote:
Quoted text here. Click to load it


PHP is server side, not client side.  You can do it with PHP - but that  
would mean submitting the file back to PHP and regenerating the entire  
page with the checkboxes checked.

Much easier to do it in Javascript, which is client-side.  And no, you  
don't have to reference the javascript in the PHP file.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: Tick all checkboxes

Quoted text here. Click to load it

PHP runs on the server.  It doesn't run until after you submit the
form or click on a link.

Quoted text here. Click to load it

You'd need to find a client-side way of doing it.  I don't think it's
possible in just HTML.  Javascript could do it.

                        Gordon L. Burditt

Re: Tick all checkboxes

Quoted text here. Click to load it

Javascript is the way for that.  Using PHP is very possible, but javascript  
is quicker to implement and for the browser.


Re: Tick all checkboxes

here is a simple example,wish it can be helpful! :)
function CheckAll(form){
    for(var i=0;i<form.elements.length;i++){
        var e = form.elements[i];
        if (e.name == 'id[]'){
            e.checked = form.chkall.checked;

<form name="test" method="post" >
<input name="chkall" type="checkbox"
onclick="CheckAll(this.form);">Select All
<input type="checkbox" name="id[]" value="1000">Joe Schmoe<br />
<input type="checkbox" name="id[]" value="1001" checked="checked"><br
<input type="checkbox" name="id[]" value="1002">Jane Johnson<br />
<input type="checkbox" name="id[]" value="1003">Charlie Brown<br />

Re: Tick all checkboxes

Thanks guys, got that sorted now, I didnt realise that you could put  
Javascript into a php file.

I now have another problem with the checkboxes. I set up my form as follows:

<form method="get" action="sub.php">
    <div align="left" style="font-size:12px ;clear : both">
        <input type=button name="CheckAll" value="Check All"  
onClick="modify_boxes(true,4)" style="font-size:10px"/>
        <input type=button name="UncheckAll" value="Uncheck All"  
onClick="modify_boxes(false,4)" style="font-size:10px"/><p/>
        <div style="width:10px"></div><input type="checkbox"  
name="subscription" value="economic" id="economic"/><label  
        <div style="width:10px"></div><input type="checkbox"  
name="subscription" value="environmental" id="environmental"/><label  
        <div style="width:10px"></div><input type="checkbox"  
name="subscription" value="political" id="political"/><label  
        <div style="width:10px"></div><input type="checkbox"  
name="subscription" value="social" id="social"/><label  
        <br><input type="submit" value="Submit" style="font-size:10px"/>
    <input type="hidden" name="add_subscription" value="1"/>

Once the user ticks the boxes that apply to them, they click the submit  
button and I want to go through the boxes. I am using the following type of  

    $query = "INSERT INTO inj_subscription VALUES ('$user' , 'economic' ,  
    $result = mysql_query($query) or die('Error during subscription process,  
please refresh the page and try again');
        echo '<p style="color:red">Subscription Successful</p>';

The clause that I use to get into the loop is:


The problem is that the code I have written checks whether the boxes are  
ticked when the page loads up, rather than after the user clicks the submit.  
I know that loop worked before as I was using radio buttons rsther than  
checkboxes, is this the wrong assumption to make?

Any help would be much appreciated.


Site Timeline