Refresh or redirect a running page

2

I am making a shopping cart, I have the product table formed, with the values that I am adding in a session variable .. the problem is that every time I add a product, it sends me the beginning of the page, and I would like that either send me to each div, or just refresh the cart table (which is below the product at this time)

This is the home page, where the two tables are:

<?php
session_start();
include_once("conectar.php");  
$link = Conectarse();


if(isset($_SESSION['carrito'])) 
{
    $carrito=$_SESSION['carrito'];
}
else
{
    $carrito=false;
}

?> 

<div class="col-md-8 col-md-offset-3">
<table class='Table table-striped' style="float:left">
    <thead>  
        <tr>;  
            <th scope="col">Codigo</th>  
            <th scope="col">Producto</th>  
            <th scope="col">Comentario</th>  
            <th scope="col">Precio</th>
            <th scope="col">Agregar al carro</th>  
        </tr>
    </thead>
    <tbody> 
<?php
include ("carrito.php");

    foreach ($link->query("SELECT * FROM productos ORDER BY codigo") as $row)
    {   
        echo "<tr>"; 
        echo "<div id='".$row["codigo"]."'>";
        echo "<th scope='row'>".$row["codigo"]."</th>";
        $cod=$row["codigo"];
        echo "<td>".$row["producto"]."</td>"; 
        echo "<td>".$row["descripcion"]."</td>"; 
        echo "<td>$".$row["precio"]."</td>";
        echo "<td><a href='agregarproducto.php?codigo=$cod'><i class='fa fa-cart-plus fa-3x' aria-hidden='true'></i> </td>";  
        echo "</div>";   
        echo "</tr>";  
    }
?>
    </tbody>  
</table> 
</div>



<table class='Table table-striped'>
<thread>
    <tr>
            <th scope="col">Producto</th>  
            <th scope="col">Cantidad</th>  
            <th scope="col">Precio Unitario</th>
            <th scope="col">Precio total</th>  
            <th scope="col">Eliminar del carro</th>  
    </tr>
</thread>
    <tbody>
<?php
    $carrito=$_SESSION["carrito"];
    $total=0;
    foreach ($carrito as $car) 
    {
        echo "<tr>";  
        echo "<th scope='row'>".$car["producto"]."</th>";
        echo "<td>".$car["cantidad"]."</td>"; 
        echo "<td>$".$car["precio"]."</td>";
        echo "<td>$".$car["precio"]*$car["cantidad"]."</td>";
        $total=$total+($car["precio"]*$car["cantidad"]);
        echo "<td><i class='fa fa-cart-arrow-down fa-3x' aria-hidden='true'></i> </td>";     
        echo "</tr>";  
    }
    echo "<tr>";
    echo "<th scope='col'>Precio total: $".$total."</th>";
    echo "</tr>";
?>

    </tbody>
</table>

And this is the cart class, to add each one (through a page that only instantiates that class):

<?php

class carrito
{
    public function agregarProducto()
    {
        session_start();
        include_once ("conectar.php");
        $link = Conectarse();
        $id=$_GET["codigo"];

        if(isset($_SESSION['carrito']))
        {
            $carrito=$_SESSION['carrito'];
        }

        $carrito=$_SESSION['carrito'];
        $cantidad=$carrito[$id]['cantidad']+1;

        $consulta=("select * from productos where codigo=$id");
        $pro=$link->query($consulta);

        $fila=mysqli_fetch_array($pro);

        $carrito[$id]=array($id,'cantidad'=>$cantidad,'producto'=>$fila['producto'],'precio'=>$fila['precio'],'id'=>$id);

        $_SESSION['carrito']=$carrito;

        header("location:inicio.php");
    }
}
?>
    
asked by Gaston Flores 03.01.2018 в 23:01
source

2 answers

1

I recommend you:

1) Use the .click event to capture when the user clicked on adding the item to the cart (and also when pressing delete is another .click event).

2) Create a div ("items_carrito") that will contain the table where you show the items "attached" to the cart or simply those that the cart has at the moment.

3) Use the .load function of jquery to "refresh" or "reload" the contents of <div id="items_carrito"></div> with the new information that the user is adding.

To find out how to do all this visit jquery load . You will explicitly serve example 1 shown on that web page.

That's it. A greeting.

    
answered by 04.01.2018 в 13:31
0

Your question is very broad, but use JS to load your car, in a div called a car, when you add it just send the request to your server and it will send you the data and create your car and keep it in that div via Jquery or JS

    
answered by 04.01.2018 в 06:59