-1

i have a problem when i click to post button i want data appeared in my page without refreshing page and add to that i want to store those data on my db and the next time i visit this page i find those data

    <form method="post">
                    <textarea id="txt" class="form-control input-lg no-border" rows="2"
                              placeholder="What are you doing?..."></textarea>
        <script>
            $(document).ready(function() {
                $("#btnpost").click(function () {
                    var text = $('#txt').val();
                    $.ajax({
                        type: 'POST',
                        url: '{{ path('group_addpub') }}',
                        data: {desc: text}
                    });
                    $("#publication").prepend('<div class="panel panel-success rounded shadow" style="text-align: left;margin-bottom: 5px;">' +
                            '<div class="panel-heading no-border">'+
                            '<div class="pull-left half">'+
                            '<div class="media" style="text-align: left;">'+
                            '<div class="media-object pull-left" style="margin-top: 35px;">'+
                            '<img src="http://bootdey.com/img/Content/avatar/avatar2.png" style="width: 40px;height: 40px;">' +
                            '</div>'+
                            '</div>'+
                            '</div>'+
                            '<a href="">test profile</a>'+
                            '<span class="text-white h6" style="display: block; color: black;">on 8th June, 2014</span>'+
                            '<br>'+
                            '<span style="color: black;margin-bottom: 10px;word-break: break-all  ">'+text+ '</span>'+


                            '</div>'+
                            '</div>'+
                            '<div class="panel-footer">'+

                            '<form action="#" class="form-horizontal">'+
                            '<div class="form-group has-feedback no-margin">'+
                            '<div style="text-align: right;margin-top: 32px;">'+
                            '<a href=""><img src="{{ asset('Groupe/img/like-icon.png') }}" style="width:5%;"></a>'+
                            '<a href="" ><img src="{{ asset('Groupe/img/Unlike-icon.png') }}" style="width:5%;"></a>'+
                            '</div>'+
                            '<input class="form-control" type="text" placeholder="Votre commentaire ici..." style="width: 95%;margin-left: 10px;">'+
                            '</div>'+
                            '</form>'+
                            '</div>');


                    $('#txt').val('');

                });
            });
        </script>

    </form>

this is my controller action

   public function addpubicationAction(Request $request)
{

    if ($request->isXmlHttpRequest() && $request->isMethod('post') ) {
        $em = $this->getDoctrine()->getManager();
        $des = $request->get('txt');

        $publication = new Publication();
        $publication->setDescription($des);
        $em->persist($publication);
        $em->flush();
    }



}

this is my routing file

  group_group_photos:
path:     /photos
defaults: { _controller: GroupGroupBundle:Group:photosGroupe}

  group_new:
path:     /{id}
defaults: { _controller: GroupGroupBundle:Group:newGroupe}

 group_addpub:
path:     /addpub
defaults: { _controller: GroupGroupBundle:Group:addpubication}
1
  • is there someone who could help me Commented Feb 12, 2017 at 18:25

1 Answer 1

2

You have the pieces, now just hook them up correctly.

$.ajax({
    type: 'POST',
    url: "{{ path('group_addpub') }}", // NOTE the " instead of '.
    data: {desc: text} // You'll access the text variable by it's property name, 'desc'.
});

In your controller:

public function addpubicationAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $des = $request->get('desc'); // <-- Here is the text variable.

    $publication = new Publication();
    $publication->setDescription($des);
    $em->persist($publication);
    $em->flush();
}

Side note re: mixing single quotes and double quotes:

'{{ path('group_addpub') }}' The problem here is that when Twig parses this, it sees 'string'variable'string' which doesn't make any sense.

Sign up to request clarification or add additional context in comments.

6 Comments

what should i do to url
@Was'SiimBenHssen try setting the AJAX url: '/addpub', then in your controller add dump($request->request); die; to see what your controller is receiving. Open the Chrome Dev Tools network tab and view the response.
nothing appears it seems that my controller didnt receive the data
@Was'SiimBenHssen, what's the full path to this controller action? Is the GroupBundle prefixed with /group/addpub?
Try changing it to url: '/groupe/addpub',
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.