我希望使用一些Ajax 保存textarea的值。
JS:
function saveCommentary(id)
{
commentary = document.getElementById('commentaire' + id).value.replace(/\n/g, '<br />');
$.get(Routing.generate('ajax_savecommentary', { id:id, commentary:commentary }),
function(response)
{
alert('commentary');
alert(commentary);
}, "json");
}
我添加了替换跳跃来解决我的问题,但是没有。 当我使用 只有一行时,一切正常 ,这就是为什么我试图替换&#34; \ n&#34;与&#34;&#34;但仍然没有。
PHP:
public function saveCommentaryAction($id, $commentary)
{
if (!$this->get('session')->get('compte'))
return $this->redirect($this->generateUrl('accueil'));
$request = $this->container->get('request_stack')->getCurrentRequest();
$isAjax = $request->isXMLHttpRequest();
if ($isAjax)
{
$information = $this->getDoctrine()->getManager()->getRepository('CommonBundle:Information')->find($id);
$information->setCommentaire(nl2br($commentary));
$this->getDoctrine()->getManager()->flush();
$response = array("code" => 100, "success" => true, 'commentary' => $commentary);
return new Response(json_encode($response));
}
$response = array("code" => 0, "success" => false, 'commentary' => $commentary);
return new Response(json_encode($response));
}