starrylitgal Posted January 20, 2009 Share Posted January 20, 2009 hi! i hav a form named "modify" which shows a list of usernames and along with each username, there is a textbox for entering passwords. Basically this form is for modifying the passwords of the existing users. Also, there is a checkbox along with each username and password row and 1 common checkbox called "master" in the 1st row of the table. I want this "master" checkbox to work as a select/deselect all option checkbox. Onclicking the "master" checkbox , all the checkboxes listed in the table of usernames and passwords should get checked/unchecked. How do i do that?? my code (relevant to the problem) looks like this:: <form name="modify"> <table> <tr> <th><input type="checkbox" name='master'/></th> <th >Name</th> <th>Username</th> <th>Enter New Password</th> </tr> <?php while($row=mysql_fetch_array($res)) {?> <tr align='center'> <td><input type='checkbox' /></td> <th>$row[name]</th> <th>$row[username]</th>"; <td><input type='password' name=pwd /></td></tr> <?php } ?> </table> </form> im doing dynamically retireving all the usernames , and the number of rows is therefore dynamic. functionality, the rows with the checkboxes selected will be updated with the new passwords. if the user wants to update all , then all the checkboxes shd be selected. For that, i need help! Help required is very urgent! Quote Link to comment Share on other sites More sharing options...
ratcateme Posted January 20, 2009 Share Posted January 20, 2009 you need to look at javascript and a onlclick action for you master checkbox that goes down and checks all the checkboxes or unchecks as required. i had a similar script that used checkbox arrays google it for more info Scott. Quote Link to comment Share on other sites More sharing options...
starrylitgal Posted January 20, 2009 Author Share Posted January 20, 2009 you need to look at javascript and a onlclick action for you master checkbox that goes down and checks all the checkboxes or unchecks as required. i had a similar script that used checkbox arrays google it for more info Scott. i did google it...i found few codes too and tried them ...but it wasnt working..wht do i do? i dont know javascript but since i hav programming knowledge i try to understand it....but i m not able to understand where hav i gone wrong. Wht shd i do? Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted January 20, 2009 Share Posted January 20, 2009 You can do it using a onclick and a function <script type="text/javascript"> function checkCheckboxes(){ //code that checks checkboxes } </script> <input onclick="checkCheckboxes()" type="checkbox" /> Quote Link to comment Share on other sites More sharing options...
starrylitgal Posted January 20, 2009 Author Share Posted January 20, 2009 You can do it using a onclick and a function <script type="text/javascript"> function checkCheckboxes(){ //code that checks checkboxes } </script> <input onclick="checkCheckboxes()" type="checkbox" /> i dont know javascript...so please if ne1 can give me the code...tht wud be gr8...coz currently i dont hav such time to actually sit n learn n then try to solve the prob...but wid whtevr programming knowledge i hav, i can understand the code.. plzz help! i have tried this code...but it is not working i dont know wht is the prob...if possible some1 plzz guide me thru this.. <script language="javascript"> function selectall() { var masterChecked = document.modifyform.master.checked; for(var i=1; i<=document.modifyform.elements.length; i++) { if(document.modifyform.elements[i].type=="checkbox") document.modifyform.elements[i].checked = masterChecked; } } </script> HTML code: <tr> <th><input type="checkbox" name='master' onclick="selectall()"/></th><tr> <?php for($i=0;$i=10;$i++) {?> <tr><td><input type='checkbox' name="u" value=\"$i\"/></td><tr> <?php } ?> Quote Link to comment Share on other sites More sharing options...
starrylitgal Posted January 20, 2009 Author Share Posted January 20, 2009 <table> <th><input type="checkbox" name='master' onclick="selectall()"/></th><tr> <?php for($i=0;$i=10;$i++) {?> <tr><td><input type='checkbox' name="u" value=\"$i\"/></td><tr> <?php } ?> </table> i made a few typos...the code is: <form name="modifyform"> <table> <tr><th><input type="checkbox" name='master' onclick="selectall()"/></th></tr> <?php for($i=1;$i<=10;$i++) {?> <tr><td><input type='checkbox' name="u" value=\"$i\"/></td></tr> <?php } ?> </table> </form> can ne1 help me find the prob wid d code i posted earlier with this correction? Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted January 20, 2009 Share Posted January 20, 2009 so please if ne1 can give me the code...tht wud be gr8...coz currently i dont hav such time I'm not going to write the script for you but i can point you more towards to solution <script type="text/javascript"> function checkCheckboxes(){ //the following line sets the checkbox with the id value "checkbox1" to checked document.getElementById("checkbox1").checked=true; } </script> <label>checkbox1</label> <input id="checkbox1" name="checkbox1" type="checkbox" /> <label>your master checkbox</label> <input onclick="checkCheckboxes()" type="checkbox" /> Quote Link to comment Share on other sites More sharing options...
starrylitgal Posted January 20, 2009 Author Share Posted January 20, 2009 Hi guys! thnx dj kat for the reply. i used the following code: <SCRIPT LANGUAGE="JavaScript"> function Check(chk) { if(document.modifyform.master.checked==true){ for (i = 0; i < chk.length; i++) chk[i].checked = true ; }else{ for (i = 0; i < chk.length; i++) chk[i].checked = false ; } } </script> Here is the code for body part . <form name="modifyform" action="1.php" method="post"> <b>Sample Program</b><br> <input type="checkbox" name="master" value="yes" onClick="Check(document.modifyform.u)"><b>Check Control</b> <br/> <?php for($i=0;$i<5;$i++) {?> <input type="checkbox" name='u' value='<?php echo $i ?>'>ASP <?php echo $i ?><br> <?php } ?> <input type="submit" value="submit" name="gen"/> </form> I hav used the above code and it worked.... but i hav one problem... i want to store all the values of the selected checkboxes in an array form so that i can then determine wich checkboxes are checked(may be like a checkbox array)....n do some appropriate task.. i used the following code to check wht values were passed whn d form was submitted: <?php if(isset($_POST['submit'])) { for($i=0;$i<5;$i++) { echo $_POST['u']."<br/>"; } } ?> Output: 4 4 4 4 4 The system is taking the value of the last checkbox selected. I want the corresponding values of each checkbox selected. how do i do this? can some modifications in the above code help me to do this?? if yes, can ne1 suggest wht modifications hav to be made?? if the javascript has to be changed then plzz tell me wht r d changes coz i cant figure it out by myself. Thanx! Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 20, 2009 Share Posted January 20, 2009 <SCRIPT LANGUAGE="JavaScript"> function Check(chkOptions, chkState) { for (i=0; i<chkOptions.length; i++) { chkOptions[i].checked = chkState; } return; } </script> Here is the code for body part . <form name="modifyform" action="1.php" method="post"> <b>Sample Program</b><br> <input type="checkbox" name="master" value="yes" onClick="Check(document.modifyform['u[]'], document.modifyform.master.checked)"><b>Check Control</b> <br/> <?php for($i=0;$i<5;$i++) {?> <input type="checkbox" name='u[]' value='<?php echo $i ?>'>ASP <?php echo $i ?><br> <?php } ?> <input type="submit" value="submit" name="gen"/> </form> <?php foreach($_POST['u'] as $checkValue) { echo "$checkValue<br />"; } ?> Quote Link to comment Share on other sites More sharing options...
starrylitgal Posted January 20, 2009 Author Share Posted January 20, 2009 hey mjdamato!! thanx! it worked!!! thnx a lot! Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 20, 2009 Share Posted January 20, 2009 You're welcome. Please mark the topic as solved. Quote Link to comment Share on other sites More sharing options...
starrylitgal Posted January 20, 2009 Author Share Posted January 20, 2009 hi again! though my earlier prob was solved...there is one more problem... since the checkbox array is a dynamic one..so if the 1st and the third checkboxes are selected then the array stores the 1st checkbox value to an index 0 and the third checkbox value to an index 2... i want that whn the 1 & 3rd checkboxes are selected then corresponding values should have the respective indexes...i.e. 1st selected checkbox--index 0 3rd selected checkbox--index 2 how do i do that? i know how to do this in simple php without the use of dynamic checkbox array and without the javascript running....but im not able to do this with a dynamic array coz that is reqd for the javascript to work properly wht shd i do?!! does ne1 know how to do this?? Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 20, 2009 Share Posted January 20, 2009 Only checkboxes that are "checked" are passed in the form data. So, if you make the boxes an array using a name such as "u[]" then the first "checked" item will have an index of 0, the second 1, etc. Regardless of where they were positioned in the form. You shouldn't rely upon the index of the values in any case. None of the code you posted was really very complete. From your first post, it looks as if you are getting records from a database and creating a chekbox option for each. If that is the case you should set the value of each checkbox to the ID of the record. Then the PHP page that receives the POST data will be able to determine which records were selected. Something like this: <?php while($row=mysql_fetch_array($res)) { echo "<tr align=\"center\">\n"; echo " <td><input type=\"checkbox\" name=\"u[]\" value="{$row['id']}" /></td>\n"; echo " <th>{$row['name']}</th>\n"; echo " <th>{$row['username']}</th>\n"; echo " <td><input type=\"password\" name=\"pwd_{$row['id']}\" /></td>\n"; echo "</tr>\n"; } ?> Quote Link to comment Share on other sites More sharing options...
starrylitgal Posted January 21, 2009 Author Share Posted January 21, 2009 thnx for the reply,mjdamato. ya, i was taking the value of each chkbox to be the id of the record . It wasnt working. I hav found out the problem. It was a PHP code problem. I used the dynamic array of the checkboxes only.Didnt make ne changes there. Changed the code instead. I guess discussing abt the code here would be irrelevant in this section of the forum.. But thanx a lot for the javascript, it works perfectly fine!! thanx a lot!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.