1

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

I am new to PHP and playing around with it. I have following code in my php file.

$output = "<div style='display:none'>
    <div class='contact-top'></div>
    <div class='contact-content'>
        <h1 class='contact-title' style='text-align:center'>Write a Testimonial:</h1>
        <div class='contact-loading' style='display:none'></div>
        <div class='contact-message' style='display:none'></div>
        <form action='#' style='display:none'>
            <label for='contact-name'>*Name:</label>
            <input type='text' id='contact-name' class='contact-input' name='name' tabindex='1001' />
            <label for='contact-email'>*Email:</label>
            <input type='text' id='contact-email' class='contact-input' name='email' tabindex='1002' />";

    if ($extra["form_subject"]) {
        $output .= "
            <label for='contact-subject'>Subject:</label>
            <input type='text' id='contact-subject' class='contact-input' name='subject' value='' tabindex='1003' />";
    }

    $output .= "
            <label for='contact-message'>*Message:</label>
            <textarea id='contact-message' class='contact-input' name='message' cols='40' rows='4' tabindex='1004'></textarea>
            <br/>";

    if ($extra["form_cc"]) {
        $output .= "
            <label>&nbsp;</label>
            <input type='checkbox' id='contact-cc' name='cc' value='1' tabindex='1005' /> <span class='contact-cc'>Send me a copy</span>
            <br/>";
    }

    $output .= "
            <label>&nbsp;</label>
            <button type='submit' class='contact-send contact-button' tabindex='1006'>Send</button>
            <button type='submit' class='contact-cancel contact-button simplemodal-close' tabindex='1007'>Cancel</button>
            <br/>
            <input type='hidden' name='token' value='" . smcf_token($to) . "'/>
        </form>
    </div>

</div>";

    echo $output;

When I am trying to run the code, though the model box is appearing but it also showing a php error.

below is the error message I am getting

Notice: Undefined index: form_cc in F:\wamp\www\blog\wordpress\wp-content\plugins\demo\contact.php on line 56

Any idea what is going wrong?

Community
  • 1
  • 1
Umesh Awasthi
  • 23,407
  • 37
  • 132
  • 204

5 Answers5

2

Simple, it seems that you do not have a definition of this index in the array, in this case, it seems that you are not getting the value of input

2

Because the array extra has no index called form_cc. Do a var_dump of the array extra, so you can see where your problem lies. Also use the isset() and empty() methods.

Mob
  • 10,958
  • 6
  • 41
  • 58
2

In this case simply use:

if (!empty($extra["form_cc"])) {
   ...
}

empty() will check existence of key/index in the array and also whether value is set or not.

ioseb
  • 16,625
  • 3
  • 33
  • 29
2

The problem is that in the $extra array you haven't a position (index) called "form_cc". Thus, you should use

if (! empty($extra["form_cc"]))
{
   // do stuff
}
Aurelio De Rosa
  • 21,856
  • 8
  • 48
  • 71
0
if (isset($extra["form_cc"])) {
    ...
}

Will remove the warning message, this is a good habit to check isset()

Samik Chattopadhyay
  • 1,820
  • 6
  • 23
  • 34