Results 1 to 4 of 4

Thread: Validation of checkboxes

  1. #1
    Join Date
    Sep 2008
    Posts
    3

    Question Validation of checkboxes

    Hello All,

    1st things first, Russel & Co. GREAT JOB!!!

    OK, here's my Q?:

    I have a few lists of checkboxes on my (multi-page) form. 8 lists of checkboxes all numbered from 1 to 49 on 1 page. I want to validate that someone picks exact 6 numbers per list. Not 5, not 7, but 6 numbers from each list.

    What can I use to make this miracle happen?

    My syntax is:

    Each list:

    <input type="checkbox" name="row_01_01" value="01">
    <input type="checkbox" name="row_01_02" value="02">
    etc...

    next list is:

    <input type="checkbox" name="row_02_01" value="01">
    <input type="checkbox" name="row_02_02" value="02">
    etc...

    Thanks in advance for your help & time,

    Greetings from Amsterdam,
    Frank

    ************************************************
    http://www.my420online.com/
    ************************************************

  2. #2
    Join Date
    Feb 2007
    Posts
    162

    Default Re: Validation of checkboxes

    The best way to handle validation of any form field is using server side scripting, and not javascript. Don't be tempted to use javascript. Make the form post back to itself using the php variable $_SERVER['PHP_SELF'], or simply put a # as the action of the form.

    Now that the form is posting back to the same page, you will examine the post variables as they come in. In your case, you specifically want to check for 6 checkboxes, so as those variables are validated, put the ones that contain the value "yes" into an array. When all of the checkbox vars have been validated, and all of the yes values are in the array, you simply use the php function count() to check that the array contains 6. If the array does not contain six, simply tell the page not to post to the formmail script. Posting in this way would rely on cURL.

    So consider this:

    Code:
    <?php
        $fail = 0;
        if(isset($_POST['submit'])){
            $check = array("checkbox_one","checkbox_two","checkbox_three","checkbox_four","checkbox_five","checkbox_six","checkbox_seven");
            foreach( $check as $yes ){
                if(isset($_POST["$yes"]) && $_POST["$yes"] == 'yes'){
                    $yesArray["$yes"] = 'yes';
                }
            }
            if (count($yesArray) != 6){
                $fail = 1;
            }
            if ($fail == 1){
                $errorStack[] = '<li>You need to choose 6 checkboxes</li>';
            }else{
                    /* Uncomment this to send a cURL post to your formmail script
                    $url = "http://www.yoursite/formmail.php";
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL,$url);
                    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
                    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                    curl_setopt($ch, CURLOPT_POST, 1);
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $yesArray);
                    $confirmation_message = curl_exec($ch);
                    */
            }
        }
    ?>
    <html>
    <head>
        <title>Your page</title>
    </head>
    <body>
    <?php
        if(isset($_POST['submit'])){
            if ($fail == 1){
                echo "<ul>";
                foreach ($errorStack as $error){
                    echo $error;
                }
                echo "</ul>";
            }else{
                echo "<p>Thank you for your form submission</p>";
            }
        }
        if ($fail == 1 || !isset($_POST[submit])){
            echo '<form action="' . htmlspecialchars($_SERVER[PHP_SELF]) . '" method="post">
                    <input id="cb1" type="checkbox" name="checkbox_one" value="yes" /><label for="cb1">Checkbox 1</label><br />
                    <input id="cb2" type="checkbox" name="checkbox_two" value="yes" /><label for="cb2">Checkbox 2</label><br />
                    <input id="cb3" type="checkbox" name="checkbox_three" value="yes" /><label for="cb3">Checkbox 3</label><br />
                    <input id="cb4" type="checkbox" name="checkbox_four" value="yes" /><label for="cb4">Checkbox 4</label><br />
                    <input id="cb5" type="checkbox" name="checkbox_five" value="yes" /><label for="cb5">Checkbox 5</label><br />
                    <input id="cb6" type="checkbox" name="checkbox_six" value="yes" /><label for="cb6">Checkbox 6</label><br />
                    <input id="cb7" type="checkbox" name="checkbox_seven" value="yes" /><label for="cb7">Checkbox 7</label><br />
                    <input id="button" type="submit" name="submit" value="Submit Your Checkbox Choices" />
                </form>
            ';
        }
    ?>
    </body>
    </html>
    This is a working example.

  3. #3
    Join Date
    Sep 2008
    Posts
    3

    Default Re: Validation of checkboxes

    Great script and indeed, it works. Now this may sound stupid, but how do I make this work on my form? As there are 8 different lists with all numbers from 1 to 49 I see where this can be solved by naming them right, but how to make this php work on my form, not a clue...

    Thanks for the advice ;-)

    I did read this bit of code about a 100 times now and I do see what it does, and why, but how to implement this into my files.

    Greetz from Amsterdam,
    Frank

  4. #4
    Join Date
    Feb 2007
    Posts
    162

    Default Re: Validation of checkboxes

    I think you need to either decide to hire a php developer, or learn php. The code that I shared with you should be more than enough of an example to help you do what you need to do, and since you self admittedly don't have a clue how to proceed, then I advise you to hire somebody. php developers are everywhere, so it shouldn't take long to hunt one down. You might go to a place like phpfreaks.com and post a help wanted ad, or look at developers that are seeking work/projects. I'm available for hire, but last time I suggested such a thing in here, I felt that the response I received indicated that such a thing wasn't welcome on the tectite forum. Even still, send me a message on my website if you are interested.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. something not working, checkboxes
    By ScottR in forum Community Support
    Replies: 1
    Last Post: 13-Feb-2006, 09:22 PM
  2. Email validation - but only if field completed
    By tvulucy in forum FormMail Subscription Support
    Replies: 1
    Last Post: 31-Aug-2005, 09:12 PM
  3. Condition - restrict maximum checkboxes
    By tvulucy in forum FormMail Subscription Support
    Replies: 3
    Last Post: 22-Aug-2005, 08:42 PM
  4. new code for multiple checkboxes
    By nevdev in forum FormMail Subscription Support
    Replies: 2
    Last Post: 15-Jan-2005, 10:39 PM
  5. Checkboxes in JavaScript in conjunction with Formmail
    By Lesjo in forum FormMail Subscription Support
    Replies: 4
    Last Post: 15-Dec-2004, 10:46 PM

Bookmarks

Posting Permissions

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