26

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; ?>">
4
  • D:\wamp\www\update.php, Are you accessing page this way or it just a path to the file? Have you tried it on localhost? Commented Dec 31, 2012 at 5:56
  • Notice: Undefined index: filename in D:\wamp\www\update.php on line 4, obviously he's accessing it through localhost or he wouldn't get that error. Commented Dec 31, 2012 at 5:59
  • 1
    have u added method="post" in form? Commented Dec 31, 2012 at 6:00
  • We can't help you anymore OP, we will get no where just guessing. Please post the entire code. Commented Dec 31, 2012 at 6:01

10 Answers 10

48

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

Sign up to request clarification or add additional context in comments.

7 Comments

It depends at what stage of the script he wants to echo it, he can do that also if he wishes.. but then why assign it to $filename at all :P It was really just to demonstrate the use of isset
I personally would just do echo $_POST['filename'];
You're missing the point though. It doesn't matter where you use isset(), it can always be the same as isset(GET FILENAME) and isset(filename)
Its a shame i expected his form to be complete not as it was shown, thought he was just showing a snippet ! Good spot with that!
Why is this the best answer? It's wrong in almost every aspect.
|
7
if(isset($_POST['form_field_name'])) {
    $variable_name = $_POST['form_field_name'];
}

Comments

5

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>

6 Comments

Yes, it's enclosed within the form field. I didn't include above, that's all.
@TingPing I dont find any other issues with your code. Is that your complete code
That's a very key thing to include in your post, Ting. What happens when you var_dump($_POST) ?
var_export($_REQUEST) with var_dump($_POST) could be checked.
var_export isn't really needed, var_dump would suffice. var_export just prints out valid PHP code.
|
2

short way, you can use Ternary Operators

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

Comments

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

echo $filename;
}

Comments

1

Please try this

error_reporting = E_ALL & ~E_NOTICE

in php.ini

1 Comment

that would have been a comment
0

Simply

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

Comments

0

use isset for this purpose

<?php

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

?>

Comments

0

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

Comments

-1
enctype="multipart/form-data"

Check your enctype in the form before submitting

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.