J查询内容可编辑的div和提交时的文本框不发送POST

我有一个网站,您可以在其中编辑和格式化文本,然后将其保存到服务器上。我正在使用jquery将数据发送到PHP页面,在那里它将被保存。我的网站不会将文件名和格式化文本发送到 PHP。


这是我的网页代码:


<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>

这也是我的PHP代码:


$name = $_POST['name'];

$text = $_POST['data'];

$file = fopen("./location/" . $name . ".html", "w") or die("<script> alert('Error'); </script>");

fwrite($file, $text);

fclose($file);

我的代码甚至不会在 java 脚本中显示警报。


心有法竹
浏览 83回答 1
1回答

翻阅古今

在西蒙娜·罗塞尼发表评论后,我修复了我的代码。我忘记了表单数据末尾的} 右括号。修复了 J 查询代码。$(document).ready(function() {&nbsp; &nbsp; $("#save").on("click", function(event) {&nbsp; &nbsp; &nbsp; &nbsp; var formData = {text: $("#editor").html(), name: $("#name").val()};&nbsp; &nbsp; &nbsp; &nbsp; event.preventDefault();&nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: 'freewordsave.php',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type:'POST',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: formData,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function(msg)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert('Your file was saved!');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });});我知道我的PHP说,但这是我在问我的问题时犯的一个错误,在我的PHP文件中实际上并不是这样。$_POST['data']
打开App,查看更多内容
随时随地看视频慕课网APP