0

Got an error of

Undefined index: mytext

because I've got a form wherein it has a button wherein it adds an input field dynamically! But what if I will not add an input field? Can it be possible that it will get passed the variable in the php code.

Here is my declaration of mytext variable

if ($_POST['mytext']) {
    foreach ( $_POST['mytext'] as $key=>$value ) {
    $values = mysqli_real_escape_string($conn, $value);
    $query = mysqli_query($conn,"INSERT INTO tblalumni_past_o (pastoccu, alum_id) VALUES ('$values', '$id')");
        }
    }

So can it get passed if the input field mytext is not in the form?

sauce
  • 255
  • 2
  • 4
  • 15
  • You should check with isset or array_key_exists to avoid "undefined index", if you really want to ignore those notices (not recommended) you could specify error_level as warning, or use @ operator in front of the variable – Dan Ionescu Mar 04 '17 at 12:55
  • 1
    @DanIonescu Thank you for understanding my question. ! Thanks – sauce Mar 04 '17 at 12:56
  • You're wellcome, i've also posted an answer – Dan Ionescu Mar 04 '17 at 13:02
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – user3942918 Mar 04 '17 at 17:23

3 Answers3

0

Just change you condition if(isset($_POST['mytext']) && is_array($_POST['mytext']))

Shailesh Chauhan
  • 643
  • 5
  • 18
0

Replace if( isset($_POST['mytext']) && is_array($_POST['mytext']) ) with if ($_POST['mytext'])

Vineet1982
  • 7,552
  • 4
  • 29
  • 64
0

To avoid "undefined index" notices the right way you can use one of the following:

if (isset($_POST['mytext'])) {
    // your code
}

or

if (array_key_exists('mytext', $_POST)) {
    // your code
}

You could also choose to ignore them using the @ operator or by setting error_reporting above notice but that is not recommended.

Dan Ionescu
  • 2,694
  • 1
  • 11
  • 14