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

    Join Date
    Jun 2002
    Posts
    42
    Rep Power
    13

    Question PHP javascript conflict - checkboxes array forms


    the following script will show how my javascript function is supposed to work: - but the PHP in the script will NOT work.

    PHP Code:
    <html>
    <head>
    <script language="JavaScript">
    <!--
    function checkChoice(field, i) {
    if (i == 0) { // "All" checkbox selected.
    if (field[0].checked == true) {
    for (i = 1; i < field.length; i++)
    field[i].checked = false;
       }
    }
    else  {  // A checkbox other than "Any" selected.
    if (field[i].checked == true) {
    field[0].checked = false;
          }
       }
    }
    //-->
    </script>
    </head>
    <body>

    <form action="test.php" method="post" name="furnitureform">
    <input type="radio" name="[B][COLOR=crimson]furniture[/COLOR] [/B] "  value="none" onclick="checkChoice(document.furnitureform.[B][COLOR=crimson]furniture[/COLOR] [/B] , 0)">none  <br>

    <input type="checkbox" name="[B][COLOR=crimson]furniture[/COLOR] [/B] " checked=checked value="beds" onclick="checkChoice(document.furnitureform.[B][COLOR=crimson]furniture[/COLOR] [/B], 1)">beds <br>
    <input type="checkbox" name="[B][COLOR=crimson]furniture[/COLOR] [/B] " checked=checked value="dressers" onclick="checkChoice(document.furnitureform.[B][COLOR=crimson]furniture[/COLOR] [/B], 2)">dressers <br>                
    <input type="checkbox" name="[B][COLOR=crimson]furniture[/COLOR] [/B] " checked=checked value="dining table" onclick="checkChoice(document.furnitureform.[B][COLOR=crimson]furniture[/COLOR] [/B], 3)">dining table <br> 
    <input type="checkbox" name="[B][COLOR=crimson]furniture[/COLOR] [/B] " checked=checked value="desks"  onclick="checkChoice(document.furnitureform.[B][COLOR=crimson]furniture[/COLOR] [/B], 4)">desks <br> 
    <input type="checkbox" name="[B][COLOR=crimson]furniture[/COLOR] [/B] " checked=checked value="coffee table"  onclick="checkChoice(document.furnitureform.[B][COLOR=crimson]furniture[/COLOR] [/B], 5)">coffee table <br>
    <input type="checkbox" name="[B][COLOR=crimson]furniture[/COLOR] [/B] " checked=checked value="sofas"  onclick="checkChoice(document.furnitureform.[B][COLOR=crimson]furniture[/COLOR] [/B], 6)">sofas <br> 
    <input type="checkbox" name="[B][COLOR=crimson]furniture[/COLOR] [/B] " checked=checked value="lampstands"  onclick="checkChoice(document.furnitureform.[B][COLOR=crimson]furniture[/COLOR] [/B], 7)">lampstands <br>
    <input type="checkbox" name="[B][COLOR=crimson]furniture[/COLOR] [/B] " checked=checked value="TV"  onclick="checkChoice(document.furnitureform.[B][COLOR=crimson]furniture[/COLOR] [/B], 8)">TV <br> 
    <input type="checkbox" name="[B][COLOR=crimson]furniture[/COLOR] [/B] " checked=checked value="others"  onclick="checkChoice(document.furnitureform.[B][COLOR=crimson]furniture[/COLOR] [/B], 9)">others <br>
    <br>
    <input type="submit" name="submit" >
    </form>

    </body>
    </html>

    <?php

    if($submit)
            {
            
    $furniture implode(','$_POST['furniture']);
            
            echo 
    "$furniture";
            }
            
    ?>
    in the following script - the PHP aspect works - but the javascript does not...

    PHP Code:
    <html>
    <head>
    <script language="JavaScript">
    <!--
    function checkChoice(field, i) {
    if (i == 0) { // "All" checkbox selected.
    if (field[0].checked == true) {
    for (i = 1; i < field.length; i++)
    field[i].checked = false;
       }
    }
    else  {  // A checkbox other than "Any" selected.
    if (field[i].checked == true) {
    field[0].checked = false;
          }
       }
    }
    //-->
    </script>
    </head>
    <body>

    <form action="test.php" method="post" name="furnitureform">
    <input type="radio" name="[B][COLOR=crimson]furniture[][/COLOR] [/B]"  value="none" onclick="checkChoice(document.furnitureform.[B][COLOR=crimson]furniture[][/COLOR] [/B], 0)">none  <br>

    <input type="checkbox" name="[B][COLOR=crimson]furniture[][/COLOR] [/B] " checked=checked value="beds" onclick="checkChoice(document.furnitureform.[B][COLOR=crimson]furniture[][/COLOR] [/B], 1)">beds <br>
    <input type="checkbox" name="[B][COLOR=crimson]furniture[][/COLOR] [/B] " checked=checked value="dressers" onclick="checkChoice(document.furnitureform.[B][COLOR=crimson]furniture[][/COLOR] [/B], 2)">dressers <br>                
    <input type="checkbox" name="[B][COLOR=crimson]furniture[][/COLOR] [/B] " checked=checked value="dining table" onclick="checkChoice(document.furnitureform.[B][COLOR=crimson]furniture[][/COLOR] [/B], 3)">dining table <br> 
    <input type="checkbox" name="[B][COLOR=crimson]furniture[][/COLOR] [/B] " checked=checked value="desks"  onclick="checkChoice(document.furnitureform.[B][COLOR=crimson]furniture[][/COLOR] [/B], 4)">desks <br> 
    <input type="checkbox" name="[B][COLOR=crimson]furniture[][/COLOR] [/B] " checked=checked value="coffee table"  onclick="checkChoice(document.furnitureform.[B][COLOR=crimson]furniture[][/COLOR] [/B], 5)">coffee table <br>
    <input type="checkbox" name="[B][COLOR=crimson]furniture[][/COLOR] [/B] " checked=checked value="sofas"  onclick="checkChoice(document.furnitureform.[B][COLOR=crimson]furniture[][/COLOR] [/B], 6)">sofas <br> 
    <input type="checkbox" name="[B][COLOR=crimson]furniture[][/COLOR] [/B] " checked=checked value="lampstands"  onclick="checkChoice(document.furnitureform.[B][COLOR=crimson]furniture[][/COLOR] [/B], 7)">lampstands <br>
    <input type="checkbox" name="[B][COLOR=crimson]furniture[][/COLOR] [/B] " checked=checked value="TV"  onclick="checkChoice(document.furnitureform.[B][COLOR=crimson]furniture[][/COLOR] [/B], 8)">TV <br> 
    <input type="checkbox" name="[B][COLOR=crimson]furniture[][/COLOR] [/B] " checked=checked value="others"  onclick="checkChoice(document.furnitureform.[B][COLOR=crimson]furniture[][/COLOR] [/B], 9)">others <br>
    <br>
    <input type="submit" name="submit" >
    </form>

    </body>
    </html>

    <?php

    if($submit)
            {
            
    $furniture implode(','$_POST['furniture']);
            
            echo 
    "$furniture";
            }
            
    ?>
    notice how i change the "furniture" into "furniture[]". when i do that, the javascript doesnt work - but the PHP does work. how do i get them both to work?
    Last edited by smakz; November 8th, 2002 at 05:27 AM.
  2. #2
  3. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,944
    Rep Power
    4033
    well, one, is there a reason that you have spaces after the check box names? Might want to take those out. Two, use the .elements array on the form or `this'

    if you need to do it for separate elements at any time, you can reference them like this:
    document.formName.elements['fieldName[]'];

    In your case, you can juse use `this' like so:
    <input type="checkbox" name="furniture" checked=checked value="beds" onclick="checkChoice(this, 1)">beds <br>
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    42
    Rep Power
    13
    nope - sorry does not work when i do that -

    did you get it to work??? did you try it, kicken??
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    28
    Rep Power
    0
    PHP Code:
    <input type="checkbox" name="furniture[0]" checked=checked value="beds" onclick="checkChoice(document.furnitureform.elements['furniture[0]'], 1)>beds <br>

    <input type="
    checkbox" name="furniture[1" checked=checked value="dressers" onclick="checkChoice(document.furnitureform.furniture['furniture[1]'] , 2)">dressers

    // etc 
    That should resolve your problems!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    42
    Rep Power
    13
    not working...

    what about

    <input type="radio" name="furniture" value="none" onclick="checkChoice(document.furnitureform.furniture,0)">none <br>


    ???

    you forgot about none

    did you get it to actually work richard??
  10. #6
  11. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Posts
    645
    Rep Power
    0
    for one thing it doesn't work either way you say. I get a javascript both ways.

    I have to ask, what are you trying to do? making it so you can click the radio button and have all of them uncheck?

    I don't understand why you want to have javascript check them when all you have to do is click on them and they check by themselves.

    also you don't need checked=checked as that is not valid, jsut have "checked" will work

    if that is all you want to do then use this.

    PHP Code:
    <html>
    <
    head>
    <
    script language="JavaScript">

    <!--
    function 
    CheckAll() {
        for (var 
    i=0;i<document.main.elements.length;i++) {
            var 
    document.main.elements[i];
            if ((
    e.name != 'all') && (e.type=='checkbox')) {
                
    e.checked document.main.all.checked;
            }
        }
    }
    </script>
    </head>
    <body>

    <form action="test.php" method="post" name="main">
    use this one to check all
    <input type="checkbox" name="all" onClick="CheckAll();"> <p>
    <input type="checkbox" name="del[0]" value="yes"><br>
    <input type="checkbox" name="del[1]" value="yes"><br>
    <input type="checkbox" name="del[2]" value="yes"><br>
    <input type="checkbox" name="del[3]" value="yes"><br>
    <input type="checkbox" name="del[4]" value="yes"><br>

    </body>
    </html> 
  12. #7
  13. No Profile Picture
    Gödelian monster
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 1999
    Location
    Central Florida, USA
    Posts
    2,307
    Rep Power
    62
    This is very similar to a question I answered at Experts Exchange: http://www.experts-exchange.com/Web/..._20339550.html
    The real n-tier system:

    FreeBSD -> PostgreSQL -> [any_language] -> Apache -> Mozilla/XUL

    Amazon wishlist -- rycamor (at) gmail.com
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    42
    Rep Power
    13
    scout - the javascript only works for me WITHOUT the stuff for the php - (when trying the script out, please excuse the spaces in the script above where it shows .furniture [] -->should be .furniture[] - it's a formatting error in the webpage since i made them bold and red- the spaces should not be there)

    by the way, scout, thanks for the tip w/ the "checked" comment...

    the problem/question is:

    If the user clicks on the "none" radio button, I want it to uncheck all the checkboxes - because the entry cannot be both "none and beds" - it can only be either/or. Also, let's say that the "none" radio button is checked. then if the user clicks on "beds" or anything else, I want the "none" radio button should be unchecked. again - because the entry cannot be both "none and beds" - it can only be either/or.

    you can see it in action here:

    http://javascript.internet.com/forms...led-boxes.html

    however- instead of "All" checkbox, I use a "none" radio button

    so again - i cannot get the php to work using "[]" in the form without messing up the javascript.
    Last edited by smakz; November 9th, 2002 at 01:16 AM.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    28
    Rep Power
    0
    There was a couple of typos in my last example

    But the code below will work for javascript and PHP and I have checked it!
    PHP Code:
    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
        <input type="checkbox" name="furniture[0]" checked=checked value="beds" onclick="checkChoice(document.furnitureform.elements['furniture[0]'], 1)">beds <br>
        <input type="checkbox" name="furniture[1]" checked=checked value="dressers" onclick="checkChoice(document.furnitureform.elements['furniture[1]'] , 2)">dressers
        
        <input type="submit" name="submit" value="submit">
    </form>

    <script>
    for (var i = 0; i < 2; i ++)
    {
        alert(document.forms[0].elements['furniture['+i+']'].value);
    }
    </script>


    <?php
    if (isset($_POST['submit']))
    {
        for (
    $i 0$i sizeof($_POST['furniture']); $i++)
        {
            echo 
    $_POST['furniture'][$i].'<br>';
        }
    }



    ?>
    As I'm not sure what you're trying to achieve with your javascript I've added my own which demonstrates referencing your checkbox values with no problem.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    42
    Rep Power
    13
    wrong answer again...

    please read my last reply carefully - it demonstrates how i want the javascript to work.

    anyone else with an answer to the problem??
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    28
    Rep Power
    0
    wrong answer again...

    please read my last reply carefully
    In your 1st post:
    the following script will show how my JavaScript function is supposed to work: - but the PHP in the script will NOT work.
    PHP Code:
    <html>
    <head>
    <script language="JavaScript">
    <!--
    function checkChoice(field, i) {
    if (i == 0) { // "All" checkbox selected.
    if (field[0].checked == true) {
    for (i = 1; i < field.length; i++)
    field[i].checked = false;
       }
    }
    else  {  // A checkbox other than "Any" selected.
    if (field[i].checked == true) {
    field[0].checked = false;
          }
       }
    }
    //-->
    </script>
    </head>
    <body>

    <form action="test.php" method="post" name="furnitureform">
    <input type="radio" name="furniture  "  value="none" onclick="checkChoice(document.furnitureform.furniture  , 0)">none  <br>

    <input type="checkbox" name="furniture  " checked=checked value="beds" onclick="checkChoice(document.furnitureform.furniture , 1)">beds <br>
    <input type="checkbox" name="furniture  " checked=checked value="dressers" onclick="checkChoice(document.furnitureform.furniture , 2)">dressers <br>                
    <input type="checkbox" name="furniture  " checked=checked value="dining table" onclick="checkChoice(document.furnitureform.furniture , 3)">dining table <br> 
    <input type="checkbox" name="furniture  " checked=checked value="desks"  onclick="checkChoice(document.furnitureform.furniture , 4)">desks <br> 
    <input type="checkbox" name="furniture  " checked=checked value="coffee table"  onclick="checkChoice(document.furnitureform.furniture , 5)">coffee table <br>
    <input type="checkbox" name="furniture  " checked=checked value="sofas"  onclick="checkChoice(document.furnitureform.furniture , 6)">sofas <br> 
    <input type="checkbox" name="furniture  " checked=checked value="lampstands"  onclick="checkChoice(document.furnitureform.furniture , 7)">lampstands <br>
    <input type="checkbox" name="furniture  " checked=checked value="TV"  onclick="checkChoice(document.furnitureform.furniture , 8)">TV <br> 
    <input type="checkbox" name="furniture  " checked=checked value="others"  onclick="checkChoice(document.furnitureform.furniture , 9)">others <br>
    <br>
    <input type="submit" name="submit" >
    </form>

    </body>
    </html>

    <?php

    if($submit)
            {
            
    $furniture implode(','$_POST['furniture']);
            
            echo 
    "$furniture";
            }
            
    ?>
    Have you actually tried this code? It's full of JavaScript errors! if this is the desired effect then fair enough, but before you condemn others for not reading your post correctly, I suggest you post accurate code.

    notice how i change the "furniture" into "furniture[]". when i do that, the JavaScript doesn’t work - but the PHP does work. how do i get them both to work?
    My post addresses the problem of naming form elements with [] suffixes and still being able to access them legally through the JavaScript DOM. If my post hasn't addressed your problem then I suggest you state your problems more clearly for others too understand.

    I have your code working with the assumed PHP and JavaScript functionality, so I suggest you re-read what I have already posted. It's worth noting that your JavaScript code will also need changing, but I suggest you post that problem in the JavaScript forum preferably with an improved attitude!

    There's also no need to send me PM's telling me that I was wrong again and should read posts more clearly in the future! you've clearly stated this view in the thread.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    42
    Rep Power
    13
    rich - your efforts are worth noting. thanks. there are times of frustration - and i apologize if you felt i was not appreciative of your help...

    for those of you who want to know the ANSWER to this problem:

    here is the correct html script that enables the use of BOTH javascript and php...

    PHP Code:
    <input type="radio" name="furniture[]" value="none" 
    onclick="checkChoice(this.form['furniture[]'], 0)">none <br>
    <
    input type="checkbox" name="furniture[]" checked=checked value="beds" 
    onclick="checkChoice(this.form['furniture[]'], 1)">beds <br>
    <
    input type="checkbox" name="furniture[]" checked=checked value="dressers" 
    onclick="checkChoice(this.form['furniture[]'], 2)">dressers <br
    <
    input type="checkbox" name="furniture[]" checked=checked value="dining table" 
    onclick="checkChoice(this.form['furniture[]'], 3)">dining table <br

IMN logo majestic logo threadwatch logo seochat tools logo