简体   繁体   English

如何将表单数据发送到远程URL并以Bootstrap模式显示结果?

[英]How to send form data to remote URL and the result display in Bootstrap modal?

I have a CakePHP form and I want to allow users to display a preview. 我有一个CakePHP表单,我想允许用户显示预览。 So there is a preview submit button #btn_submit_preview and when user clicks the button, data from the form are serialized and sent to my controller. 因此,有一个预览提交按钮#btn_submit_preview ,当用户单击该按钮时,表单中的数据将被序列化并发送到我的控制器。 This controller saves the data and return HTML view output with preview: 该控制器保存数据并返回带有预览的HTML视图输出:

 $('#btn_submit_preview').click(function (event) {
    var formData = $("#OfferAddForm").serialize();
    $.ajax({
        type: "POST",
        url: "/offers/preview/",
        data: formData,
        success: function (data) {
            $('#content').html(data);
            $('#myModal').modal('show');
        }
    });
    event.preventDefault();
    return false;
});

After data are passed back to the page I need to display them in a Boostrap modal window. 将数据传递回页面后,我需要在Boostrap模态窗口中显示它们。 I have these HTML source code for modal. 我有这些HTML模态源代码。

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content" id="content">
        </div>
    </div>
</div>

Question: is the way how I open the "preview" correct or should I do something else? 问题:打开“预览”的方式是否正确?还是应该做其他事情? I am quite sure about assigning returned data back to modal window - $('#content').html(data); 我很确定要将返回的数据分配回模式窗口- $('#content').html(data); maybe I should return all HTML source for modal window with preview data and open it only...? 也许我应该返回带有预览数据的模态窗口的所有HTML源,并仅将其打开...?

I think what you have now is perfectly fine. 我认为您现在拥有的一切都很好。 I wouldn't return the HTML source for the entire modal, I'd just grab the HTML for the email and display it on my modal, this helps to achieve a better separation of concerns IMO. 我不会返回整个模式的HTML源代码,而只是获取电子邮件的HTML并将其显示在我的模式上,这有助于实现IMO更好的关注点分离。

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

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