0

To get famous sports from counties, I created this form.

$sports = array (
            'Australia' =>  array (
                            1 => 'Cricket',
                            2 => 'Foot Ball',
                            3 => 'Net Ball',
                            4 => 'Kabadi',
                            5 => 'Ragby',
                            6 => 'Basket Ball',
                            7 => 'Volley Ball',
                        ),          
          'New Zealand' =>  array (
                            1 => 'Cricket',
                            2 => 'Foot Ball',
                            3 => 'Net Ball',
                            4 => 'Ragby',
                            5 => 'Basket Ball',                         
                        ),        
              'England' =>  array (
                            1 => 'Cricket',
                            2 => 'Foot Ball',
                            3 => 'Net Ball',
                            4 => 'Ragby',
                            5 => 'Karom',                           
                            6 => 'Basket Ball',                         
                            7 => 'Table Tennis',                            
                            8 => 'Tennis',                          
                        ), 
                );

echo '<br><form action="" method="post">';
    foreach ( $sports AS $country => $sport ) { 
        echo "<h3>{$country}</h3\n";    
        foreach ($sport AS $k => $v) { 
            echo "<br /><input type='checkbox'  name='country-sport[{$country}][]' value='{$k}' />{$v}\n";
        } 
    }
echo "\n<br><input type='submit' value='go' />\n</form>";

My problem is When I am going to validate this. Here I need to check some conditions with this form validation.

  1. country-subject array is completely empty or not
  2. at least 1 or upto 3 sports for each countries have selected or not

these conditions not met need to display error message.

I tried something like this.. with this code I can get 1st error message which is if whole array is empty..

UPDATE : this is my validation code so far..

if ( isset($_POST['country-sport']) && is_array( $_POST['country-sport'])) {

    foreach ( $_POST['country-sport'] AS $country => $sport) { 

        if ( count($sport) >= 1 && count($sport) <= 3) { //checking that it has 3 or more values.
             //process
        } else {
            echo "select at leat 1 or upto 3 sports for {$country} ";          
        }   
    }

} else {    
    echo 'You have not selected sports for any country!';
}

UPDATE : with var_dump($_POST['country-sport']);

array(3) {
  ["Australia"]=>
  array(3) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "2"
    [2]=>
    string(1) "3"
  }
  ["New Zealand"]=>
  array(3) {
    [0]=>
    string(1) "2"
    [1]=>
    string(1) "3"
    [2]=>
    string(1) "4"
  }
  ["England"]=>
  array(3) {
    [0]=>
    string(1) "6"
    [1]=>
    string(1) "7"
    [2]=>
    string(1) "8"
  }
}
2
  • Checkboxs' names ara 'country-subject...' not 'country-sport'. Fix it. Then do a echo "<pre>"; var_dump($_POST); to see how it looks like when posted. Commented Jan 18, 2013 at 9:35
  • sorry.. its mistake. I corrected it Commented Jan 18, 2013 at 9:43

3 Answers 3

1

try this:

if ( isset($_POST['country-sport']) && !empty( $_POST['country-sport'])) {//using empty

    foreach ( $_POST['country-sport'] AS $country => $sport) { 

        if ( count($sport) > 2) { //checking that it has 3 or more values.
             //process
        } else {
            echo "select at leat 1 or upto 3 sports for {$country} ";          
        }   
    }
} else {    
    echo 'You have not selected sports for any country!';
}
Sign up to request clarification or add additional context in comments.

8 Comments

this code also works for one country. I need to check every selected sports under each counties when the form submit. That mean I need to check at least one or upto 3 sports have selected for every countries in the form.
isn't your $_POST['country-sport'] has the structure that you shown in your question? @TharangaNuwan
@TharangaNuwan - what's the result of var_dump($_POST['country-sport']); ?
Check my question. I updated it with var_dump($_POST['country-sport']); result. Also change your IF condition (under foreach loop)
@TharangaNuwan - if what you need is to check whether the post is empty or not and then to check that each country has at least three sports, then my code will work for you. please recheck it. I just tried it for similar array as your post and it's working.
|
1

Please try below code:

<?php
if (isset($_POST['country-sport']) && is_array($_POST['country-sport']))
{      
    foreach ($_POST['country-sport'] AS $country => $sport)
    {
        $varTotal = 0;
        foreach($sport as $k=>$v)
        {
            if($v != '')
            {
                $varTotal += 1;
            }
        }
        if ($varTotal >= 1 && $varTotal <= 3)
        {

        } 
        else
        {
            $arrError[$country] = 'select at least 1 or upto 3 sports for '.$country ;
        }
    }
    print_r($arrError);
} 
else
{
    echo 'You have not selected sports for any country!';
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        $sports = array(
            'Australia' => array(
                1 => 'Cricket',
                2 => 'Foot Ball',
                3 => 'Net Ball',
                4 => 'Kabadi',
                5 => 'Ragby',
                6 => 'Basket Ball',
                7 => 'Volley Ball',
            ),
            'New Zealand' => array(
                1 => 'Cricket',
                2 => 'Foot Ball',
                3 => 'Net Ball',
                4 => 'Ragby',
                5 => 'Basket Ball',
            ),
            'England' => array(
                1 => 'Cricket',
                2 => 'Foot Ball',
                3 => 'Net Ball',
                4 => 'Ragby',
                5 => 'Karom',
                6 => 'Basket Ball',
                7 => 'Table Tennis',
                8 => 'Tennis',
            ),
        );

        echo '<br><form action="" method="post">';
        foreach ($sports AS $country => $sport)
        {
            echo "<h3>{$country}</h3\n";
            $i=0;
            foreach ($sport AS $k => $v)
            {  
                // This will help to get all the fields name in post 
                echo "<input type='hidden'  name='country-sport[{$country}][]' value='' />";              
                echo "<br /><input type='checkbox'  name='country-sport[{$country}][]' value='{$k}' />{$v}\n";
                $i++;
            }
        }
        echo "\n<br><input type='submit' value='go' />\n</form>";
        ?>
    </body>
</html>

6 Comments

Its working for one array.. that mean If I select sports for one country its working. But I need to check weather subjects have selected or not for every counties.
Please try to run full attached code. when you are selecting any checkbox, you will get error in array for each country. and if you selected sports from one country then you will error for rest counties.
please note that I have added some code in html part as well.. echo "<input type='hidden' name='country-sport[{$country}][]' value='' />";
yes your code working..thanks for it.. But it not working with my actual code. With my actual code multi-dimensional array generating dynamically.
I would like to know. can I get a solution without changing HTML
|
0

I see that the best way to do it is to break it down into chunks by looping through each country individually checking that the sports for the country meet the requirements you stated (empty or not, and/or has at least 1 to 3 sports selected), then put the result of the error in a separate array as to whether it has valdiated.

That means that once it has run through the countries you will have an array with a similar structure below as to which ones are valid and which ones haven't. From there you can then display your error and the reason. You could even highlight the country which hasn't been filled in correctly because you've broken it all down.

$result = array(
    'Australia' => array('error' => false, 'reason' => ''),
    'New Zealand' => array('error' => true, 'reason' => 'No countries selected')
);

1 Comment

little hard to understand.. can you elaborate it with an example? Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.