[php] How to fix 'Notice: Undefined index:' in PHP form action

I received the following error message when I tried to submit the content to my form. How may I fix it?

Notice: Undefined index: filename in D:\wamp\www\update.php on line 4

Example Update.php code:

<?php

    $index = 1;
    $filename = $_POST['filename'];

    echo $filename;
?>

And $_POST['filename'] comes from another page:

<?php
    $db = substr($string[0],14) . "_" . substr($string[1],14) . "_db.txt";
?>

<input type="hidden" name="filename" value="<?php echo $db; ?>">

This question is related to php forms post

The answer is


use isset for this purpose

<?php

 $index = 1;
 if(isset($_POST['filename'])) {
     $filename = $_POST['filename'];
     echo $filename;
 }

?>


enctype="multipart/form-data"

Check your enctype in the form before submitting


Change $_POST to $_FILES and make sure your enctype is "multipart/form-data"

Is your input field actually in a form?

<form method="POST" action="update.php">
    <input type="hidden" name="filename" value="test" />
</form>

Please try this

error_reporting = E_ALL & ~E_NOTICE

in php.ini


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

echo $filename;
}

if(isset($_POST['form_field_name'])) {
    $variable_name = $_POST['form_field_name'];
}

Simply

if(isset($_POST['filename'])){
 $filename = $_POST['filename'];
 echo $filename;
}
else{
 echo "POST filename is not assigned";
}

short way, you can use Ternary Operators

$filename = !empty($_POST['filename'])?$_POST['filename']:'-';

Use empty() to check if it is available. Try with -

will generate the error if host is not present here

if(!empty($_GET["host"]))
if($_GET["host"]!="")

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to forms

How do I hide the PHP explode delimiter from submitted form results? React - clearing an input value after form submit How to prevent page from reloading after form submit - JQuery Input type number "only numeric value" validation Redirecting to a page after submitting form in HTML Clearing input in vuejs form Cleanest way to reset forms Reactjs - Form input validation No value accessor for form control TypeScript-'s Angular Framework Error - "There is no directive with exportAs set to ngForm"

Examples related to post

How to post query parameters with Axios? How can I add raw data body to an axios request? HTTP POST with Json on Body - Flutter/Dart How do I POST XML data to a webservice with Postman? How to set header and options in axios? Redirecting to a page after submitting form in HTML How to post raw body data with curl? How do I make a https post in Node Js without any third party module? How to convert an object to JSON correctly in Angular 2 with TypeScript Postman: How to make multiple requests at the same time