#1
  1. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Posts
    26
    Rep Power
    0

    javascript and PHP


    I have a javascript function that I am passing a group of checkboxes to.
    The purpose of the js is to check/uncheck all of the boxes. That said, the function used to work perfectly when the name of the checkbox group was as simple as:

    <input type="checkbox" name="somename">

    now I am using the php method of:

    <input type="checkbox" name="somename[]">

    as I need to hv an array...

    the js does not like "somename[]" being passed in as a parameter. Any ideas as to how to pass that to the js correctly?

    Thanx,
    gaucho
  2. #2
  3. Wiking
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Sep 2000
    Location
    Sweden
    Posts
    3,608
    Rep Power
    26
    You probably just need to escape those [] with a backslash for your javascript to handle them correctly.

    //NoXcuz
    UN*X is sexy!
    who | grep -i blonde | date; cd ~; unzip; touch; strip; finger; mount; gasp; yes; uptime; umount; sleep
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2001
    Location
    England
    Posts
    967
    Rep Power
    14
    Explicitly enter the array numbers, like:

    PHP Code:
    for ($i=0$i<20$i++)
    {
      echo 
    "<input type=\"checkbox\" name=\"whatever[$i]\">";

    Then you can use a function like this:
    PHP Code:
    function DoCheck(method)
    {
      if (
    method == "a")
      {
        for(var 
    i=0i<20i++)
        {
           
    document.formname.elements["whatever[" "]"].checked true;
        }
      } else
      {
        for(var 
    i=0i<20i++)
        {
           
    document.formname.elements["whatever[" "]"].checked false;
        }
      }

    And call it with a button or with a link, like <a href="javascript:DoCheck('a');">Check All</a> | <a href="javascript:DoCheck('b');">Uncheck All</a>.
  6. #4
  7. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Posts
    26
    Rep Power
    0
    hi,
    thnx for the suggestions.. what i hv done so far:

    PHP Code:
                echo "<td class=ForumCellColor align=center><input type=\"checkbox\" name=\"delete[]\" value='".$row["code"]."'></td>"
    and in the javascript:

    <script>
    function check(){

    alert(document.frm_request.elements["delete[]"].length);

    var boxLength = document.frm_request.elements["delete[]"].length;

    for(var i=0; i<boxLength; i++){
    alert(document.frm_request.elements["delete["+i+"]"].checked);
    }

    }
    </script>

    I get an error stating that document.frm_request.elements["delete["+i+"]"].checked is not an object...

    The earlier alert shows the length correctly...
    any more suggestions??

    thnx,
    sands

IMN logo majestic logo threadwatch logo seochat tools logo