Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Mar 2005
    Posts
    30
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Javascript problems and PHP

    I've got a form where it's a bunch of checkboxes, each name of the checkbox is "bullets[]" so when the form is submitted, all of the selected checkboxes are in the $_POST['bullets'] array. My problem is that the way I have it set up in my loop, the name "bullets[]" makes my SelectAll and SelectNone Javascript functions not work any longer. JS error: "'length' is null or not an object.' Has anyone else run into this?

    Javascript in page head:
    Code:
      <script>
    	function checkAll(field) {
      for (i = 0; i < field.length; i++)
      	field[i].checked = true;
    	}
    
    	function uncheckAll(field) {
      for (i = 0; i < field.length; i++)
      field[i].checked = false;
    	}
      </script>
    PHP/HMTL Loop for Form Checkboxes and JS Func Buttons:
    Code:
    <input type=button name=\"CheckAll\" value=\"Check All\" onClick=\"checkAll(document.refineForm.bullets)\">
        <input type=button name=\"UnCheckAll\" value=\"Uncheck All\" onClick=\"uncheckAll(document.refineForm.bullets)\">
    
    <td rowspan=4 valign=\"top\"><input class=\"norm\" type=\"checkbox\" name=\"bullets[]\" value=\"$jobID\" checked></td>

  • #2
    New Coder
    Join Date
    Mar 2005
    Posts
    30
    Thanks
    2
    Thanked 0 Times in 0 Posts
    NM... this worked great.

    Fix

    Code:
    <html>
    <head>
    <title>Checkbox Fun</title>
    <script type="text/javascript"><!--
    
    var formblock;
    var forminputs;
    
    function prepare() {
    formblock= document.getElementById('form_id');
    forminputs = formblock.getElementsByTagName('input');
    }
    
    function select_all(name, value) {
    for (i = 0; i < forminputs.length; i++) {
    // regex here to check name attribute
    var regex = new RegExp(name, "i");
    if (regex.test(forminputs[i].getAttribute('name'))) {
    if (value == '1') {
    forminputs[i].checked = true;
    } else {
    forminputs[i].checked = false;
    }
    }
    }
    }
    
    if (window.addEventListener) {
    window.addEventListener("load", prepare, false);
    } else if (window.attachEvent) {
    window.attachEvent("onload", prepare)
    } else if (document.getElementById) {
    window.onload = prepare;
    }
    
    //--></script>
    </head>
    
    <body>
    
    <form id="form_id" name="myform" method="get" action="search.php">
    
    <a href="#" onClick="select_all('area', '1');">Check All Fruit</a> | <a href="#" onClick="select_all('area', '0');">Uncheck All 
    Fruit</a><br><br>
    
    <input type="checkbox" name="area[]" value="1" />Apples<br />
    <input type="checkbox" name="area[]" value="2" />Bananas<br />
    <input type="checkbox" name="area[]" value="3" />Chickens<br />
    <input type="checkbox" name="area[]" value="4" />Stoats
    
    <br><br><a href="#" onClick="select_all('location', '1');">Check All Locations</a> | <a href="#" onClick="select_all('location', 
    '0');">Uncheck All Locations</a><br><br>
    
    <input type="checkbox" name="location[]" value="1" />Brighton<br />
    <input type="checkbox" name="location[]" value="2" />Hove<br />
    
    </form>
    
    </body>
    </html>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •