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 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了