Developing a shopping cart using objects and session variables.
<?php
session_start();
require ("../../models/claseTicket.php");
if (isset($_POST['accion'])){
if ($_POST['accion']=="listar"){
if(isset($_SESSION['carrito'])){
$carrito = $_SESSION['carrito'];
echo json_encode($carrito);
}else{
$carrito = array();
echo json_encode($carrito);
}
}else if ($_POST['accion']=="agregar"){
$id = $_POST['id'];
$pu = $_POST['precio'];
$cant = $_POST['cantidad'];
$nom = $_POST['nombre'];
$detalle = new detalleTicket($id,$pu,$cant,$nom);
if(isset($_SESSION['carrito'])){
$carrito = $_SESSION['carrito'];
} else {
$carrito = array();
}
array_push($carrito, $detalle);
$_SESSION['carrito'] = $carrito;
}
}
?>
And this is the AJAX request, as you will see in our "data":
$(document).ready(function() {
listarDetalle();
});
function listarDetalle(){
var accion="listar";
$.ajax({
type: "POST",
url: "//localhost/gestionweb/includes/php/procesoDetalle.php",
data: { "accion":accion},
dataType:'json',
error: function(){
alert("error petición ajax");
},
success: function(data){
console.log(data);
for (var i = 0; i < data.length; i++) {
var newRow =
"<tr>" +
"<td>" + data[i].idp + "</td>" +
"<td>" + data[i].precio + "</td>"
"<td>" + data[i].cantidad + "</td>" +
"<td>" + data[i].pu + "</td>" +
"<td><input type='radio' id='"+data[i].idp+"' name='seleccion'/></td>"+
"</tr>";
$(newRow).appendTo("#ticket tbody");
} }
}).fail( function( jqXHR, textStatus, errorThrown ) {
if (jqXHR.status === 0) {
alert('Not connect: Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (textStatus === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (textStatus === 'timeout') {
alert('Time out error.');
} else if (textStatus === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error: ' + jqXHR.responseText);
}
});;
};
And in the columns of the table says undefined.
The method that adds a product is in another .js file but it is as follows:
$("#Agregar").click(function(event){
var cantidad=$("#CANTIDAD").val();
if (cantidad!==""){
var accion = "agregar";
$.ajax({
type: "POST",
url: "../gestionweb/includes/php/procesoDetalle.php",
data: {accion,id,cantidad,nombre,marca,precio},
dataType:'html',
error: function(){
alert("error petición ajax");
},
success: function(data){
}
})
I attach the Ticket class:
<?php
require_once ("/../includes/claseConexion.php");
require_once ("claseProducto.php");
class detalleTicket{
private $idp;
private $precio;
private $cantidad;
private $nombre;
private $subtotal;
public function __construct($id,$pu,$cant,$nom){
$this->idp=$id;
$this->precio=$pu;
$this->cantidad=$cant;
$this->nombre=$nom;
$this->subtotal= ($this->cantidad * $this->precio);
}
public function setSubtoal($sub){
$this->subtotal=$sub;
}
public function actualizaCantidad($nueva){
$this->cantidad=$this->cantidad + $nueva;
$sub= $this->cantidad * $this->precio;
$this->setSubtoal($sub);
}
}
class Ticket{
private $id;
private $detalle = array();
private $fecha;
private $hora;
private $total;
private $stm;
private $vuelto;
public function __construct(){
}
public function agregaDetalle(detalleTicket $d){
$this->detalle[] = $d;
}
public function setTotal($val){
$this->total=$val;
}
public function listarArt(){
return $this->detalle;
}
public function registrar(){
//me conecto a la bd
$conexion = Conexion::singleton_conexion();
try{
$conexion->beginTransaction();
$queryid="select comprobante from identificadores;";
$array=$conexion->query($queryid);
foreach($array as $a){
$id = $a['comprobante'];
}
$qnuevoid="UPDATE identificadores SET comprobante=".++$id.";";
$conexion->query($qnuevoid);
$queryticket="insert into despensa.ticket (idticket, fecha, total,vuelto) values (".$id.",'".$this->fecha."',56,88);";
$conexion->query($queryticket);
$conexion->commit();
}catch(Exception $e){
$conexion->rollBack();
echo "Fallo: " . $e->getMessage();
}
}
}
?>