0

I have a editor embeded in the html page

<div id="editor">
Problem Statement goes here&hellip;
</div>

I basically want to store the contents written in editor to a file(preferably in rich text format). I used a script (given below) for storing the contents in a string.(This I used by referring to HTML div text to save and display )

<script type='text/javascript' src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> 
     var StoreEditorContent;  //declare a variable to save Content

      document.getElementById('save').addEventListener("click", SaveText);  // adding event listner for onclick for Saving text

    function SaveText(){

       StoreEditorContent = document.getElementById('editor').innerHTML; // Save the Content into 
       document.getElementById('editor').innerHTML = ""; // to blank the editor Content
       window.onload = function(){

                $.ajax({
                    url: "submit_request.php",
                    type: "GET",
                    success: function writeMsg(StoreEditorContent){
                       //Not sure if this part is right.
                    }
                });   
        }
    }

    </script>

This of course is storing in the contents in a string StoreEditorContent. I now want to pass this string to a php function which will write this(StoreEditorContent) to a file.The php file that contains function to write is given below

<?php

function writeMsg($msg){
    $file = "myfile.txt";
    file_put_contents($file, $msg);
}
?>

I know I need to use ajax here, but cannot figure out how? Any help appreciated.

1
  • The success callback function in the ajax call is for what you want to do on the frontend if the request succeeds. If you don't need to do anything, then just don't include it. In your case, it would make sense to put the piece where you empty the editor inside of here because you only want to empty the editor when your request has succeeded. Otherwise, the user loses all the contents even if the request fails. Commented Oct 23, 2014 at 4:13

1 Answer 1

2

You need to provide a data: option to $.ajax():

$.ajax({
    url: "submit_request.php",
    type: "POST",
    data: { content: StoreEditorContent },
    success: function (response) {
        // Do something with the response sent from PHP
    }
});

Then your PHP can do:

writeMsg($_POST['content']);

You should use POST rather than GET for large requests because the limit on GET parameters is relatively small.

Sign up to request clarification or add additional context in comments.

2 Comments

Should this be a post request rather than a get request?
@stefvhuynh Yes it should mate.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.