如何在PHP表单操作中修复“通知:未定义的索引:" [英] How to fix 'Notice: Undefined index:' in PHP form action

查看:57
本文介绍了如何在PHP表单操作中修复“通知:未定义的索引:"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将内容提交到表单时,我收到以下错误消息.我该如何解决?

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

注意:未定义的索引:第4行D:\ wamp \ www \ update.php中的文件名

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

示例Update.php代码:

Example Update.php code:

<?php

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

    echo $filename;
?>

$ _ POST ['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; ?>">

推荐答案

Assuming您仅复制/粘贴了相关代码,并且表单中包含<form method="POST">

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;
}

如果未设置_POST,则在上面的示例中,filename变量也不会.

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

另一种方法:

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

在此示例中,无论_POST处于何种情况,都设置了文件名.这应该很好地展示了isset的用法.

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

更多信息在这里: http://php.net/manual/en/function.isset.php

这篇关于如何在PHP表单操作中修复“通知:未定义的索引:"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