0

I am currently working on a Symfony2 project that at some point involves submitting a form and performing a lengthy operation on the filesystem (recursive copy).

At the moment, the user has to wait until the filesystem operation is finished and form processed before being redirected. I have tried enhancing this functionality by using some simple AJAX. I have run into trouble of not being able to perform a form submission and querying another action/controller concurrently.

Does Symfony2 allow concurrent controller requests? If it doesn't, is there any way to complete achieve something that would work like basic concurrency?

This is the outline of what I am trying to do in terms of code:

<form action="{{ path('performSomeAction', { 'id': client.getId(), 'someId':some.someId() }) }}" method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}

<input type="submit" />

And this is what I am trying to do concurrently to submitting the form, using asynchronous XMLHttpRequest:

<script language="JavaScript" type="text/javascript">        
        function getXmlHttpRequestObject() {
            if (window.XMLHttpRequest) {
                return new XMLHttpRequest(); //Not IE
            } else if (window.ActiveXObject) {
                return new ActiveXObject("Microsoft.XMLHTTP"); //IE
            } else {
                alert("Your browser doesn't support the XmlHttpRequest object. ");
            }
        }

        var receiveRequest = getXmlHttpRequestObject();

        function handleFetchProgress() {
            if (receiveRequest.readyState == 4) {
                var newData = receiveRequest.responseText;
                var oldData = document.getElementById("span_result").innerHTML;
                var toDisplay = oldData +  "<br />" + newData;
                document.getElementById("span_result").innerHTML = toDisplay;
            }
        }

        function fetchProgress() {     
            if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
                receiveReq.open("GET", '{{ path('fetchProgress') }}', true);
                receiveRequest.onreadystatechange = handleFetchProgress;
                receiveRequest.send(null);
            }         
        }

        $(document).ready(function() {
            $('form').submit(function() {
                setInterval("fetchProgress()", 1000);
                return false;
            });
        });

6
  • Symfony2 is for building web apps, I hope it allows concurrent controller requests... Describe "not being able to perform a form submission and querying another action/controller concurrently" more precisely please. Commented Mar 18, 2012 at 23:16
  • As soon as the AJAX request is executed on the jQuery's .submit (this is the one that tries return Response with some text - "receiveReq.open("GET", '{{ path('fetchProgress') }}'), the controller responsible for handling the form ({{ path('performSomeAction') }}) doesn't return anything. I do get a response from the AJAX request, but there is no response from the form submission controller. So, I submit a form, the page loads for one second until there is a response from the AJAX request and the controller responsible for form submission stops. Commented Mar 19, 2012 at 11:32
  • Are you sure you have 2 requests? Check your access log. Commented Mar 19, 2012 at 11:54
  • Thank you for pointing this out. There are no 2 requests. It is one or the other, it is either the form submission request (if I disable the setInterval("fetchProgress()") and the filesystem operation completes and returns a view render. If I don't disable the setInterval(), then I am constantly receiving responses from the "fetchProgress()". Could it be something with the property of jQuery's .submit handler? Commented Mar 19, 2012 at 14:36
  • @greg0ire If I set return to true for the jQuery's .submit handler, the form is being submited, but the AJAX request is not being performed. Commented Mar 19, 2012 at 21:04

1 Answer 1

4

This is probably caused by session lock and is explained here : How do I kill a session in Symfony2?

You have to unlock session write mode to admit another request to access it.

I wrote a blog post about dealing with asynchronous requests : http://blog.alterphp.com/2012/08/how-to-deal-with-asynchronous-request.html

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

1 Comment

This was exactly what was causing the issue! The question is quite outdated and I have solved it by performing session_write_close(), since then I have also re-written it to use jQuery as @greg0ire pointed out.

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.