My Cart Div is not refreshing until i press refresh symbol [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am starting a cart session on my cart page but cart dropdown is not updating with a count until I refresh the page. I just want to update the div without reloading.
I am using foreach but it doesn't work. What should I do now.
<?php
session_start();
require_once("product.php");
?>
<li class="header-cart dropdown default-dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" aria-expanded="true">
<div class="header-btns-icon">
<i class="fa fa-shopping-cart"></i>
<?php
if(isset($_SESSION['cart'])){
?>
<span class="qty"><?php echo count($_SESSION['cart'])?></span>
<?php
}else{
?>
<span class="qty">0</span>
<?php
}
?>
</div>
<strong class="text-uppercase">My Cart:</strong>
<br>
<span>Shop Now</span>
</a>
<div class="custom-menu">
<div id="shopping-cart">
<div class="shopping-cart-list">
<?php
#$cart = unserialize(serialize($_SESSION['cart']));
$total = 0;
if($cart){
foreach($cart as $k=>$v)
{
$total += $v->price * $v->quantity;
?>
<div class="product product-widget">
<div class="product-thumb">
<img src="<?php echo $v->img;?>" height="50" width="60" alt="">
</div>
<div class="product-body">
<h3 class="product-price"><?php echo $v->price;?> <span class="qty">x1</span></h3>
<h2 class="product-name"><?php echo $v->name;?></h2>
</div>
<button class="cancel-btn" style="margin-right:8px;"><i class="fa fa-trash"></i></button>
<?php }
?>
<div class="shopping-cart-btns">
<button class="main-btn">View Cart</button>
<button class="primary-btn" style="margin-left:15px;">Checkout <i class="fa fa-arrow-circle-right"></i></button>
</div>
<?php
}
else{
echo '<p style="color: red;">No Product is added in your cart</p>';
}
?>
</div>
</div>
</li>
I want to update the cart div when I click on Add to cart.

As you have not provided enough code to work with, I will post the concept and hope you will be able to do it, also comment if you have any more doubts I'm always open to help
So firstly you need to write a function that asynchronously fetches
the data - Which means it fetches the data without having the page to
reload.
So you need to write the PHP code in a PHP file instead of in the HTML page, for example,
cart.php or where ever you have written your query to fetch the count
of the cart from database.
Once you have done that then you need to write an AJAX call to the
button of Add to Cart, So when you click on add to cart, it triggers
the ajax request and fetches the data from the backend which is
passed from your cart.php
for example , write this code inside your add to cart button
fetch("cart.php")
.then(res=>res.json)
.then(res=>{
//You will get the count here and just append it inside the tag where your count is displayed
//For example :
count.innerHTML = res
})
Don't forget to json_encode($count); in the PHP file so that you can receive the data in JSON format

Related

How to link to a specific part of a page HTML PHP

I am trying to link to a specific part in my php page. I have tried all the other answers on here but none of them have helped my special problem. This is how I am trying to go to the link
<a href="<?php echo $userLoggedIn; ?>#posts_tab">
So when the user clicks the dropdown they see a list of things. One of those things being the word Profile and when the user clicks on their profile they go to their own personal profile. When they are there they have a list of items at the top that look like this:
So what I want to do is when they click the profile they are automatically on the Profile Posts tab. Like a default option. For some reason it's not working for me and I can't figure out why. Any help please ?
header.php:
<a href="<?php echo $userLoggedIn; ?>#posts_tab">
</a>
profile.php:
<a style="color: #000;" id="posts_tab" href="javascript:void(0)" onclick="openCity(event, 'Posts');">
<div class="w3-third tablink w3-bottombar w3-hover-light-grey w3-padding">
<center>Profile Posts</center>
</div>
</a>
I believe this is what you want, you need to let your profile page know that you want to load that part of profile.php, you can do it different ways I have used php and GET as example, I've sent a parameter on the URL and received it on profile.php
header.php
<a href="<?php echo $userLoggedIn; ?>?tab=posts_tab">
profile.php
<?php
$tab = $_GET["tab"];
if ($tab == "posts_tab"){
?><script>openCity(event, 'Posts');</script><?php
}
?>
<a style="color: #000;" id="posts_tab" href="javascript:void(0)" onclick="openCity(event, 'Posts');">
<div class="w3-third tablink w3-bottombar w3-hover-light-grey w3-padding">
<center>Profile Posts</center>
</div>
</a>

How to redirect to a particular block of html page?

I'm working on a simple project which has a requirement to show a particular block of html by passing an div id to that url.
I have tried so many solutions, but none of them worked for me.
Here is my code.
<div class="row">
<a onclick="redirect_tooltip();">
<i class="fa fa-question-circle" aria-hidden="true"></i>
</a>
</div>
Whenever I click the icon it calls a Javascript function, through that function I tried to pass a url with another page division id
function redirect_tooltip()
{
var source_type = $("#_src_type").val();
if(source_type == "source")
{
window.location.replace(base_url+'/help/tooltip#rtmp_4');
//I have already difined the base url
}
}
My html code:
<div id="rtmp_4">
<h4>Some Title</h4>
<h5> <?php echo trim($tooltip_rtmp_server[1]);?></h5>
<p> <?php echo trim($tooltip_rtmp_server[3]);?> </p>
<h5> <?php echo trim($tooltip_rtmp_stream_name[1]);?></h5>
<p> <?php echo trim($tooltip_rtmp_stream_name[3]);?> </p>
</div>
I need to display only the rtmp block whenever the url executes. Please give me a suggestions like where I am doing the mistake.

Show amount and total from the cart [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I was wondering what will be the best way to show the number of items and the total price for those item which are already in cart(shopping basket).
The header.php page is on all my pages. so whatever page the user goes to they should be able to still see what the number of item in page and total price.
For an example if I had about 7 items my cart and let's say those item cost about $600.78. How cab I count those item up and show the number and total price in the div below.
example
Let's say u go on Nike website and buy 3 pairs of Nike shoes. If u look on top of the page it tells u how many items u have in your basket and the total price for all those items.
My header.php
<div class="cart">
<div class="shbag">
<a href ="cart.php">
</div>
<ul>
<li class="item"><a>
Item
<span id="items">0</span>
</a></li>
<li class="cart_price"><a href=""
title="Cart">
Total £
<span id="cart_total">0.00</span></a></li>
<!--cart page -->
<li><a href="cart.php">
<span class="check_bdr"
title="Checkout">View bag</span></a></li>
</ul>
</div>
Thanks in Advance
Here is my approach:
header.php
<?php require_once('minicart.php'); ?>
<div class="cart">
<div class="shbag">
<a href ="cart.php">
</div>
<ul>
<li class="item">
Item <span id="items"><?php echo cart_item_count() ?></span>
</li>
<li class="cart_price">
<a href="" title="Cart">Total £
<span id="cart_total"><?php echo cart_total() ?></span>
</a>
</li>
<!--cart page -->
<li>
<a href="cart.php">
<span class="check_bdr" title="Checkout">View bag</span>
</a>
</li>
</ul>
</div>
minicart.php
<?php
function cart_item_count()
{
return count($_SESSION['cart_array'])
}
function cart_total()
{
// calculate your cart total here
return $cart_total;
}

Display current value of PHP variable in realtime using js/jQuery

This might seem easy enough for the lovely experts here at SO :) but I can't find a decent answer/question about this on SO or google. Pls help. :)
How do i display a current value of a php variable in realtime via AJAX/js/jQuery
Basically im trying to do an E-commerce site and I am storing the shopping cart contents using a session and displaying it with an echo statement counting how many contents is currently in the basket, however when they are on a certain product and tried adding it in the cart, the value of the cart doesn't echo the added item until there is a page refresh, so i need something like an AJAX to display the updated value of the session when an item is added without having a page refresh.
Thank you so much.
Here is my code snippet for the cart as a sample:
// Assign shopping cart ($_SESSION['cart']) into variable
$basket = count($_SESSION['cart']);
And for displaying:
<!-- Display contents of the shopping cart -->
<ul id="basket" class="clearfix">
<li class="top">
<a href="checkout.php" class="top_link">
<span>Items: <? echo $basket ?></span>
</a>
</li>
</ul>
Thank you so much.. kindly provide a sample code on how to achieve this (either js or jQuery) and would help as well for best practices in dealing with this type of scenario.
First solution (based on your request - BAD IDEA)
get-count-items.php
<?php
session_start();
die(count($_SESSION['cart']));
?>
Javascript
<script>
setInterval(function(){
$.post('get-count-items.php', {}, function (total_items){
$('#total-items').html(total_items);
});
}, '1000');
</script>
HTML
<ul id="basket" class="clearfix">
<li class="top">
<a href="checkout.php" class="top_link">
<span>Items: <span id="total-items"><? echo $basket ?></span></span>
</a>
</li>
</ul>
This javascript will make a request to server at each 1 second.
Second solution (event on button "Add to cart")
add-to-cart.php
<?php
session_start();
// code for add to cart...
die(count($_SESSION['cart']));
?>
Javascript
<script>
$('.add-to-cart').click(function(){
$.post('add-to-cart.php', {}, function (total_items){
$('#total-items').html(items);
});
return false;
});
</script>
HTML
<ul id="basket" class="clearfix">
<li class="top">
<a href="checkout.php" class="top_link">
<span>Items: <span id="total-items"><? echo $basket ?></span></span>
</a>
</li>
</ul>
<a href="" class="add-to-cart">Add to cart</>

having trouble with jquery .context and .index

here is my PHP code to generate a dynamic product table:
while($item = mysqli_fetch_array($result))
{
enter code here
$i++;
$pic = "cartimg/".$item[2];
echo "<div class='prod_box'".$i.">
<div class='center_prod_box'>
<div class='product_title'><a>".$item[1]."</a></div>
<div class='product_img'><a ><img src='".$pic."' alt='' border='0' /></a></div>
<div class='prod_price'><span class='reduce'>".$item[3]."$</span> <a class='price'>".$item[4]."$</a></div>
</div>
<div class='prod_details_tab'> <a class='prod_buy'>Add to Cart</a> <a class='prod_details'>Details</a> </div>
</div>";
}
?>
and here is my jQuery to respond to the clicked event to add the product into the shopping cart
enter code here
cart.addBuyButton(".prod_buy",{
name:'MacBook', // Item name appear on the cart
thumbnail:'media/macbook.jpg', // Thumbnail path of the item (Optional)
price:$("span").index(0), // Cost of the item
shipping:20
// Shipping cost for the item (Optional)
});
prettyPrint();
and here is the query for addBuyButton function
enter code here
self.addBuyButton=function(target,data){$(target).click(function(){self.cart.add(data)
the problem is that, I will have 10 containers, contain 10 products, with the same class name and Id names, and I cant figure out how to read the amount of ".$item[4]."$ if the customer clicked on Add to Cart of different products.
right now the function inserts 12 as the price.
please help me out here, I have surf through many jQuery tutorials but wasn't lucky to find a way.
thanks
Instead of having an addBuyButton for each item on the jQuery I would have just one event listener that handles all of it like this (I personally don't like huge chunks of php in quotes because my IDE doesn't color highlight so pardon my php syntax. You don't have to follow it if you don't like it):
PHP
<div id="products">
<?php while ($item = mysqli_fetch_array($result)) : ?>
<!-- additional code here -->
<div class="product prod_box<?php echo ++i; ?>">
<div class="center_prod_box">
<div class="product_title"><?php echo $item[1] ?></div>
<div class="product_img"><img src="cartimg/<?php echo $item[2] ?>" border="0" /></div>
<div class="prod_price">
<span class="reduce"><?php echo $item[3] ?>$</span>
<a class="price"><?php echo $item[4] ?>$</a>
</div>
</div>
<div class="prod_details_tab">
<a class="prod_buy">Add to Cart</a>
<a class="prod_details">Details</a>
</div>
</div>
<?php endwhile; ?>
</div>
JAVASCRIPT
$('#products').on('click', '.prod_buy', function() {
var product = $(this).closest('.product');
cart.add({
name: product.find('.product_title').first().text(),
thumbnail: product.find('.product_img').first().text(),
price: product.find('.price').first().text(),
shipping: 20
});
});
So that's one event listener for ALL of your prod_buy buttons and it gets the values for the product out of the DOM and adds it to the cart.
If you need any further help add a comment to this post and I'll add more to my answer.

Categories