dqsvnsad79721 2016-06-03 01:13
浏览 63
已采纳

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

I am using input from a html form to output arrays containing the value inputted. I would like to carry out basic error handling that states what fields were left empty, a error message if no fields are inputted and then if all of the fields are inputed. However, my code isn't doing as I would like. It works elsewhere in my code but I can not figure out the difference.

EDIT: I am getting "Notice: Undefined index: ageChoice" and "Notice: Undefined index: 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条回答 默认 最新

  • douyan8027 2016-06-03 01:46
    关注

    Use isset() and !isset() for everything here and you won't get any warnings. As I mentioned in comments, isset() is best used for radio buttons (and checkboxes).

    • empty() is mostly used for string inputs, lengths etc.

    Sidenote: $passedcolourChoice has also been replaced with $passedColourChoice.
    Variables are case-sensitive.

    You can omit $passedAgeChoice = $_POST['ageChoice']; $passedcolourChoice = $_POST['colourChoice']; and use the below:

    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条)

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错