未定义索引:第6行的C:\ xampp \ htdocs \ Monitor \ edit_index.php中的内容

问题描述 投票:0回答:2

我的php脚本有问题。 php非常新,所以我知道它不是最好的。正在收到此错误

注意:未定义索引:内容在C:\ xampp \ htdocs \ Monitor \ AddNewUser.php在线207

这是我的代码

<section class="sign-in">
            <div class="container">
            <?php
                    if (isset($_POST['submit']))
                    {
                        $fn = "hi.txt"; 
                        $content = stripslashes($_POST['content']); 
                        $fp = fopen($fn,"w") or die ("Error opening file in write mode!"); 
                        fputs($fp,$content); 
                        fclose($fp) or die ("Error closing file!"); 

                        echo '<script type="text/javascript">';
                        echo 'swal("'.$fn.'", "updated", "success");';
                        echo '</script>';
                    }

                    else {
                        echo "<form action=\"edit_index.php\" method=\"post\"> 
                        <textarea rows=\"15\" cols=\"50\" name=\"content\">";
                        $fn = "hi.txt"; 
                        print htmlspecialchars(implode("",file($fn))); 
                        echo "</textarea><br> 
                        <input type=\"submit\" value=\"UPDATE!\" name=\"submit\"> 
                        </form> 
                        ";
                        }
                    ?>
                </div>
                </div>
            </div>
        </section>
php textarea
2个回答
0
投票

因此,由于您使用了isset($_POST['submit']),但仍收到此错误,这意味着您正在接收POST数据,但其中不包含content。检查您要从中发送数据的表格(<form>)。 content的拼写错误,或者您忘记为其添加name属性。


0
投票

Undefined index: content in ...仅表示您正在尝试访问不存在的数组索引,例如,我们有以下数组:

$arr = [ 'foo' => 1, 'bar' => 2];

如果写$arr['blah'],将会收到错误Undefined index: blah in ...

现在$_POST$是服务器定义的全局数组,它为您提供已发布的数据,因此错误表示HTML表单在发送content值时没有向您发送submit(因为代码进入了if部分。)这可能是因为您的HTML表单没有任何带有名称内容的<input><textarea>等标记,所以应该有类似您表单中的<input name = 'content' ...>,可能会丢失。

© www.soinside.com 2019 - 2024. All rights reserved.