2

I've got a 'create' form in Symfony and by putting in some informations in textarea, the controller should just process the request and response it respectfully.

The AJAX call is made in an twig template:

{% block content_foot_script %}
<script>
    function fetchData() {
        xhttp = new XMLHttpRequest();
        xhttp.open("POST", "{{ path('receiptFetchSystem') }}", true);
        xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        var data = $("#admin_store_receipt_receiptbundle_receipt_objects").val();
        xhttp.send('items=' + data);

        xhttp.onreadystatechange = function() {
            if (xhttp.success && xhttp.code == 200) {
                document.getElementById('demo').innerHTML = xhttp.responseText;
            }
        }
    }
</script>
{% endblock %}

and the ReceiptFetchController handles the requests:

<?php

namespace Admin\Store\Receipt\ReceiptBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class ReceiptFetchController extends Controller
{
    /**
     * @Route("/call", name="receiptFetchSystem")
     * @Template()
     */
    public function callAction()
    {
        $request = $this->container->get('request');
        $data = $request->request->get('items');

        $response = array("code" => 200, "success" => true);
        return new Response($response);    
    }

}

Well, as expected, I'm getting no response :)

Best regards

4
  • 1
    what happens when you add a use statement for the Symfony Response class? Commented Nov 17, 2015 at 17:00
  • 1
    What is xhttp.success? Also encode your data properly... xhttp.send('items=' + encodeURIComponent(data)); Commented Nov 17, 2015 at 17:54
  • 1
    actually the check is if (xhttp.responseType == 4 && xhttp.status == 200) { your response is not attached to the xhr object Commented Nov 17, 2015 at 17:57
  • Musa there are no results... I've changed the code. I think the problem might be in AJAX call. fetchData() is called by onchange event. Commented Nov 17, 2015 at 19:11

1 Answer 1

1

Well, I've made it working by changing some codes, here they are:

ReceiptFetchController.php:

<?php

namespace Admin\Store\Receipt\ReceiptBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Response;

class ReceiptFetchController extends Controller
{
    /**
     * @Route("/call", name="receiptFetchSystem")
     * @Template()
     */
    public function callAction()
    {
        $request = $this->container->get('request');

        $items = $request->request->get('items');
        $response = new Response();
        $response->headers->set('Content-Type', 'application/json');

        $itemsList = preg_split('/\r\n|[\r\n]/', $items);

        $em = $this->getDoctrine()->getRepository('AdminStoreProductProductBundle:Product');
        $productsList = array();
        foreach ($itemsList as $key){
            $product =$em->findOneBy(array('barCode' => $key));
            if ($product) {
                $productDetail['Price'] = $product->getSalesPrice();
                $productDetail['Type'] = $product->getType()->getSlugify();
                $productDetail['Name'] = $product->getName();
                $productsList[] = $productDetail;
            }

        }

        $response->setContent(json_encode($productsList));
        return $response;
    }

}

and here is the view file:

{% block content_head_script %}
    <script>
        function fetchData() {
            xhttp = new XMLHttpRequest();
            var data = $("#admin_store_receipt_receiptbundle_receipt_objects").val();
            xhttp.open("POST", "{{ path('receiptFetchSystem') }}", true);
            xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xhttp.send('items=' + (data));

            xhttp.onreadystatechange = function () {
                if (xhttp.readyState == 4 && xhttp.status == 200) {
                    TableGenerator(xhttp.responseText);
                }
            }
        }

        function TableGenerator(response) {
            var totalPrice = 0;
            var arr = JSON.parse(response);
            var i;
            var out = "";

            for(i = 0; i < arr.length; i++) {
                out += "<tr><td>" +
                        arr[i].Price +
                        "</td><td dir='ltr'>" +
                        arr[i].Type +
                        "</td><td>" +
                        arr[i].Name +
                        "</td></tr>";

                totalPrice += arr[i].Price;
            }
            out += "<tr><td colspan='2' dir='ltr'><strong>جمع کل</strong></td><td>" + totalPrice + "</td></tr>";
            $('#tableBody').html(out);
        }
    </script>
{% endblock %}
Sign up to request clarification or add additional context in comments.

Comments

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.