简体   繁体   English

Ajax在第一次尝试时不发送textarea(CKEditor)的值,但是在第二次尝试时发送

[英]Ajax doesn't send the value of a textarea (CKEditor) on the first try, but it sends it on the second try

I have a textarea in my HTML that loads CKEditor 4. Then I will send the data of my HTML form to a PHP file that processes the inputs. 我的HTML中有一个加载CKEditor 4的文本区域。然后,我会将HTML表单的数据发送到处理输入的PHP文件。

       $.ajax({
            xhr: function(){
                //blah blah blah
                return XHR;
            },
            type: "POST",
            url: "process.php",
            data: formData,
            processData: false,
            contentType: false,
            mimeType: "multipart/form-data",
            success: function (response) {
                //blah blah blah
            },
            error: function(XHR, textStatus, error){
               //blah blah blah
            }
        });

In the PHP file, I first sanitize all the inputs and then pass them to the database. 在PHP文件中,我首先清理所有输入,然后将它们传递到数据库。 It turns out that the value of the textarea in my page, which its name is "description", is not sent via ajax the first time that I press the submit button (I have verified this by writing the content of $_POST[] in a file on the server to facilitate the debugging process) but the second time I press the submit button on my HTML form, the value of the description textarea is sent with no issue! 事实证明,我第一次按下“提交”按钮时,不是通过ajax发送页面中textarea的值(其名称为“描述”)(我已通过将$ _POST []的内容写入服务器上的一个文件,以方便调试过程),但是第二次我按HTML表单上的Submit按钮时,说明textarea的值没有问题!

I first assumed that it can be due to the fact that the value of the description textarea contains HTML codes and it is sanitized by PHP. 我首先假设这可能是由于描述textarea的值包含HTML代码并且被PHP清除了。 But I removed the sanitation thing for it and the problem persisted. 但是我为此删除了卫生用品,问题仍然存在。 I have written thousands of lines of code so far, but I won't mind revealing more codes if need be. 到目前为止,我已经编写了数千行代码,但是如果需要的话,我不介意透露更多代码。 I just avoided to do so to prevent this post from becoming unnecessarily long. 我只是避免这样做,以防止帖子过长。

Any help is appreciated. 任何帮助表示赞赏。 Thanks in advance. 提前致谢。

Edit This is how I fill the formData variable: 编辑这是我填写formData变量的方式:

   $("#form").submit(function (event) {
        event.preventDefault();
        var formData = new FormData(this);
        loading_start();
        submitForm(formData);
    });

And I need to upload files simultaneously during the submission of this form. 在提交此表单期间,我需要同时上传文件。

如果不需要将文件上传到服务器,请尝试使用.val()函数获取textarea内容。

I found the issue causing this weird behavior. 我发现了导致这种奇怪行为的问题。 For future reference, CKEditor needs to be updated before you can submit the value of the textarea that holds its data. 为了将来参考,需要先更新CKEditor,然后才能提交保存其数据的textarea的值。 To force CKEditor to update itself, you can use the following code: 要强制CKEditor进行自我更新,可以使用以下代码:

   for(var instanceName in CKEDITOR.instances)
            CKEDITOR.instances[instanceName].updateElement();

The credit for the above code goes to the user who has posted it here . 以上代码的功劳归功于已在此处发布代码的用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React - 第一个文件上传到 Spring API 不起作用,再试一次,第二次尝试正常吗? - React - 1st file upload to Spring API doesn't work, try again and it works fine on second try? CKEDITOR在首次提交时不通过ajax提交数据 - CKEDITOR doesn't submit data via ajax on first submit Canvas toDataURL在首次尝试时不会返回整个URL - Canvas toDataURL doesn't return whole URL on first try 向元素添加数据属性在第一次尝试时不起作用 - Adding data attribute to element doesn't work on first try 更改时不触发JavaScript选择 - Javascript select on change doesn't fire first try Highcharts oncahnge 事件它在第一次尝试时有效,但在第二次尝试时无效 - Highcharts oncahnge event It works on the first try but not on the second try React setState 在第一次尝试时不起作用,但在第二次尝试时有效? - React setState not working on first try, but works on second? Firestore设置在第一次尝试时失败,但在第二次尝试时有效 - Firestore set fails on first try but works on second 使用 CKEDITOR 会隐藏默认的 TEXTAREA 并且不显示 CKEDITOR - Using CKEDITOR hides the default TEXTAREA and doesn't display CKEDITOR 发布动态更改的textarea值不适用于Ajax表单 - Posting a dynamically changed textarea value doesn't work with ajax forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM