PHP:REQUEST_METHOD作为html表单的基本错误处理

时间:2016-06-03 01:13:55

标签: php post error-handling

我使用来自html表单的输入来输出包含输入值的数组。我想执行基本的错误处理,说明哪些字段留空,如果没有输入字段则输出错误消息,然后是否输入所有字段。但是,我的代码并没有按照我的意愿行事。它在我的代码中的其他地方工作,但我无法弄清楚它们的区别。

修改 我得到"注意:未定义的索引:ageChoice"和"注意:未定义的索引:colourChoice"

HTML

<form action="profileFilter.php" method="POST">
    <p>Age: <br>
            <input type="radio" name="ageChoice" value="1">1 
            <input type="radio" name="ageChoice" value="2">2

    <p>Colour: <br>
            <input type="radio" name="colourChoice" value="Tabby">Tabby: 
            <input type="radio" name="colourChoice" value="Ginger">Ginger 

    <input type="submit" name="button">   
    </form>

ProfileArray.class.php

class ProfileArray {
 Public function dataArray() {
                $profileArray = array( 
                    array(  'Name' => 'Toby',
                            'Age' => 3, 
                            'Colour' => 'Ginger',
                            'Gender' => 'Male',     
                            'Personality' => 'Gentle',
                                                ),

                    array(  'Name' => 'Cassie',
                            'Age' => 2, 
                            'Colour' => 'Tabby',
                            'Gender' => 'Female',
                            'Personality' => 'Playful',
                                                ),
                                );

                    return $profileArray;
                    }
            }

profileFilter.php

include ("ProfileArray.class.php");
$object = new ProfileArray();
$profileArray = $object->dataArray();

if($_SERVER["REQUEST_METHOD"] == "POST") {

$passedAgeChoice = $_POST['ageChoice'];
$passedcolourChoice = $_POST['colourChoice']; 
$errorMessage = "";

    if (!empty($passedAgeChoice)) {
        echo $errorMessage = "WARNING: You have not entered an age to search for<br>";
    }

    if (!empty($passedColourChoice)) {
        echo $errorMessage = "WARNING: You have not entered a colour to search for<br>";
    }


    if ($errorMessage == TRUE) {
        echo '<h4 class="error">You have inputted nothing to search for</h4><a href="catMatch.html" class="error_button"> Go back and try again</a>';
    }

//If there are no errors submit the form
    if ($errorMessage == "") {
         echo '<h4 class="error">Here are your full search results</h4><a href="catMatch.html" class="error_button"> Back to Homepage</a><br>';
    }

}

2 个答案:

答案 0 :(得分:3)

在此处使用isset()!isset(),您将不会收到任何警告。正如我在评论中提到的,isset()最适用于单选按钮(和复选框)。

  • empty()主要用于字符串输入,长度等。

旁注:$passedcolourChoice也已替换为$passedColourChoice 变量区分大小写。

您可以省略$passedAgeChoice = $_POST['ageChoice']; $passedcolourChoice = $_POST['colourChoice'];并使用以下内容:

if(isset($_POST['ageChoice'])){

    $passedAgeChoice = $_POST['ageChoice'];

}

if(isset($_POST['colourChoice'])){

    $passedColourChoice = $_POST['colourChoice'];

}

$errorMessage = "";

    if (!isset($passedAgeChoice)) {
        echo $errorMessage = "WARNING: You have not entered an age to search for<br>";
    }

    if (!isset($passedColourChoice)) {
        echo $errorMessage = "WARNING: You have not entered a colour to search for<br>";
    }


    if ($errorMessage == TRUE) {
        echo '<h4 class="error">You have inputted nothing to search for</h4><a href="catMatch.html" class="error_button"> Go back and try again</a>';
    }

答案 1 :(得分:0)

在profileFilter.php中更改两行

$passedAgeChoice = (isset ($_POST['ageChoice'])) ?$_POST['ageChoice']:'';
$passedcolourChoice =(isset ($_POST['clourChoice'])) ? $_POST['colourChoice']:''; 

最佳做法:不要直接使用全局变量(例如$ _POST,$ _GET)。尝试在使用前验证它们。