使用Symfony的购物系统

时间:2016-04-14 10:36:22

标签: php symfony

Iam学习symfony,我想为我的学校项目建立一个商店系统。我想将用户选择的项目列表发送给另一个我不知道如何的控制器。

这是我的ShowItemAction

public function ShowItemAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $form = $this->createFormBuilder()
        ->add('search', 'textbox', array(
            'attr' => array(
                'label' => false
            )))->getForm();

    $form->handleRequest($request);
    if (($form->isSubmitted()) && ($form->isValid())) {
        $formdata = $request->request->get('form');
        $search = $formdata['search'];
        $products_Repo = $em->getRepository('MyShopBundle:Products')->GetProducts($search);
        return $this->render('MyShopBundle:Products:show.html.twig',array(
            'form' => $form->createView(),
            'products'=> $products_Repo
        ));
    }

    return $this->render('MyShopBundle:Products:show.html.twig',array(
        'form' => $form->createView()
    ));

}

和我的show.html.Twig

{% block body%}
<div id="cart-content">

</div>
    <div class="cart-buttons">
    <button id="cart">View Shopping Cart</button>
    <button id="clear-cart">Clear Cart</button>
</div>

{{ form(form) }}

{% if products is defined %}
    <table>
        <thead>
        <tr>
            <th>Partner-Name</th>
            <th>ProductNr</th>
            <th>Description</th>
            <th>price</th>
        </tr>
        </thead>
        <tbody>
        {% for product in products %}
            <tr>
                <td><div class="partner">{{product.partner}}</div></td>
                <td><div class="partnerdata" data-value="{{ product.id }}">{{ product.productnr }}</div></td>
                <td><div class="description"> {{ product.description }}</div></td>
                <td><div class="price">{{ product.price }}</div></td>
                <td class="counter"><input type="number" name="count" min="1" step="1"></td>
                <td class="cart">
                    <button type='submit'>in den Warenkorb</button>
                </td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
{% endif %}
{% endblock %}

我的javascript:

 $('.cart').click(function (event) {
        event.preventDefault();
        var closestTr = $(this).closest('tr');
        var ref = closestTr.find('.partner').text();
        var data_value = closestTr.find('.partnerdata').data('value');
        var productNr=    closestTr.find('.partnerdata').html();
        var price= closestTr.find('.price').html();
        var count = closestTr.find('input').val();
        if (count < 1) {
        }
        else {
            $(".cart-content").prepend(HS_ref + "|" + data_value + "|" + herstellerNr + "|" + count + "|" + vk);
        }
    });

我可以看到用户选择的数据(在<div id="cart-content"></div>内),但我不知道如何将这些内容作为POST发送到控制器。

像这样:https://www.codeofaninja.com/2013/04/shopping-cart-in-php.html

我正在研究Symfony 2.7

1 个答案:

答案 0 :(得分:0)

您可以使用AJAX

执行此操作

首先,您需要获取html数据

其次,您必须将此数据发送到控制器

第三,你必须在控制器中获取数据

以下是一个例子:

首先 “你需要获取html数据”

HTML:

<div id="cart-content" data-route="{{ path('get_user_card_items') }}">
    <span data-item-id="item1" data-quantity="2"></span>
    <span data-item-id="item2" data-quantity="1"></span>
    <span data-item-id="item3" data-quantity="5"></span>
</div>

JS

/**
 * Get the content of #cart-content
 */
function getCartItems() {

    var items = [];
    $('#cart-content span').each(function(){ // loop into all span
        var item = { // create an item object who get all the data
            'id' : $(this).attr('data-item-id'),
            'quantity' : $(this).attr('data-quantity'),
        }
        items.push(item); // push into an array
    });

    return items;
}

第二次 “您必须将此数据发送到控制器”

<强> JS

function sendCartItems() {
    $.ajax({
        url: $('#cart-content').attr('data-route'), // here is a route variable
        method: 'POST',
        data: {items: getCartItems()},
        success: function (data) {
            // do some stuff when it's send
        }
    });
}

第三次您必须在控制器中获取数据

<强> CustomController.php

class CustomController extends Controller
{

    /**
     * @Route("/getUserCardItems",  name="get_user_card_items")
     */
    public function getUserCardItemsAction(Request $request)
    {
        $items = $request->get('items');
        var_dump($items);die; // display for you the items to see if it's works (look into the networks console tab on google chrome)
       // some stuff like sending items to database...
    }
}

Access network tab google chrome

你做完了;)