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...