-1

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.

RezaM
  • 167
  • 1
  • 1
  • 11
  • 1
    So how do you expect us to find line 30? – u_mulder Dec 14 '14 at 15:56
  • @u_mulder My bad, I already put two * in front the lines but I will put it again in my first post. I'm sorry – RezaM Dec 14 '14 at 15:58
  • You have `
    ` wrapped around your ``. Are you using javascript or other library to modify the `
    – Sean Dec 14 '14 at 15:58
  • @Sean Yeah, I'm using the pre tag with editor because of Javascript. Didn't think that JS would modify/remove the name tag. Could you give me an example on how to use the save()? Thank you! – RezaM Dec 14 '14 at 16:01
  • I don't know what javascript library/plugin you are using, so I can't give an example. You can always verify with `print_r()` -> `if(isset($_POST['save'])) { print_r($_POST); }` to see if your `textarea` element is posted. – Sean Dec 14 '14 at 16:04
  • @Sean I'm using [Ace-editor](http://ace.c9.io/#nav=about) I've used var_dump and print_r and both give me the same undefined index error. – RezaM Dec 14 '14 at 16:06
  • I am not familiar with Ace-editor, but it looks like it is overwriting the contents of `
    `. Have you tried changing it to just `` instead of `
    `
    – Sean Dec 14 '14 at 16:16
  • If the `
    ` is needed, then on form submit you will need to recreate the `editor` element, and set its value to [`editor.getValue();`](http://ace.c9.io/#nav=howto).
    – Sean Dec 14 '14 at 16:21
  • @Sean Yes, I've already tried that and unfortunately that won't work either... Thank you for helping me! – RezaM Dec 14 '14 at 16:22
  • @Sean I've looked it up and it has to be indeed done by your way. But I can't make it work in my code, any chance you can help me also with that? – RezaM Dec 14 '14 at 16:41
  • I have posted an answer giving an idea of how to recreate the element, with its value, on form submit. It is untested as I don't have the Ace-editor plugin, but it should get you pointed in the right direction – Sean Dec 14 '14 at 16:48
  • You should check the provided file path/name, otherwise one could download/upload arbitrary files. – Gumbo Dec 14 '14 at 16:50

1 Answers1

1

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>
Sean
  • 12,443
  • 3
  • 29
  • 47
  • Thank you very much! If fixes the problem, but now it won't give the right styles etc. to editor:http://gyazo.com/2c65755e300fcd866d7cf6275d5a62b0 - Normally it would be this: http://gyazo.com/26425c6a7886489c86da24aecf58dfef – RezaM Dec 14 '14 at 16:59
  • The `...your other js...` is where your editor styles go. You never provided this code, so I could not guess what you had. – Sean Dec 14 '14 at 17:01
  • You're right, but the editor has no styles in it. But thank you, I will figure it out from here! – RezaM Dec 14 '14 at 17:02
  • It seems that your code will still not work? I've changed nothing to it and it won't work.. – RezaM Dec 14 '14 at 17:12
  • Can you add your javascript code to your original question. Without it I would just be guessing/speculating what the issue is. – Sean Dec 14 '14 at 17:18
  • As you requested, I've put it in the main post at the bottom. As you can see, it hasn't got many JS codes. That's it.. – RezaM Dec 14 '14 at 17:43
  • And do you have `
    ` OR ``?
    – Sean Dec 14 '14 at 17:46
  • So is your issue the editor styles OR in php `$_POST['editor']` still is undefined OR both? Not sure what your current issue is. – Sean Dec 14 '14 at 17:52