dppn67180 2015-03-08 18:14
浏览 51
已采纳

Symfony2如何使用doctrine更新生成Ajax请求

I am developing a interface for edit page of my website. CKeditor is used. I want use Ajax method to save the modification. My text is stock in DB. I use Doctrine to access to him.

script jQuery

$(".loading").hide();

$('[name="contentsForm"]').submit(function() {
    $('[name="contentsForm"]').hide();
    $("#contents .loading").show();
    $.ajax({
        type: "POST",
        url: "{{ path('AdminAjaxEditText', {'page' : 'index', 'description' : 'contents'})}}",
        data: "{'data': '" + $('#contentsForm_data').val() + "'}",
        cache: false,
        success: function(data){
            $('[name="contentsForm_data"]').html(data);
            CKupdate();
            $('[name="contentsForm"]').show();
            $("#contents .loading").hide();
        }
    });
    return false;
});

Controller php

public function ajaxEditAction($page, $description, Request $request) {

    $em = $this->getDoctrine()->getManager();

    $text = $em->getRepository("FDMWebsiteBundle:Text")->find(array("page" => $page, "description" => $description));
    $form = $this->get('form.factory')->createNamed($description."Form", new TextType(), $text);

    if ($form->handleRequest($request)->isValid()) {
        $em->flush();
    }

    return $this->render("Bundle:Admin:textForm.html.twig", array(
        "form" => $form->createView()
        ));
}

Template twig

{{ form_start(form, {'action': ''}, {'method': 'POST'}) }}
{{ form_errors(form) }}
{{ form_widget(form.data, { 'attr': {'class': 'ckeditor'} }) }}
<input type="submit" class="saveTextBtn btn btn-primary pull-right" value="Enregistrer"/>
<div class="clearfix"></div>
{{ form_rest(form) }}
{{ form_end(form) }}

My problem is when is click on save the Ajax run but nothing is saved in my DB ?

What wrong I have done ?

I don't understand why my code is not flush but is displayed in textarea after the Ajax request.

Sorry for my English, I am learning it...

  • 写回答

1条回答 默认 最新

  • douwei9973 2015-03-10 18:35
    关注

    I have found my problems

    First I need to set the charset to utf-8 and get data form CKEditor not form the textarea

    scriptCharset: "utf-8",
    data: "data=" + CKEDITOR.instances['contentsForm_data'].getData()
    

    http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#getData

    Second : My ajax controller need to use getter's request. The handleRequest method of form isn't working, I don't knonw why. If someone can give me a explain I am looking for it.

    if($request->isXmlHttpRequest()) {
        $data = $request->request->get('data');
        $text->setData($data);
        $em->flush();
    }
    

    Tutorial and some post use this solution but I don't understand why handlerResquest fail ?

    Third: I need to configure CKEDITOR to don't use entities

    CKEDITOR.config.entities = false;
    

    http://docs.ckeditor.com/#!/api/CKEDITOR.config-cfg-entities

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 matlab appdesigner私有函数嵌套整合
  • ¥15 给我一个openharmony跑通webrtc实现视频会议的简单demo项目,sdk为12
  • ¥15 vb6.0使用jmail接收smtp邮件并另存附件到D盘
  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题