'JQuery content editable div and text box on submit not sending POST

I have a website where you can edit and format text, then you can save it on to the server. I am using jquery to send the data to a PHP page where it will be saved. My site won't send the name of the file and the formatted text to PHP.

Here is my html code:

<div id="editor" contenteditable="true">
    This is the editable text.
</div>

<input type="text" id="name" placeholder="File Name">
<input type="submit" id="save" value="Save">

<script>
    $(document).ready(function() {
        $("#save").on("click", function(event) {
            var formData = {text: $("#editor").html(), name: $("#name").val()};
            event.preventDefault();
            $.ajax({
                url: 'freewordsave.php',
                type:'POST',
                data: formData,
                success: function(msg)
                {
                    alert('Your file was saved!');
                }
            });
        });
    });
</script>

Here is also my PHP code:

$name = $_POST['name'];
$text = $_POST['data'];
$file = fopen("./location/" . $name . ".html", "w") or die("<script> alert('Error'); </script>");
fwrite($file, $text);
fclose($file);

My code won't even bring up the alert in javascript.

Thank you.



Solution 1:[1]

I fixed my code after Simone Rossaini posted a comment. I forget a } closing bracket at the end of formData.

Fixed JQuery Code.

$(document).ready(function() {
    $("#save").on("click", function(event) {
        var formData = {text: $("#editor").html(), name: $("#name").val()};
        event.preventDefault();
        $.ajax({
            url: 'freewordsave.php',
            type:'POST',
            data: formData,
            success: function(msg)
            {
                alert('Your file was saved!');
            }
        });
    });
});

I know my PHP said $_POST['data'] but that was a mistake I made when I asked my question it wasn't actually like that in my PHP file.

Thank you

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Crann Moroney