1

I have made a simple form tried to do validation on it but keep getting few errors like undefined indexes. Here is the code:

<?php

//echo '<pre>'.print_r($_POST,true).'</pre>';

$display_result = false;

$form_data = [

    'name' => [
        'value' => '',
        'error' => false,
        'err_msg' => '',
    ],

    'flavor' => [
        'value' => '',
        'error' => false,
        'err_msg' => '',
    ],

    'cakesize' => [
        'value' => '',
        'error' => false,
        'err_msg' => '',
    ],

    'filling' => [
        'value' => [],
        'error' => false,
        'err_msg' => '',
    ],

    'agree' => [
        'value' => '',
        'error' => false,
        'err_msg' => '',
    ],
];

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

    $name = trim ( $_POST['name'] );
    if (empty($name) ) {

        $form_data['name']['error'] = true;
        $form_data['name']['err_msg'] = "Please enter a name";
    }

    else {

        $form_data['name']['value'] = $name;
        $form_data['name']['error'] = false;
    }


    $flavor = $_POST['flavor'];
    if ( empty($flavor) ) {

        $form_data['flavor']['error'] = true;
        $form_data['flavor']['err_msg'] = "Please select a flavor";
    }

    else {

        $form_data['flavor']['value'] = $flavor;
        $form_data['flavor']['error'] = false;
    }

    $cakesize = $_POST['cakesize'];
    if( empty($cakesize) ){

        $form_data['cakesize']['error'] = true;
        $form_data['cakesize']['err_msg'] = "Please select the size";
    }

    else {
        $form_data['cakesize']['value'] = $cakesize;
        $form_data['cakesize']['error'] = false;
    }

    $filling = $_POST['filling'];
    if( empty($filling) || count($filling) < 2 ) {

        $form_data['filling']['error'] = true;
        $form_data['filling']['err_msg'] = "Please select atleast 2 fllings";
    }

    else {
        $form_data['filling']['value'] = $filling;
        $form_data['filling']['error'] = false;
    }


    $agree = $_POST['agree'];
    if( empty($agree) ) {

        $form_data['agree']['error'] = true;
        $form_data['agree']['err_msg'] = "Tick the box if you agree to the terms and conditions";
    }

    else {
        $form_data['agree']['value'] = $agree;
        $form_data['agree']['error'] = false;
    }
}


?>

<!DOCTYPE html>

<html>
    <head>
        <meta charset="UTF-8">
        <title>Cake ordering Form</title>

        <style>

            .reqd {
                color: red;
            }

        </style>
    </head>
    <body>

        <form action="" method="post" id="cakeForm">

            <fieldset>
                <legend>Personal Details</legend>
                <p>
                    <label for="name">Your Full Name: <span class="reqd"><?php print 
                    $form_data['name']['error'] ? $form_data['name']['err_msg'] : '' ?></span></label><br />
                    <input type="text" name="name" id="name" />
                </p>
            </fieldset>
            <br />
            <fieldset>
                <legend>Make Your cake!</legend>
                <p>
                    <label for="flavor">Select your flavor: <span class="reqd"><?php print 
                    $form_data['flavor']['error'] ? $form_data['flavor']['err_msg'] : '' ?></span></label><br />
                    <select id="flavor" name="flavor">
                        <option value="0">Select Flavor</option>
                        <option value="yellow" >Yellow</option>
                        <option value="white">White</option>
                        <option value="chocolate">Chocolate</option>
                        <option value="combo">Combo</option>
                   </select>
                </p>

                <p>
                    <label>Size of the cake: </label><br />
                    <label><input type="radio" name="cakesize" value="round6"/>Round cake 6" -  serves 8 people <br /></label>
                    <label><input type="radio" name="cakesize" value="round8"/>Round cake 8" - serves 12 people <br /></label>
                    <label><input type="radio" name="cakesize" value="round10"/>Round cake 10" - serves 16 people <br /></label>
                    <label><input type="radio" name="cakesize" value="round12"/>Round cake 12" - serves 30 people <br /></label>
                </p>

                <p>
                    <label>Fillings: </label><br />
                    <label class="checkFill"><input type="checkbox" name="filling" value="lemon"/>Lemon <br /></label>
                    <label class="checkFill"><input type="checkbox" name="filling" value="custard"/>Custard <br /></label>
                    <label class="checkFill"><input type="checkbox" name="filling" value="fudge"/>Fudge <br /></label>
                    <label class="checkFill"><input type="checkbox" name="filling" value="mocha"/>Mocha <br /></label>
                    <label class="checkFill"><input type="checkbox" name="filling" value="raspberry"/>Raspberry <br /></label>
                    <label class="checkFill"><input type="checkbox" name="filling" value="pineapple"/>Pineapple <br /></label>
                </p>
            </fieldset>


            <p>
                <label class="inlinelabel"><input type="checkbox" id="agree" name="agree" /> I agree to the terms and conditions </label>
            </p>

            <p>
                <input type="submit" value="submit" name="submit" />
            </p>


        </form>
    </body>
