PHP验证一堆复选框失败

时间:2014-03-26 15:53:46

标签: php

表格          

echo '<form action="formProcess.php" method="post">';
echo    '<input type="checkbox" name="country[]" value="US">';
echo    '<input type="checkbox" name="country[]" value="UK">';
echo    '<input type="checkbox" name="country[]" value="SE">';
echo    '<input type="checkbox" name="country[]" value="CA">';
echo    '<input type="submit" name="Submit" value="Submit">';
echo '</form>';

?>

formProcess.php

<?php


if (isset($_POST['country'])) {
    echo 'Set <br>';
    if (check($_POST['country'])){
        echo 'Country OK<br>';
    } else {
        echo 'Country Faulty<br>';
    }

} else {
    echo 'Not Set';
}

function check($colors) { // This function is where I fail
    global $countries;

    foreach($colors as $country) {
        echo "You selected: $country <br>";
        if (array_key_exists($country,$countries)) {
            return true;
        } else {
            return false;
        }
    }
}
?>

我试图验证一堆复选框,但我在函数内部return部分出错了。

问题是,即使我选择了多个复选框,我只输出一个值。问题出在函数的return中。这怎么做对了?

输出

Set

You selected: US // Outputs only one even when more are selected.

Country OK

countries array

$countries = array (
"US" => "United States Of America",
"GB" => "United Kingdom",
"CA" => "Canada"
// a lot more removed for this question
}

5 个答案:

答案 0 :(得分:2)

您可以使用所选复选框创建一个数组,然后返回该数组。

function check($colors) { 
    global $countries;
    $checkedCountries = array();
    foreach($colors as $country) {
        echo "You selected: $country <br>";
        if (array_key_exists($country,$countries)) {
            $checkedCountries[] = $country;
            echo $country . "<br />";
        }
    }
    if(count($checkedCountries) > 0)
    return true;
    else  
    return false;
}

答案 1 :(得分:1)

你的代码有点尴尬,但是:

function check($colors) {
    global $countries;

    foreach($colors as $country) {
        echo "You selected: $country <br>";
        if (!array_key_exists($country,$countries)) {
            return false;
        }
    }
    return true;
}

答案 2 :(得分:1)

您将在第一场比赛中返回结果,因为您希望显示所有选中的项目,您需要像这样重写您的功能..

<?php
function check($colors) {
    global $countries;

    $cntries=array();
    foreach($colors as $country) {
        if (array_key_exists($country,$countries)) {
            $cntries[]=$country;
        }else { return false;}
    }
    echo $str= "You selected ".implode("<br>",$cntries);
    return true;
}

答案 3 :(得分:1)

您将使用以下功能实现此目的

function check($colors) { // This function is where I fail
    global $countries;

    foreach($colors as $country) {

        //if country not exists, set return to false;
        if (!array_key_exists($country,$countries)) {
            return false;
        }
        //If you want to echo selected countries
        else{
                   echo "You selected" . $country;
        }
    }
    //If all contries exist, return true.

    return true;
}

然后你可以循环输出所选国家的阵列!

答案 4 :(得分:0)

  1. 在循环
  2. 之前将变量设置为false
  3. 在当前返回true的循环内将该变量设置为true
  4. 完全摆脱其他
  5. 在循环之后返回该变量