为了从县获得着名的体育项目,我创建了这个表格。
$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>";
我的问题是当我要验证这个时。在这里,我需要通过此表单验证来检查一些条件。
这些条件不符合需要显示错误信息。
我试过这样的东西..用这段代码我可以得到第一个错误信息,如果整个数组是空的..
更新:这是我目前的验证码..
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!';
}
更新:与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"
}
}
答案 0 :(得分:1)
请尝试以下代码:
<?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>
答案 1 :(得分:1)
试试这个:
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!';
}
答案 2 :(得分:0)
我看到最好的方法是将每个国家分别检查该国家的运动是否符合您所述的要求(空或不,和/或至少有1个),将其分解成块。选择3个体育项目,然后将错误的结果放在一个单独的数组中,看它是否已经进行了调整。
这意味着一旦它穿过这些国家,你就会得到一个数组,其结构类似于哪些有效,哪些没有。然后,您可以显示错误和原因。您甚至可以突出显示尚未正确填写的国家/地区,因为您已将其全部拆除。
$result = array(
'Australia' => array('error' => false, 'reason' => ''),
'New Zealand' => array('error' => true, 'reason' => 'No countries selected')
);