【问题标题】:Symfony2 Ajax call to display a message on a specific conditionSymfony2 Ajax 调用以在特定条件下显示消息
【发布时间】:2015-07-16 07:31:29
【问题描述】:

我是 javascript 和 ajax 的初学者,我只是不知道如何在某些情况下显示消息。

假设用户可以增加或减少他想要购买的产品的数量。我做到了(并不是说这很容易)。但是如果产品缺货,他就不能再增加数量了。我怎样才能在消息中显示这一点。例如,如果用户尝试增加数量,但产品缺货,我想在在同一个 ajax 调用上显示下面的消息。

这是控制器:

public function addQuantityAction( Request $request ) {
    $response = new JsonResponse();
    $requestData = $request->request->all();
    $productid     = $requestData['product'];
    $quantity = $requestData['quantity'];
    /** logic*/
    $em = $this->getDoctrine()->getManager();
    $product = $em->getRepository('MpShopBundle:Product')->find($productid);
    $qtyAvailable = $product->getStockAvailable();
    $session = $this->getRequest()->getSession();
    $cart = $session->get('cart', array());
    if ( $qtyAvailable > $cart[ $productid ] ) {
        $cart[ $productid ] = $cart[ $productid ] + 1;
        $qtyAvailable = $qtyAvailable - 1;
        $response->setData(array('success'=>true,'message'=>'Qunatity increased','amount'  => $cart[ $productid ]));
         $session->set('cart', $cart);
    } else {
        $response->setData(array('success'=>false,'message'=>'Out of stock'));
    }
    return $response;
}

阿贾克斯:

$(document).ready(function () {
    $(document).on('click', '.add', function (e) {
    $this = $(this);   
    $.ajax({
        type: 'POST',
        url: 'add',
        dataType: 'JSON',
        data: {product: $this.parent('.input-append').find('input').data('id'),quantity: $this.parent('.input-append').find('input').val()},
        success: function (data) {      
          if(data.success == false){
           alert('error')
          }else{
            $('.test').load(" .test");
            $('.sidebar').load(" .sidebar");
            $('.top').load(" .top");
           }
        }
    });

还有树枝:

<div class="input-append">

   <input class="span1" style="max-width:34px" placeholder="{{ key }}" value="{{ item }}" id="appendedInputButtons" size="16" type="text" data-id="{{ key }}"/>
   <a href="javascript:void(0)" class="remove btn"><i class="icon-minus"></i></a>
   <a href="javascript:void(0)" class="add btn"><i class="icon-plus"></i></a>                   
   <button class="btn btn-danger" type="button"><a href="{{ path('cart_remove', {'id': key}) }}"><i class="icon-remove icon-white" style="color:white"></i></a></button>

    <p> display message here </p>

                        </div>

【问题讨论】:

    标签: javascript php jquery ajax symfony


    【解决方案1】:

    您还可以根据操作的成功或错误状态添加类。

    阿贾克斯

    $(document).ready(function () {
        $(document).on('click', '.add', function (e) {
        $this = $(this);   
        $.ajax({
            type: 'POST',
            url: 'add',
            dataType: 'JSON',
            data: {product: $this.parent('.input-append').find('input').data('id'),quantity: $this.parent('.input-append').find('input').val()},
            success: function (data) {      
              if(data.success == false){
               alert('error')
              }else{
                if(data.message != 'undefined') {
                    $('#ajax_response--message').html(data.message)
                }
                $('.test').load(" .test");
                $('.sidebar').load(" .sidebar");
                $('.top').load(" .top");
               }
            }
        });
    
    <div class="input-append">
    

    树枝

        <p id="ajax_response--message"> display message here </p>
    
                            </div>
    

    【讨论】:

      猜你喜欢
      • 2023-01-02
      • 1970-01-01
      • 2014-01-28
      • 1970-01-01
      • 2023-03-03
      • 2014-07-12
      • 2021-05-17
      • 2012-10-26
      • 2016-09-01
      相关资源
      最近更新 更多