简体   繁体   English

使用 AJAX MVC PHP 发送内容 tinyMCE

[英]Send content tinyMCE using AJAX MVC PHP

I am trying to post a array that has the elements of an email like subject and message.我正在尝试发布一个包含主题和消息等电子邮件元素的数组。 I am using tinyMCE on the subject part and now that is the only part that is not getting send through.我在主题部分使用了 tinyMCE,现在这是唯一没有通过的部分。 All the other POST variables do have something in them.所有其他 POST 变量都包含一些内容。

Script:脚本:

        $('#message').html(tinymce.get('#message').getContent());
        $.ajax({
            type: "POST",
            data: $('#contact-form, #message').serializeArray(),
            dataType: 'JSON',
            url: $('#url').val() + 'Ajax/ajax',
            success: function ($data) {
                console.log($data);
                console.log("Email verzonden!");
            },
            error: function (jqXHR, exception) {
                alert("ajaxerror: " + jqXHR.responseText);
            }
        });
    });

Form:形式:

<section class="mb-4">
    <div class="row">
        <div class="col-md-9 mb-md-0 mb-5">
            <form id="contact-form" name="contact-form">
                <div class="row">
                    <div class="col-md-12">
                        <div class="md-form mb-0">
                            <input type="text" id="reciever" name="reciever" class="form-control" placeholder="Aan *" required>
                        </div>
                        <div class="md-form mb-0">
                            <input type="text" id="cc" name="cc" class="form-control" placeholder="CC">
                        </div>
                        <div class="md-form mb-0">
                            <input type="text" id="bcc" name="bcc" class="form-control" placeholder="BCC">
                        </div>
                        <div class="md-form mb-0">
                            <input type="text" id="subject" name="subject" class="form-control" placeholder="Onderwerp *" required>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-12">
                        <div class="md-form">
                            <textarea type="text" id="message" name="message" rows="2" class="form-control md-textarea" placeholder="Bericht *"></textarea>
                        </div>
                    </div>
                </div>
                <div class="text-center text-md-left">
                    <input type="hidden" name="action" value="GCSSendMail">
                    <input type="submit" id="GCSSendMail" class="btn btn-primary AjaxCall" value="Submit">
                </div>
                <div class="status"></div>
            </form>
        </div>
    </div>
</section>
<a href="<?php echo BASE_URL ?>Mail/mail">Go mail</a>

Ajaxcontroller Ajax控制器

function action()
{
    if (isset($_POST['action'])) {
        switch ($_POST['action']) {
            case "SendMail":
                $mailController = new MailController;
                $result = $mailController->sendMail($_POST['reciever'], $_POST["subject"], $_POST["message"]);
                return $result;
                break;
         }
    }
}

I have some extra files connected to it like a dispatcher and a controller but that has no influence on the content not getting to the ajax script.我有一些额外的文件连接到它,如调度程序和控制器,但这对没有到达 ajax 脚本的内容没有影响。

When TinyMCE is loaded on the page the original <textarea> is no longer visible.在页面上加载 TinyMCE 时,原始<textarea>不再可见。 TinyMCE uses an iframe for the content editing area in place of the <textarea> . TinyMCE 使用 iframe 作为内容编辑区域来代替<textarea> If you use JavaScript code to send the form's content the underlying <textarea> will not contain any content as you are not actually typing into the <textarea> .如果您使用 JavaScript 代码发送表单的内容,则底层<textarea>将不包含任何内容,因为您实际上并未在<textarea>键入内容。 You can use the triggerSave() method on the tinymce object to have TinyMCE update the <textarea> with the current contents of the editor.您可以在tinymce对象上使用triggerSave()方法让 TinyMCE 使用编辑器的当前内容更新<textarea> You should do this right before you start your AJAX POST process.您应该在开始 AJAX POST 过程之前执行此操作。

Instead of代替

$('#message').html(tinymce.get('#message').getContent());

...you want to use... ……你想用……

tinymce.triggerSave();

You made a silly mistake.你犯了一个愚蠢的错误。 You are not supplying the URL to the AJAX request.您没有提供 AJAX 请求的 URL。 Look at line看线

url: $('#url').val() + 'Ajax/ajax',

I could not find url ID in html form.我找不到 html 形式的url ID。 Add a fully qulified URL to AJAX's url attribute.将完全限定的 URL 添加到 AJAX 的 url 属性。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM