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 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question number of checkbox selected

    i have a code that should trap that only 1 checkbox should be selected but i don't know how????? any suggestions please please plase???
    thanks in advance

  • #2
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Do you mean that you are looking for a script to check and make sure that only one check box is checked?

    If so I'd recommend a client-side language (javascript) to do so as to not tie up my server. But it can be done in PHP as well.

    Create an array by naming all the check boxes the same name and then run a for loop to check which ones have a value of being checked.

    OR

    You could simply use radio buttons
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #3
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If so I'd recommend a client-side language (javascript) to do so as to not tie up my server.
    You need to do it in PHP. Javascript is just a nice for the user, but if JS is disabled then it could break your script.

    Here is an example
    http://degs.co.uk/test/hct/loop_thro..._php_array.php
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <script>
    function check(){
    count = 0;
    str = '';
        for(x=0; x<document.form1.elements["checkbox[]"].length; x++){
            if(document.form1.elements["checkbox[]"][x].checked==true){
                str += document.form1.elements["checkbox[]"][x].value + ',';
                count++;
            }
        }
        
        if(count==0){
            alert("You must choose at least 1");
            return false;
        }
        else if(count>3){
            alert("You can choose a maximum of 3");
            return false;
        }
        else {
        alert("You chose " + count + ": " + str.substring(0,str.length-1));
        document.form1.submit();
        }
    }
    </script>
    </head>

    <body>
    <?php
    if(isset($_POST['Submit'])){
        if(
    is_array($_POST['checkbox'])){
            echo 
    "You chose: <br>";
        
            foreach(
    $_POST['checkbox'] as $key => $val){
                echo 
    "$key: $val <br />";
            }
        }
        else{
            echo 
    "Something went wrong. Try again.";
        }
    }
    ?>
    <form name="form1" id="form1" method="post" action="" onsubmit="return check()">
      <p>
        <input type="checkbox" name="checkbox[]" value="red" />
      Red<br />
      <input type="checkbox" name="checkbox[]" value="green" />
      Green<br />
      <input type="checkbox" name="checkbox[]" value="blue" />
      Blue<br />
      <input type="checkbox" name="checkbox[]" value="black" />
      Black<br />
      <input type="checkbox" name="checkbox[]" value="white" />
      White<br />
      <input type="checkbox" name="checkbox[]" value="yellow" />
      Yellow<br />
      <input type="checkbox" name="checkbox[]" value="purple" /> 
      Purple
    </p>
      <p>
        <input type="submit" name="Submit" value="Click Me" />
    </p>
    </form>
    </body>
    </html>

  • #4
    New Coder
    Join Date
    Feb 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    Last edited by katherine06; 02-16-2006 at 02:42 AM.

  • #5
    New Coder
    Join Date
    Feb 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    y thank you for the idea!thanks a lot for giving me an example.
    It is in a different concept but it did help me to have other idea! Thanks a lot p&#246;!!!!! God Bless!!!!

    Quote Originally Posted by StupidRalph
    You could simply use radio buttons
    err..i have a check all, delete, and edit buttons,.i choose checkboxes so the user can select all the checkboxes he wants to delete and choose only one what he wants to edit..but still thank you for the concern
    Last edited by katherine06; 02-16-2006 at 02:43 AM.


  •  

    Posting Permissions

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