A
A
Alexander Zhukov2018-04-03 18:44:27
PHP
Alexander Zhukov, 2018-04-03 18:44:27

How to send rollback json from php to ajax?

There is a product card:

<a href="./cart.php?product_id=<?=$products['id']?> data-id="<?=$products['id']?>"" class="buy">Купить</a>


cart.php:

<?php 
session_start();

include 'include/database.php';

if (!isset($_SESSION['cart'])) {
  $_SESSION['cart'] = array();
}

$res_data = array();

$product_id = isset($_GET['product_id']) ? $_GET['product_id'] : null;

if (isset($_SESSION['cart']) && array_search($product_id, $_SESSION['cart']) === false) {

  $_SESSION['cart'][] = $product_id;

  $res_data['count'] = count($_SESSION['cart']);

  $res_data['success'] = 1;

}else {
  $res_data['success'] = 0;
}

json_encode($res_data);


How to make ajax request to send json to client

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Leonid Knyazev, 2018-04-03
@amorphis

XMLHttpRequest , Fetch API , jQuery.ajax() take your pick.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question