</html>

Please help....

4
  • You need to assign value as false for $form_data['name']['error'] and all. When you load your page its not getting any value. Commented May 30, 2016 at 12:14
  • the value for $form_data['name']['error'] is already false.....$name and $flavor is working fine...only trouble with the rest Commented May 30, 2016 at 12:16
  • can you share use error notification ? Commented May 30, 2016 at 12:18
  • Undefined Index: cakesize Undefined Index: filling Undefined Index: agree Commented May 30, 2016 at 12:19

2 Answers 2

1

Use isset() or empty() to check postdata is set or not. Use below code.

<?php

//echo '<pre>'.print_r($_POST,true).'</pre>';

$display_result = false;

$form_data = [

    'name' => [
        'value' => '',
        'error' => false,
        'err_msg' => '',
    ],

    'flavor' => [
        'value' => '',
        'error' => false,
        'err_msg' => '',
    ],

    'cakesize' => [
        'value' => '',
        'error' => false,
        'err_msg' => '',
    ],

    'filling' => [
        'value' => [],
        'error' => false,
        'err_msg' => '',
    ],

    'agree' => [
        'value' => '',
        'error' => false,
        'err_msg' => '',
    ],
];

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

    $name = trim ( $_POST['name'] );
    if (empty($name) ) {

        $form_data['name']['error'] = true;
        $form_data['name']['err_msg'] = "Please enter a name";
    }

    else {

        $form_data['name']['value'] = $name;
        $form_data['name']['error'] = false;
    }


    $flavor = !empty($_POST['flavor']) ? $_POST['flavor'] : "";
    if ( empty($flavor) ) {

        $form_data['flavor']['error'] = true;
        $form_data['flavor']['err_msg'] = "Please select a flavor";
    }

    else {

        $form_data['flavor']['value'] = $flavor;
        $form_data['flavor']['error'] = false;
    }

    $cakesize = !empty($_POST['cakesize']) ? $_POST['cakesize'] : "";
    if( empty($cakesize) ){

        $form_data['cakesize']['error'] = true;
        $form_data['cakesize']['err_msg'] = "Please select the size";
    }

    else {
        $form_data['cakesize']['value'] = $cakesize;
        $form_data['cakesize']['error'] = false;
    }

    $filling = !empty($_POST['filling']) ? $_POST['filling'] : "";
    if( empty($filling) || count($filling) < 2 ) {

        $form_data['filling']['error'] = true;
        $form_data['filling']['err_msg'] = "Please select atleast 2 fllings";
    }

    else {
        $form_data['filling']['value'] = $filling;
        $form_data['filling']['error'] = false;
    }


    $agree = !empty($_POST['agree']) ? $_POST['agree'] : "";
    if( empty($agree) ) {

        $form_data['agree']['error'] = true;
        $form_data['agree']['err_msg'] = "Tick the box if you agree to the terms and conditions";
    }

    else {
        $form_data['agree']['value'] = $agree;
        $form_data['agree']['error'] = false;
    }
}


?>
Sign up to request clarification or add additional context in comments.

2 Comments

isset() will allow empty values, !empty() would be better.
yes.. you are right. I have changed it. @PedroLobito
0

Suggest you do check $_POST isset before assign. i.e.

$flavor = isset($_POST['flavor']) ? $_POST['flavor'] : NULL;

For checkbox may not submitted if unchecked, it will cause undefined for this case.

2 Comments

hello @ruchishParikh my form validation script that you modified, why is it that we must check the set value first of $flavor, $cakesize and $agree before setting its value?? please provide me a clear understanding regarding this if you don't mind...thank you
php.net/manual/en/function.isset.php Go to this link its provide you all information about it. @AbhijitBorkakoty

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.