Ting Ping
Ting Ping

Reputation: 1145

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; ?>">

Upvotes: 26

Views: 416418

Answers (10)

Aswita Hidayat
Aswita Hidayat

Reputation: 159

short way, you can use Ternary Operators

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

Upvotes: 2

Goki
Goki

Reputation: 1

enctype="multipart/form-data"

Check your enctype in the form before submitting

Upvotes: -1

David Harris
David Harris

Reputation: 2707

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>

Upvotes: 5

Rabby shah
Rabby shah

Reputation: 113

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

Upvotes: 7

Piseth Sok
Piseth Sok

Reputation: 1819

Please try this

error_reporting = E_ALL & ~E_NOTICE

in php.ini

Upvotes: 1

Danish Iqbal
Danish Iqbal

Reputation: 29

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"]!="")

Upvotes: 0

Sir
Sir

Reputation: 8280

Assuming you only copy/pasted the relevant code and your form includes <form method="POST">


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

If _POST is not set the filename variable won't be either in the above example.

An alternative way:

$filename = false;
if(isset($_POST['filename'])){
    $filename = $_POST['filename'];
 } 
    echo $filename; //guarenteed to be set so isset not needed

In this example filename is set regardless of the situation with _POST. This should demonstrate the use of isset nicely.

More information here: http://php.net/manual/en/function.isset.php

Upvotes: 48

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

use isset for this purpose

<?php

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

?>

Upvotes: 0

open source guy
open source guy

Reputation: 2787

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

echo $filename;
}

Upvotes: 1

ravi404
ravi404

Reputation: 7309

Simply

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

Upvotes: 0

Related Questions