(symfony2)如何使用ajax持久化实体

时间:2016-08-30 14:07:24

标签: javascript php jquery ajax symfony

我正在编写一个简单的论坛,我希望用户在不刷新页面的情况下添加消息,我不知道ajax,我试图让事情有效但当我点击提交按钮时没有任何反应,任何帮助请 ??  这是我的cotroller:

 $request = $this->get('request');
    $entityManager = $this->getDoctrine()->getManager();  
    $msga = new Msg();
    $form = $this->get('form.factory')->create(new MsgType());

    $form->handleRequest($request);
    if ($form->isValid())
{
    if($request->isXmlHttpRequest())
    {

            $msga->setContenu($form->get('contenu')->getData());

            $msga->setDateEcriture($date);
            $msga->setNbvote(0);
            $msga->setUser($user);
            $msga->setForum($forum);

            $entityManager->persist($msga);
            $entityManager->flush();    

    return $this->render('otaotaBundle:Forum:forum.html.twig', array(
        'forum'=>$forum,
        'msg'=>$msg,
        'msgUF' =>$msgByUser,
        'id' => $forum->getIdForum(),
        'form'      => $form->createView(),
        'action'    => "add",

            )); 

        }
    }

和视图:

{% extends "::base.html.twig" %}
{% block body %} 
<form action="{{path("show_forum", {id: id} )}}" method="post" novalidate>
                    
                    
                    Date de création :
                    <h7>{{forum.datedebforum|date("Y-m-d")}}</h7><br/><br/>
                    {{forum.description}}<br/><br/><br/>
                    
        <table>
            {% for m in msg %}
                <tr>
                    <td>
                    {{m.dateEcriture|date("Y-m-d")}}<br/>
                    {{m.nbVote}}
                    {{m.contenu}}
                    <br/><br/>
                    </td>
                     
                    <td>
                        {% for ms in msgUF %}
                            {%if ms.idMsg == m.idMsg %}
                        <a class="link" href="{{ path('delete_msg',{id: ms.idMsg}) }}">supprimer</a>
                            {% endif %}
                        {% endfor %}
                    </td>
                                         
                </tr>
            {% endfor %}
        </table>
        
        <br/>
        
        
        {% if is_granted('IS_AUTHENTICATED_REMEMBERED') %}
        
        {{ form_widget(form._token) }}
        <textarea name="textarea" id="contenu"></textarea>
        <input  type="submit" value="Repondre"  />
        
    
        
        <script>
            $(document).ready(function() {
                $('form').submit(function(event) {
                    // prevents the browser from "following" the link
                    event.preventDefault();

                    var $url = $("form").attr("action");// + '.json';
                    var $value = $('#contenu').val();

                    $.ajax({
                        type: "POST",
                        url: $url,
                        data: $value
                        
                    });
                });
            });
        </script>
        
        {% endif %}
        
        
</form>
{%endblock%}

i want that the message shows up after the submitting without refreshing

0 个答案:

没有答案