Anuj TBE
Anuj TBE

Reputation: 9800

CakePHP 3 : Retrieving data from database using Ajax

I am working on a CakePHP 3 project which is having a Apply Coupon form.

I want to apply the coupon using Ajax.

The view of coupon form is

<?= $this->Form->create(null, [
  'url' => ['controller' => 'Coupons', 'action' => 'checkCoupon'],
  'name' => 'checkCoupon'
]) ?>
<?= $this->Form->input('coupon_code', [
  'type' => 'text',
  'placeholder' => 'Apply Coupon Code',
  'label' => false
]) ?>
<?= $this->Form->submit('Apply Coupon') ?>

and the checkCoupon action in CouponsController is

public function checkCoupon()
{
  $this->request->onlyAllow('ajax'); // No direct access via browser URL

  if ($this->request->is('post')) {
    $couponCode = $this->request->data['coupon_code'];
    $couponCheck = $this->Coupons->find('all', [
      'conditions' => [
      'coupon_code' => $couponCode
      ]
    ]);
    if ($couponCheck->count() === 1) {
       $coupon = $couponCheck->first();
       $valid_till = $coupon->valid_till;
       $dt = new Time($valid_till);
       $date = $dt->format('Y-m-d');

       if ($date >= date('Y-m-d')) {
         echo 'Coupon is Valid. Discount of '.$coupon->value.'has been applied';
       } else {
         echo 'Coupon is Expired';
       }
    } else {
       echo 'This is not a valid coupon code';
    }
  }
}

I want the $coupon->value and $coupon->id to be retrieved and added to the checkout link as

<?= $this->Html->link(__('Confirm Checkout'), ['controller' => 'ServiceRequests', 'action' => 'confirmCheckout', $service->id, $primaryAddressId, $serviceArea->id, $coupon->id], ['class' => 'btn btn-block btn-success']) ?>

The Apply Coupon form is in checkout action of RequestsController Also the form is working well. I have checked it by removing the onlyAllow('ajax') line and printing values in check_coupon.ctp view.

How could I do it using Ajax ?

Edit 2 : checkout.ctp

<div class="form-info coupon">
  <?= $this->Form->create(null, [
    'url' => ['controller' => 'Coupons', 'action' => 'ajax_checkCoupon'],
    'name' => 'checkCoupon',
    'id' => 'checkCoupon'
  ]) ?>
  <?= $this->Form->input('coupon_code', [
    'type' => 'text',
    'placeholder' => 'Apply Coupon Code',
    'label' => false
  ]) ?>
  <label class="hvr-sweep-to-right">
    <?= $this->Form->submit('Apply Coupon', ['id' => 'applyCoupon']) ?>
  </label>
  <label id="couponUpdate"></label>
  <label id="loading" style="display:none;">Loading...</label>
  <?php
     $data = $this->Html->script('#checkCoupon')->serializeForm(['isForm' => true, 'inline' => true]);
     $this->Html->script('#checkCoupon')->event(
       'submit',
       $this->Html->script(
        [
          'controller' => 'Coupons',
          'action' => 'ajax_checkCoupon'
        ],
        [
          'update' => '#couponUpdate',
          'data' => $data,
          'async' => true,
          'dataExpression' => true,
          'before' => "$('#loading').fadeIn();$('#applyCoupon').attr('disabled','disabled');",
          'complete' => "$('#loading').fadeOut();$('#applyCoupon').removeAttr('disabled');"
        ]
      )
    );
   ?>
  </div>

Error : Call to a member function serializeForm() on string on line 18 in checkout.ctp

Upvotes: 0

Views: 523

Answers (0)

Related Questions