Reputation: 850
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...
Upvotes: 2
Views: 2709
Reputation: 850
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
Upvotes: 1