dq_1984 2014-12-14 15:52
浏览 30
已采纳

未定义的索引不对[重复]

I'm having problems making a online code editor, the current problem is that I made a text-area where the code from the file is gonna be. After I press the save button it needs to upload it, but I'm getting the well known error:

Notice: Undefined index: editor in /home/....../....../.../../ on line 30.

My code:

    ### FTP settings ###
$fileAdresRoot = $_SERVER['DOCUMENT_ROOT'];

        if(empty($_GET['name']))
        {
            header('Location: ftp-directory');
            exit();
        }
        else
        {
            $fileName   = trim($_GET['name']);
            $fileAdres  = $fileAdresRoot.'/'.$fileName.''; 
        }
            if(isset($_GET['doublename']))
            {
                $fileMaps   = trim($_GET['doublename']);
                $fileAdres  = $fileAdresRoot.'/'.$fileMaps.'/'.$fileName.'';
            }
        $fileContents   = fopen($fileAdres, 'rb', false);
        $fileContent    = stream_get_contents($fileContents);
### FTP settings ###

/* Saving the file */
if(isset($_POST['save']))
{
    **$textEditor   = trim($_POST['editor']);**
    $permisFile = chmod($fileAdres, 0777);


            **file_put_contents($fileAdres, $textEditor);**

    # Notify    
    $sGoed = 'U heeft succesvol het bestand opgeslagen!';
}
/* Code in between here.. */
<form method="post">
                    <p>U kunt terug naar de public_html map door <a href="ftp-directory">hierop</a> te klikken.</p>
                    <div class="pull-right"><strong>Huidige map: <?php echo $fileAdres; ?></strong></div>
                        <br />
                        <hr />
                        <br />
                    <pre id="editor"><textarea name="editor"><?php echo htmlentities($fileContent); ?></textarea></pre>
                        <hr />
                        <button type="submit" name="save" class="btn btn-primary">Wijziging opslaan</button>
                        <button type="submit" name="delete" class="btn btn-purple">Verwijderen</button>
                    </form>

Line 30:$textEditor = trim($_POST['editor']);

As requested: My JS:

    <script src="ace/src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
<script>
    var editor = ace.edit("editor");
    editor.setTheme("ace/theme/chrome");
    editor.getSession().setMode("ace/mode/php");

    document.forms[0].onsubmit = function(){
        var theForm = document.forms[0];
        var newTextarea = document.createElement("textarea");  
        newTextarea.name = "editor";
        newTextarea.value = editor.getValue();
        theForm.appendChild(newOption); 
    }
</script>

As you can see, I've got no clue why the error is coming up. I've searched on Google but this isn't the normal type of problem with this error I think. Thank you in advance for helping! English isn't also my mother tongue, sorry for any grammar/spell mistakes.

</div>
  • 写回答

1条回答 默认 最新

  • dpbv85276 2014-12-14 16:46
    关注

    Due to using the Ace-editor, the contents of <pre id="editor"></pre>, namely <textarea name="editor"></textarea>, are overwritten/modified, so your element is no longer in the DOM. You will need to recreate the editor element on form submit, something like -

    <script>
        var editor = ace.edit("editor");
    
        ...
        your other js
        ...
    
        document.forms[0].onsubmit = function(){
            var theForm = document.forms[0];
            var newTextarea = document.createElement("textarea");  
            newTextarea.name = "editor";
            newTextarea.value = editor.getValue();
            theForm.appendChild(newOption); 
        }
    </script>
    

    or if you are using the jQuery library

    <script>
        var editor = ace.edit("editor");
    
        ...
        your other js
        ...
    
        $(function(){
            $('form').on('submit',function(){
                $('<textarea>').attr({
                    name: 'editor',
                    value: editor.getValue()
                }).appendTo($(this)); 
            });
        });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置