Home php PHP Notice: Undefined index

PHP Notice: Undefined index

Author

Date

Category

The next batch of errors in the code is maddening again …

PHP Notice: Undefined index: submit on line 14
PHP Notice: Undefined index: name on line 105
PHP Notice: Undefined index: email on line 106
PHP Notice: Undefined index: theme on line 107
PHP Notice: Undefined index: text on line 108

Well, the code itself:

& lt;? php // 266
if (! $ exe)
  {
  $ title_normal [] = array (
              'link' = & gt; $ settings ['patch_region']. $ cat. '/',
              'name' = & gt; $ locale [266]
              );
  }
if ($ exe)
  {
  $ form = true;
  if ($ _ POST ['submit']! = "")
    {
    $ err = array ();
    if (! isset ($ _ POST ['name'])) $ _POST ['name'] = "";
    if (! isset ($ _ POST ['email'])) $ _POST ['email'] = "";
    if (! isset ($ _ POST ['theme'])) $ _POST ['theme'] = "";
    if (! isset ($ _ POST ['text'])) $ _POST ['text'] = "";
    if ($ _ POST ['name'] == "") {$ err [] = $ locale [151];}
    if ($ _ POST ['theme'] == "") {$ err [] = $ locale [601];}
    if ($ _ POST ['email']! = "")
      {
      if (! preg_match ("/ [0-9a-z _] + @ [0-9a-z _ ^ \.] / i", $ _POST ['email']))
        {
        $ err [] = $ locale [600];
        }
      }
    else
      {
      $ err [] = $ locale [152];
      }
    if ($ _ POST ['text'] == "") {$ err [] = $ locale [153];}
    $ _POST ['text'] = save_text ($ _ POST ['text']);
    $ text = "
    & lt; b & gt; ". $ locale [281]." & lt; / b & gt ;: ". $ _ POST ['name']." & lt; br / & gt;
    & lt; b & gt; ". $ locale [72]." & lt; / b & gt ;: ". $ _ POST ['theme']." & lt; br / & gt;
    & lt; b & gt; ". $ locale [21]." & lt; / b & gt ;: ". $ _ POST ['email']." & lt; br / & gt;
& lt; b & gt; IP & lt; / b & gt ;: ". $ settings ['ip']." & lt; br / & gt;
    & lt; hr & gt;
    ". $ _ POST ['text'];
    if (! captcha ($ settings, 'check')) {$ err [] = $ locale [46];}
    if (count ($ err) & gt; 0)
      {
      echo "
          & lt; center & gt;
        & lt; div class = 'error alert' style = 'width: 500px; max-width: 100%; text-align: left; '& gt;
                & lt; button class = 'close' data-dismiss = 'alert' & gt; × & lt; / button & gt;
          & lt; ul & gt;
          ";
          foreach ($ err as $ v)
            {
            echo "
            & lt; li & gt; ". $ v." & lt; / li & gt;
            ";
            }
          echo "
          & lt; / ul & gt;
        & lt; / div & gt;
         & lt; / center & gt;
      ";
      }
    else
      {
      $ form = false;
      if (send_mail (EMAIL, $ _POST ['theme'], $ text, array (), $ _POST ['email']))
        {
        echo "
        & lt; center & gt;
          & lt; div class = 'ok' style = 'width: 500px; max-width: 100%; '& gt;
            ". $ locale [155]."
          & lt; / div & gt;
        & lt; / center & gt;
        ";
        }
      else
        {
        echo "
        & lt; center & gt;
          & lt; div class = 'error alert' style = 'width: 500px; max-width: 100%; text-align: left; '& gt; & lt; button class =' ​​close 'data-dismiss =' ​​alert '& gt; × & lt; / button & gt;
            ". $ locale [602]." ".EMAIL."
          & lt; / div & gt;
        & lt; / center & gt;
        ";
        }
      }
    }
  if ($ form)
    {
    echo "
    & lt; center & gt;
& lt; div class = \ "mail_form \" & gt;
    & lt; form action = '?' method = 'POST' & gt;
    & lt; table border = '0' class = 'table mail_table' & gt;
    & lt; tr & gt; & lt; td & gt; ". $ locale [29]." & lt; / td & gt; & lt; td & gt; & lt; input name = 'name' type = 'text' value = '". $ _ POST [' name ' ]. "'& gt; & lt; / td & gt; & lt; / tr & gt;
    & lt; tr & gt; & lt; td & gt; ". $ locale [26]." & lt; / td & gt; & lt; td & gt; & lt; input name = 'email' type = 'text' value = '". $ _ POST [' email ' ]. "'& gt; & lt; / td & gt; & lt; / tr & gt;
    & lt; tr & gt; & lt; td & gt; ". $ locale [72]." & lt; / td & gt; & lt; td & gt; & lt; input name = 'theme' type = 'text' value = '". $ _ POST [' theme ' ]. "'& gt; & lt; / td & gt; & lt; / tr & gt;
    & lt; tr & gt; & lt; td & gt; ". $ locale [74]." & lt; / td & gt; & lt; td & gt; & lt; textarea name = 'text' rows = '5' wrap = 'on' style = 'min-height : 80px '& gt; ". $ _ POST [' text ']." & Lt; / textarea & gt; & lt; / td & gt; & lt; / tr & gt;
    & lt; tr & gt; & lt; td & gt; ". $ locale [45]." & lt; / td & gt;
        & lt; td & gt; & lt; img class = 'captcha-mail' src = '". $ settings [' patch ']." captcha / ". rand (0, 10000)."' width = '80 'height = '40 '& gt; & lt; input name =' captcha 'type =' text 'value =' 'style =' width: 80px; height: 40px; text-align: center; font-size: 21px; '& gt; & lt; / td & gt ; & lt; / tr & gt;
    & lt; / table & gt;
    & lt; input name = 'submit' type = 'submit' value = '". $ locale [55]."' class = 'btn btn-success' & gt;
    & lt; / form & gt;
    & lt; / div & gt;
    & lt; / center & gt;
    ";
    }
  }
