Ajax request and response in symfony

230
November 26, 2016, at 10:44 AM

Currently I do this non ajax request to edit project like this

<a href="{{ path('project_edit', { 'id': project.id }) }}">edit</a>

But I want to do this edit request using ajax so that whole edit process could be finished by ajax request. I need following things

  1. Do the above request using ajax request
  2. Change the following action to handle above ajax request

    /**
     * Displays a form to edit an existing project entity.
     *
     * @Route("/{id}/edit", name="project_edit")
     * @Method({"GET", "POST"})
     */
    public function editAction(Request $request, Project $project)
    {
        $editForm = $this->createForm('AppBundle\Form\ProjectType', $project);
        $editForm->handleRequest($request);             
        return $this->render('project/edit.html.twig', array(
            'project' => $project,
            'edit_form' => $editForm->createView(),
        ));
    }
    
  3. above action should send ajax response such that following modal pop up and then form edit content should be shown in the {#content should go here#} section below.

..

<div class="modal fade" id="modalBox" tabindex="-1" role="dialog" aria-labelledby="confirm" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            {#content should go here#}
        </div>
    </div>
</div>

So the whole process in short should be like this, send an ajax request for edit a project, edit action should handle the edit request and then show a modal and inside the modal render edit form content. Edit form content

{{ form_start(edit_form, { 'style': 'horizontal' }) }}
    {{ form_widget(edit_form) }}
{{ form_end(edit_form) }} 

How can I do this simple ajax request and show pop up modal.

Answer 1

Your link :

<a href="#" id="reload-modal">edit</a>

Your AJAX action:

('body').on('click','a#reload-modal',function(){
    $.ajax({
        url: "{{ path('project_edit', { 'id': project.id }) }}",
        type: "GET",
        success: function(data){
            $(".modal-content").html(data);
        }
    });
});

Your controller action stay the same. But ensure that only modal content is rendered without base template.

READ ALSO
PHP wait before executing script - automatic

PHP wait before executing script - automatic

Hi so I have a website created from PHP and I want to add a delay onto something so that the script executes after 5 secondsI want this to be automatic

314
How to add pthreads workers according to this php class

How to add pthreads workers according to this php class

I am building a Proxy Server using PHP Sockets Extension and Pthreads

240
Remove text using str_Replace

Remove text using str_Replace

I have seven lines of textI want to use PHP to do the things below

282
PHP merge two arrays like in javascript&#39;s push([[],[]])

PHP merge two arrays like in javascript's push([[],[]])

I have two arrays which I want to combine in PHP:

211