Reputation: 1145
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
Reputation: 159
short way, you can use Ternary Operators
$filename = !empty($_POST['filename'])?$_POST['filename']:'-';
Upvotes: 2
Reputation: 1
enctype="multipart/form-data"
Check your enctype in the form before submitting
Upvotes: -1
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
Reputation: 113
if(isset($_POST['form_field_name'])) {
$variable_name = $_POST['form_field_name'];
}
Upvotes: 7
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
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
Reputation: 30488
use isset
for this purpose
<?php
$index = 1;
if(isset($_POST['filename'])) {
$filename = $_POST['filename'];
echo $filename;
}
?>
Upvotes: 0
Reputation: 2787
if(!empty($_POST['filename'])){
$filename = $_POST['filename'];
echo $filename;
}
Upvotes: 1
Reputation: 7309
Simply
if(isset($_POST['filename'])){
$filename = $_POST['filename'];
echo $filename;
}
else{
echo "POST filename is not assigned";
}
Upvotes: 0