? & gt;

Answer 1, authority 100%

$ _ POST ['submit']! = ""

This is where the existing variable $ _POST [‘submit’] is checked, but it is not
Check it like this:

if (isset ($ _ POST ['submit'])) {}

or like this:

if (! empty ($ _ POST ['submit'])) {}

And if you still need to compare, then in the condition of this if, if the check is successful, compare.
i.e.

if (! empty ($ _ POST ['submit'])) {
    if ($ _ POST ['submit']! = '') {}
  }

Answer 2, authority 97%

In this case, the error tells you that you are trying to call undefined array keys.
If using PHP & gt; = 7 can be solved by the null coalescing operator ?? – It returns the first operand if it is given and is not NULL, and otherwise it returns the second operand:

if ($ _ POST ['submit'] ?? '') {
}
if ($ _ POST ['name'] ?? $ _POST ['email'] ?? $ _POST ['theme'] ?? $ _POST ['text'] ?? '') {
}

Or immediately define and check in variables, and then through if:

$ name = $ _POST ['name'] ?? '';
$ email = $ _POST ['email'] ?? '';
$ theme = $ _POST ['theme'] ?? '';
$ text = $ _POST ['text'] ?? '';
if (! $ name) {
  // ...
} else if (! $ email) {
  // ...
} else if (! $ theme) {
  // ...
} else if (! $ text) {
  // ...
} else {
  // ...
}

If using php & lt; 7, to check that a variable or an array value is not empty, use the empty construction.

if (empty ($ _ POST ['submit'])) {
  // variable is empty
}
if (! empty ($ _ POST ['submit'])) {
  // variable is not empty
}

isset works a little differently, I advise you to look at the type comparison table .
On a note:
Supports the and so on operator .

if (isset ($ _ POST ['name'], $ _POST ['email'], $ _POST [' theme '], $ _POST [' text '])) {
  // ...
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions