Hello i have an app where i ma trying to sell some stuff.
basically there also exists a laravel app remotely which serves my HTML and CSS app
e.g
function fetchcart() {
fetch('https://#################.com/cart')
.then(response => response.text())
.then(htmlContent => {
const container = document.getElementById('cart');
// Create a temporary div element
const tempDiv = document.createElement('div');
tempDiv.innerHTML = htmlContent;
// Extract and append scripts to the container
const scripts = tempDiv.getElementsByTagName('script');
for (let i = 0; i < scripts.length; i++) {
const script = document.createElement('script');
script.innerHTML = scripts[i].innerHTML;
container.appendChild(script);
}
// Append the rest of the HTML content
container.innerHTML += tempDiv.innerHTML;
})
.catch(error => {
console.error('Error fetching view:', error);
});
}
// Fetch the HTML initially
fetchcart();
should load content directly into
<div id="cart"></div>
in <div id="cart"></div>
we have products and a cart div
<div id="transcart"></div>
<div class="col-md-11 p-0 mt-3">
<div class="row row-cols-2 row-cols-md-3 row-cols-xl-4 g-2 g-md-3">
@foreach($products as $d1)
<div class="col mb-2">
<div class="card h-100 p-2">
@if ($d1->file == '')
<img src="{{ URL::asset('uploads/not.png') }}" class="card-img-top min-height-lg min-height-sm rounded" alt="...">
@else
<img src="###############.com/images/{{$d1->file}}" class="card-img-top min-height-lg min-height-sm rounded" alt="...">
@endif
<div class="position-absolute top-0 end-0 bg-primary rounded text-white p-1 px-2 m-3" style="font-size:11px !important">₦{{ number_format($d1->price, 2) }}</div>
<div class="card-body text-center text-md-start">
<h5 class="card-title mb-0">{{$d1->title}}</h5>
</div>
<div class="card-footer border-0 bg-white">
<div class="row align-items-center">
<div class="col-md-6">
<p style="display:none" class="mb-md-0 fs-6 mb-0 text-center text-md-start">
Price: <span class="text-primary">₦{{ number_format($d1->price, 2) }}</span>
</p>
</div>
<div class="col-md-6">
@if($d1->product_quantity >= '1')
<a href="{{ route('addbook.to.cart', $d1->id) }}" class="btn btn-primary btn-sm white-text white-text-hover px-0 w-100 choice" role="button">Add to cart</a>
@elseif($d1->product_quantity < '1')
<a href="#" class="btn btn-primary btn-sm white-text white-text-hover px-0 w-100" role="button">Out of stock</a>
@endif
</div>
</div>
</div>
</div>
</div>
@endforeach
</div>
</div>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
<script>
function wole() {
console.log('Before AJAX request');
$.ajax({
url: "{!! url('/market') !!}",
method: "get",
dataType: "html",
success: function (response) {
console.log('AJAX success, response:', response);
$("#transcart").html(response);
// Other logic...
},
error: function (xhr, status, error) {
console.error('AJAX error:', error);
}
});
console.log('After AJAX request');
}
$(document).ready(function () {
$(".choice").click(function (e) {
e.preventDefault();
$.ajax({
url: $(this).attr("href"),
method: "get",
dataType: "html",
success: function (response) {
// $("#vote").html(response);
alert('suce');
wole();
}
});
});
});
wole();
</script>
this is the page that served dynamically into my app. which works.
infact when I add to cart I log the cart session created
[2023-12-05 04:49:40] production.INFO: New item added. Cart content: {"355":{"name":"testing product for Stephanie","quantity":1,"price":"2000","productid":355,"image":"chelsea.jpg"}}
just to be sure that the session duly created.
but the my which should loa cart contents keeps showing empty.
what could be wrong?
this is the code that loads my cart contents
@php $total = 0
@endphp
@if(session('cart'))
@foreach(session('cart') as $id => $details)
@php $total += $details['price'] * $details['quantity'] @endphp
<li style="margin-bottom : 10px" rowId="{{ $id }}">
<div class="row m-auto align-items-center">
<div class="col-md-4 col-3 px-0">
<div class="bg-cover rounded-2 small-list-image" style="background: url({{ URL::asset('public/uploads/'. $details['image']) }});">
</div>
</div>
<div class="col-md-5 col-6">
<p class="mb-0">{{ $details['name'] }}</p>
<div class="input-group input-group-sm my-1">
<span class="input-group-text">Quantity</span>
<input readonly type="text" id="changer" value="{{ $details['quantity'] }}" class="form-control text-center changer" aria-label="quantity"><br />
</div>
<div>
<a href="{{ url('/changer/'.$id.'/'.'add') }}" class="changer"><i class="material-icons-outlined">add_circle_outline</i></a>
<a href="{{ url('/changer/'.$id.'/'.'sub') }}" class="changer"><i class="material-icons-outlined">remove_circle_outline</i></a>
</div>
<a class="btn btn-link p-0 m-0 text-decoration-none delete-product">Remove</a>
</div>
<div class="col-md-3 col-3">
<p class="text-primary">₦@php echo number_format($details['price'], 2) @endphp</p>
</div>
</div>
</li>
@endforeach
@endif
@if($total != '0')<br />
@php $total @endphp
<script>
$('#move').unbind('click')
</script>
@endif
@if($total == '0')<br />
<h3><strong>Cart is empty</strong></h3>
i have also test my link https://#################.com/cart using an iframe its the same issue.
but when I test directly https://#################.com/cart on a browser everything is fine. i need this to work dynamically or using an iframe. preferable dynamically.
please help, my cart keeps returning empty



