0
I have a boot that every time I click my page updates.
$btn_add='<a class="btn btn-success" href="cart.php?plus='.$id.'"><i class="fa fa-plus fa-lg" aria-hidden="true" add_btn></i></a>';
$btn_remove = '<a class="btn btn-warning" href="cart.php?remove='.$id.'"><i class="fa fa-minus fa-lg" aria-hidden="true" remove_btn></i></a>';
$btn_delete='<a class="btn btn-default delete_btn" href="cart.php?delete='.$id.'"><i class="fa fa-times fa-lg" aria-hidden="true"></i></a>';
My cart.php
This is the whole system of my shopping cart.
In my checkout.php
is where I add or take selected items.
and I inserted my function cart();
in it.
I wanted to use ajax to make my boot not update my page.
my script of cart.php
.
if(isset($_GET['plus'])){
$_SESSION['product_'.$_GET['plus']]+=1;
if($_SESSION['product_'.$_GET['plus']] < 1){
header('Location: checkout.php');
}else{
header('Location: checkout.php');
}
}
if(isset($_GET['remove'])){
$_SESSION['product_'.$_GET['remove']]--;
if($_SESSION['product_'.$_GET['remove']] < 1){
header('Location: checkout.php');
}else{
header('Location: checkout.php');
}
}
if(isset($_GET['delete'])){
$_SESSION['product_'.$_GET['delete']] = '0';
header('Location: checkout.php');
}
my buttons are inside a foreach loop of mine function cart()
;
function cart(){
global $conn;
$fabric_options = '';
$query2 = "SELECT * FROM almofadas";
$result = mysqli_query($conn,$query2);
while($rows = mysqli_fetch_assoc($result)){
$tecido=$rows['tecido'];
$id_price=$rows['id_price'];
$fabric_options .= "<option value=''.$id_price.''>{$rows['tecido']}</option>";
}
$s50='50';
$s45='45';
if(isset($t50)){
$_SESSION['selected']='selected';
}
foreach ($_SESSION as $name => $value) {
if($value > 0){
if(substr($name, 0, 8 ) == "product_"){
$length = strlen($name) -8;
$item_id = substr($name,8 , $length);
$query = "SELECT *
FROM gallery2
WHERE gallery2.id =".escape_string($item_id). "";
$run_item = mysqli_query($conn,$query);
while($rows = mysqli_fetch_assoc($run_item)){
$vari = $rows['variante'];
$num = $rows['title'];
$id = $rows['id'];
$btn_add='<a class="btn btn-success" href="cart.php?plus='.$id.'"><i class="fa fa-plus fa-lg" aria-hidden="true" add_btn></i></a>';
$btn_remove = '<a class="btn btn-warning" href="cart.php?remove='.$id.'"><i class="fa fa-minus fa-lg" aria-hidden="true" remove_btn></i></a>';
$btn_delete='<a class="btn btn-default delete_btn" href="cart.php?delete='.$id.'"><i class="fa fa-times fa-lg" aria-hidden="true"></i></a>';
if($rows['variante'] < 1){
$vari="";
}else{
$vari = "-".$rows['variante'];
}
$product = '
<td style="width:100px; "><img src="../'.$rows['image'].'" style="width:90%;border: 1px solid black;"></td>
<td>'.$num.''.$vari.'</td>
<td style="width:15%;">
<select id="fabric" class="select form-control selectpicker" required="" onchange="saveChoice()" >
'. $fabric_options . '
</select>
</td>
<td>
<select id="size" class="select form-control selectpicker" required style="width:80%;" onchange="saveChoice()" >
<option value="'.$s50.'">50x50</option>
<option value="'.$s45.'">45x45</option>
</select>
</td>
<td>'.$value.'</td>
<td>R$</td>
<td>sub.total</td>
<td>
'.$btn_add.' '.$btn_remove.' '.$btn_delete.'
</td>
</tr>';
echo $product;
}
}
}
}
}
I did what you said but my buttons didn’t work...on my checkout page.php is so html and there I call Function Cart(); this Function is in Cart.php where there is only php. every time I clicked on some button on the checkout page.php ran my code in Cart.php so it was always returning to header('Location: checkout.php');
– James Allan
on the page checkout.php I have already inserted
<script src="../../js/jquery.min.js" type="text/javascript"></script>
– James Allan
but nothing is working
– James Allan
I made a correction in javascript code, try to execute now. Remember that JSON return is essential.
– Diego Henrique
I took the edited code and it still doesn’t work look here my checkout.php-> https://codeshare.io/G4Jlu and my Cart.php-> https://codeshare.io/NntjJ
– James Allan
da para edit the code sent by the browser
– James Allan
Your code is a little disorganized, I edited my answer and put together a simple structure where you can better organize with the functions.
– Diego Henrique
i added an Alert
alert('funciona');
in the ajax to do Functionplus
I received the msg, but this not increasing my$value
of my session, my$value
is my amount of certain id / "product"– James Allan
when vc mentions for example: "//your function to add" in cart_functions.php you only decrypted what that function does or is for me to add my code to add by ex:
function plus_prod($item){
 
 //sua função para plus
 $_SESSION['product_'.$_GET['plus']]+=1;
 return json_encode(['plus'=>'success']);
}
– James Allan
Would that be it?
– James Allan
You add your code
– Diego Henrique
Let’s go continue this discussion in chat.
– James Allan
It worked I just had to change
$_SESSION['product_'.$_GET['plus']]+=1;
for$_SESSION['product_'.$_GET['prod_id']]+=1;
– James Allan
All I needed was mine
$value
update automatico, pq it only updates qnd dou F5...but I will seek to find out how.... thank you very much!– James Allan