1

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>';
    }

}
17
  • 2
    $passedcolourChoice != $passedColourChoice (letter case) and error reporting would have helped you out here. Commented Jun 3, 2016 at 1:18
  • 1
    check for errors php.net/manual/en/function.error-reporting.php Commented Jun 3, 2016 at 1:24
  • 1
    I think you mean to check if it's empty, then error, i.e. if(empty(... instead of if(!empty(... Commented Jun 3, 2016 at 1:24
  • 1
    However and besides what @JeffPuckettII pointed out, it's best to use isset() with radio buttons, rather than empty(). Commented Jun 3, 2016 at 1:27
  • 1
    empty() is mostly used for strings/lengths (text inputs), isset() are best for radio/checkboxes, take my word on this ;-) Use an isset() / !isset() against the POST arrays, rather than assigning variables to POST arrays (which is most likely why you're getting those notices), then using conditional statements after. You're actually working /coding harder than you should. Commented Jun 3, 2016 at 1:36

2 Answers 2

2

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>';
    }
Sign up to request clarification or add additional context in comments.

8 Comments

This worked! However my final statement if ($errorMessage == TRUE) {... this runs if there are any errors at all and opposed to when they are all blank. Any suggestions there?
@eainnem this is when you want to use empty - to check if a string is empty. $errorMessage is a string, so use if(!empty($errorMessage)) instead of if ($errorMessage == TRUE)
@JeffPuckettII haha! - I know Jeff. But seeing that there were no answers given at the time and comments were starting to pile up, I figured I'd put one in. I did though tell the OP in this comment that it was "nite nite" for me, and couldn't respond after since my head was embedded deep in my pillow. As for Stack paying me enough; hehe, well if I had a nickel for every comment I left and every answer I've given so far, I'd probably have a "paid" house by now ;-)
@eainnem I'm glad everything turned out well. I had gone to bed following my last comment to you last night, so I was unable to respond at the time. Cheers and you're very much welcome.
@JeffPuckettII "this is when you want to use empty - to check if a string is empty" - Yes, absolutely. Thanks for that comment Jeff :-)
|
0

In profileFilter.php change two lines

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

Best practice: don't use global variables (e.g. $_POST, $_GET) directly. Try to validate them before use.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.