Shopping Cart Issue in Codeigniter - php

I am trying to create a shopping cart in CodeIgniter, I tried the method mentioned in the documentation and it works perfectly. But when I try to use it in the templetes it did not works. In the documentation, the products are hard coded but I am fetching the products from a database and inserting it using ajax.
My Insert code JS
$(document).ready(function(){
$('.add-to-cart').click(function(){
var product_id=$(this).data("productid");
var product_name=$(this).data("productname");
var product_price=$(this).data("productprice");
var quantity=$('#' + product_id).val();
if(quantity !='' && quantity>0)
{
$.ajax({
url:"<?php echo base_url();?>Cart/insert_product",
method:"POST",
data:{
product_id:product_id,product_name:product_name,product_price:product_price,quantity:quantity
},
success:function(data)
{
alert("Product added into cart");
// alert("Product added into cart and product info is: ID:" + product_id +'\n\r name:' + product_name + '\n\r price:' + product_price + '\n\r quantity:' + quantity);
// $('#' + product_id).val('');
}
});
}
else
{
alert("Please enter quantity");
}
});
});
I test it using the alert to make sure that product is picking when I click "Add To Cart" Button.
My Controller Insert_Product
// my cart
public function insert_product()
{
$data=array(
'id' => $_POST['product_id'],
'qty' => $_POST['quantity'],
'price' => $_POST['product_price'],
'name' => $_POST['product_name']
);
// Inesrting Items into Cart
$this->cart->insert($data);
}
When I click on "Add To Cart" Button it shows me the success message, but I am pretty sure that the product is not inserting into the cart...
Place Where I am trying to access the product.
<!-- cart items -->
<div class="nav-cart-items">
<?php $i = 1;
if(!$this->cart->contents()){echo "no items found in cart";}
foreach ($this->cart->contents() as $items):
var_dump($items);
if(empty($items)){echo "cart is empty";}else{ echo "<h1>cart has data</h1>";?>
<div class="nav-cart-item clearfix">
<h4>Cart Details</h4>
<div class="nav-cart-img">
<a href="#">
<img src="cart_small_1.jpg" alt="">
</a>
</div>
<div class="nav-cart-title">
<a href="#">
<?php echo $items['name'];?>
</a>
<div class="nav-cart-price">
<span><?php echo $items['qty'];?> x</span>
<span><?php echo $this->cart->format_number($items['price']);?></span>
</div>
</div>
<div class="nav-cart-remove">
<i class="ui-close"></i>
</div>
</div>
<?php $i++; ?>
<?php } endforeach;?>
</div> <!-- end cart items -->
I am echoing the message "no items found in cart" if there are no items in the cart, and it shows me that. I also try this code
<?php
ob_start();
var_dump($this->cart->contents());
$result = ob_get_contents();
?>
to test the contents and it shows me the output :
F:\xampp\htdocs\cart\application\views\single_item.php:3:
array (size=0)
empty
As for I think that I am doing something wrong, but don't know where...
Please also suggest a cart solution in CodeIgniter, as in the documentation the Cart library is depreciated.
Any help regarding this will be highly appreciated.

PHP scripts, namely: <?php echo base_url();?> will not execute in .js files. Hence how in the comments your url is showing up in dev tools verbatim.
You could move the contents of the js file to your header template so it resides in a view. Or you could just define the base_url() yourself in js as a var (var base_url = 'http://somesite.com/';) and use it like base_url or do something like:
url:"/cart/insert_product" assuming that cart is not in a subdir of controllers

Why do you used $data in Controller.
If i am not wrong then you are sending values separately , not in " $data multiassociative array "
$data=array(
array(
'id' => $_POST['product_id'],
'qty' => $_POST['quantity'],
'price' => $_POST['product_price'],
'name' => $_POST['product_name'])
);
try to remove $data=array() and used following code:
$id = $this->input->post('product_id');
$quantity = $this->input->post('quantity');
$product_price = $this->input->post('product_price');
$product_name = $this->input->post('product_name');

I usually get the post data and insert into the table in this way:
$productId = $this->security->xss_clean($this->input->post('product_id'));
$quantity = $this->security->xss_clean($this->input->post('quantity'));
$productPrice = $this->security->xss_clean($this->input->post('product_price'));
$productName = $this->security->xss_clean($this->input->post('product_name'));
$data = array(
"id" => $productId,
"qty" => $quantity,
"price" => $productPrice,
"name" => $productName
);
$this->cart->insert($data);

Related

How to implement Polylang functions in my theme's php file?

I am translating a site with Polylang that was created by someone else using a custom theme. Some strings couldn't be found by Polylang, so I need to manually register them for Polylang to see. Now, some of the strings were pretty easy to translate using __():
$args = shortcode_atts([
'link' => 'service',
'text' => __('Get the code now!'),
'title' => __('Code'),
'id' => '',
'class' => 'width300',
], $args, '' );
But I don't know how to translate strings in these scenarios:
$popup = do_shortcode('[popup id="110" link="vacancy" class="" title="Code" text="Get the code now!!" ]');
Or here words Details, Close
$script = '
<script>
$(document).ready(function(){
btn = $(\'.add_vakan_info\'),
popup = $(\'.add_popup\');
btn.click(function(){
var block_info = $(this).closest(\'.vacancy\').find(\'.content\');
if(block_info.hasClass(\'active\')){
block_info.removeClass(\'active\');
$(this).html(\'Details <i class="las la-angle-down"></i>\');
}else{
block_info.addClass(\'active\');
$(this).html(\'Close <i class="las la-angle-up"></i>\');
}
})
popup.click(function(){
var text = $(this).closest(\'.vacancy\').find(\'.title\').text(),
form = $(\'.vacancy_name\');
form.val(text);
})
FileInput();
})
</script>';
Or
else:
$html = '<h4>No vacancy </h4>';
Or
<div class="content col">
<div class="sp-20"></div>
<h4>Description</h4>
'.$description.'
<div class="sp-20"></div>
<h4>Requirements</h4>
'.$requirements.'
<div class="sp-20"></div>
<h4>Conditions</h4>
'.$conditions.'
You need to use Polylang's string translations API.
Step 1. Registering your strings.
// functions.php
add_action('init', function () {
$strings = array(
'no-vacancy' => 'No vacancy',
// Add more strings here...
);
foreach ($strings as $key => $string) {
pll_register_string('theme-' . $key, $string, 'Hardcoded Theme Strings');
}
});
Now these strings will appear in Languages > String translations menu, under the dropdown for Hardcoded Theme Strings
Step 2. Translate the Strings
else:
$html = '<h4>' . pll_e( 'No vacancy' ) . '</h4>';
The value you enter on the back-end in the string translations menu will be used when translating this value.

WordPress wp_get_recent_posts(); how to make it AJAX [duplicate]

To begin with, I think this is a longshot but hopefully there is someone out there that can help.
To explain the current situation, at the moment I have a custom plugin that grabs various bits of information about a user and their 4 most recent posts.
I am also using the WPBook plugin (so this is on facebook and just just a typical wordpress site)
Ok, so here is my code that is grabbing the 4 most recent posts for a user:
// The Query
query_posts('posts_per_page=4&author='.$blogger_id);
// set $more to 0 in order to only get the first part of the post
global $more;
$more = 0;
// the Loop
while (have_posts()) : the_post();
?>
<div class="oe-grid-box">
<a href="<?php the_permalink() ?>" <?php the_title()?></a>
<div class="oe-grid-box-content">
<?php echo the_content( '...' ); ?>
</div>
<div class="oe-grid-pic">
<a href="<?php the_permalink() ?>">
<span class="<?php echo strtolower($category); ?>"></span>
</a>
<?php echo $image; ?>
</div>
</div>
<?php
endwhile;
// Reset Query
wp_reset_query();
?>
<div id="load-more">Load More</div>
I tried following this tutorial but instead of a separate plugin, placing the code in my existing plugin but now my page won't load:
http://www.problogdesign.com/wordpress/load-next-wordpress-posts-with-ajax/
Ideally, want I want to is when the load more button is clicked, fetch 4 more posts and show them.
If anybody could help I'd really appreciate it.
UPDATE
Ok,
so far I've added in my jQuery to call the php file which should hopefully return posts but it doesn't work.
I am thinking that it's because it doesn't know what the WordPress functions are?
If I put a simple echo in my load more script, the jQuery success function shows an alert to say it worked, but if I start doing WordPress stuff I get an internal server error, this is the code in the load-more.php file:
// load wordpress into template
$path = $_SERVER['DOCUMENT_ROOT'];
define('WP_USE_THEMES', false);
require($path .'/wp-load.php');
$blogger_id = $_GET['id'];
$firstname = $_GET['firstname'];
$surname = $_GET['surname'];
$posts = $_GET['posts'] + 4;
$category = $_GET['category'];
$image = $_GET['image'];
// The Query
query_posts('paged='.get_query_var('paged').'&posts_per_page=' . $posts . '&author='.$blogger_id);
// set $more to 0 in order to only get the first part of the post
global $more;
$more = 0;
// then the same loop as in my page that is making the ajax call.
After a lot of effort, I found the answer, here is a solution for those stuck in the same position.
This goes in your plugin page where you are getting posts for a user etc.
<script type="text/javascript">
$(document).ready(function() {
posts = 8;
author_posts = parseInt(<?php echo $author_posts; ?>);
$("#link_selector").click(function() {
// alert('posts - ' + posts + 'author posts - ' + author_posts);
if ((posts - author_posts) > 3) {
$("#link_selector").text('No more posts!');
}
else {
var category = '<?php echo strtolower($category); ?>';
var id = '<?php echo $blogger_id; ?>';
var firstname = '<?php echo $firstname; ?>';
var surname = '<?php echo $surname; ?>';
// alert(posts + category + id + firstname + surname);
$.ajax({
type: "GET",
url: "/wordpress/wp-admin/admin-ajax.php",
dataType: 'html',
data: ({ action: 'loadMore', id: id, firstname: firstname, surname: surname, posts: posts, category: category}),
success: function(data){
$('#ajax_results_container').hide().fadeIn('slow').html(data);
posts = posts + 4;
if ((posts - author_posts) > 3) {
$("#link_selector").text('No more posts!');
}
}
});
}
});
});
</script>
Then in your theme's functions.php, put your function to do your ajax request:
function loadMore() {
$blogger_id = $_GET['id'];
// get your $_GET variables sorted out
// setup your query to get what you want
query_posts('posts_per_page=' . $posts . '&author='.$blogger_id);
// initialsise your output
$output = '';
// the Loop
while (have_posts()) : the_post();
// define the structure of your posts markup
endwhile;
// Reset Query
wp_reset_query();
die($output);
}
Then, just after the closing of your function, put in the action hooks
add_action('wp_ajax_loadMore', 'loadMore');
add_action('wp_ajax_nopriv_loadMore', 'loadMore'); // not really needed
That's it!

Wordpress - how can I fetch more posts via AJAX?

To begin with, I think this is a longshot but hopefully there is someone out there that can help.
To explain the current situation, at the moment I have a custom plugin that grabs various bits of information about a user and their 4 most recent posts.
I am also using the WPBook plugin (so this is on facebook and just just a typical wordpress site)
Ok, so here is my code that is grabbing the 4 most recent posts for a user:
// The Query
query_posts('posts_per_page=4&author='.$blogger_id);
// set $more to 0 in order to only get the first part of the post
global $more;
$more = 0;
// the Loop
while (have_posts()) : the_post();
?>
<div class="oe-grid-box">
<a href="<?php the_permalink() ?>" <?php the_title()?></a>
<div class="oe-grid-box-content">
<?php echo the_content( '...' ); ?>
</div>
<div class="oe-grid-pic">
<a href="<?php the_permalink() ?>">
<span class="<?php echo strtolower($category); ?>"></span>
</a>
<?php echo $image; ?>
</div>
</div>
<?php
endwhile;
// Reset Query
wp_reset_query();
?>
<div id="load-more">Load More</div>
I tried following this tutorial but instead of a separate plugin, placing the code in my existing plugin but now my page won't load:
http://www.problogdesign.com/wordpress/load-next-wordpress-posts-with-ajax/
Ideally, want I want to is when the load more button is clicked, fetch 4 more posts and show them.
If anybody could help I'd really appreciate it.
UPDATE
Ok,
so far I've added in my jQuery to call the php file which should hopefully return posts but it doesn't work.
I am thinking that it's because it doesn't know what the WordPress functions are?
If I put a simple echo in my load more script, the jQuery success function shows an alert to say it worked, but if I start doing WordPress stuff I get an internal server error, this is the code in the load-more.php file:
// load wordpress into template
$path = $_SERVER['DOCUMENT_ROOT'];
define('WP_USE_THEMES', false);
require($path .'/wp-load.php');
$blogger_id = $_GET['id'];
$firstname = $_GET['firstname'];
$surname = $_GET['surname'];
$posts = $_GET['posts'] + 4;
$category = $_GET['category'];
$image = $_GET['image'];
// The Query
query_posts('paged='.get_query_var('paged').'&posts_per_page=' . $posts . '&author='.$blogger_id);
// set $more to 0 in order to only get the first part of the post
global $more;
$more = 0;
// then the same loop as in my page that is making the ajax call.
After a lot of effort, I found the answer, here is a solution for those stuck in the same position.
This goes in your plugin page where you are getting posts for a user etc.
<script type="text/javascript">
$(document).ready(function() {
posts = 8;
author_posts = parseInt(<?php echo $author_posts; ?>);
$("#link_selector").click(function() {
// alert('posts - ' + posts + 'author posts - ' + author_posts);
if ((posts - author_posts) > 3) {
$("#link_selector").text('No more posts!');
}
else {
var category = '<?php echo strtolower($category); ?>';
var id = '<?php echo $blogger_id; ?>';
var firstname = '<?php echo $firstname; ?>';
var surname = '<?php echo $surname; ?>';
// alert(posts + category + id + firstname + surname);
$.ajax({
type: "GET",
url: "/wordpress/wp-admin/admin-ajax.php",
dataType: 'html',
data: ({ action: 'loadMore', id: id, firstname: firstname, surname: surname, posts: posts, category: category}),
success: function(data){
$('#ajax_results_container').hide().fadeIn('slow').html(data);
posts = posts + 4;
if ((posts - author_posts) > 3) {
$("#link_selector").text('No more posts!');
}
}
});
}
});
});
</script>
Then in your theme's functions.php, put your function to do your ajax request:
function loadMore() {
$blogger_id = $_GET['id'];
// get your $_GET variables sorted out
// setup your query to get what you want
query_posts('posts_per_page=' . $posts . '&author='.$blogger_id);
// initialsise your output
$output = '';
// the Loop
while (have_posts()) : the_post();
// define the structure of your posts markup
endwhile;
// Reset Query
wp_reset_query();
die($output);
}
Then, just after the closing of your function, put in the action hooks
add_action('wp_ajax_loadMore', 'loadMore');
add_action('wp_ajax_nopriv_loadMore', 'loadMore'); // not really needed
That's it!

Shopping cart output

I have an online store. A products page that allows the user to view a product and add it to the basket. It is added to the basket by clicking "Add to basket" button.
When a user clicks "add to basket", the script redirects them to the basket page and adds the product to the basket.
My question is, how do I print the basket output on the "basket.php" page? How do I pass the session content into variables to be printed?
Thank you.
"products" table in the database:
id int(11), name varchar(255), price int(11)
product.php
...
<form id="basket" name="basket" method="post" action="basket.php">
<input type="hidden" name="p_id" value="<?php echo $id; ?>"/>
<input type="submit" name="submit" value="Add to basket"/>
</form>
...
basket.php
<?php
//add product to cart with product ID passed from previous script
if (isset($_POST["p_id"]))
{
$p_id = $_POST["p_id"];
$q = mysql_query("SELECT * FROM products WHERE id='$p_id'");
$is = mysql_fetch_row($q); $is = $is[0];
$result = "";
while($row = mysql_fetch_array($q)) {
$name = $row["name"];
$price = $row["price"];
$info = $row["info"];
}
$result .= $name .= $price .= $info;
//$_SESSION['p_id'] contains product IDs
//$_SESSION['counts'] contains item quantities
// ($_SESSION['counts'][$i] corresponds to $_SESSION['p_id'][$i])
//$_SESSION['p_id'][$i] == 0 means $i-element is 'empty' (does not refer to any product)
if (!isset($_SESSION["p_id"]))
{
$_SESSION["p_id"] = array();
$_SESSION["counts"] = array();
}
//check for current product in visitor's shopping cart content
$i=0;
while ($i<count($_SESSION["p_id"]) && $_SESSION["p_id"][$i] != $_POST["p_id"]) $i++;
if ($i < count($_SESSION["p_id"])) //increase current product's item quantity
{
$_SESSION["counts"][$i]++;
}
else //no such product in the cart - add it
{
$_SESSION["p_id"][] = $_POST["p_id"];
$_SESSION["counts"][] = 1;
}
}
?>
<div>
<?php echo $result ?>
</div>
create ajax request on button click, to basket.php with GET veriable todo='add_to_basket', witch you will handle in basket.php
HTML of you add product button or link
Your count of product, you will get by Jquery, just type your count html selectors with IDs
<select id="count_<?=$YOUR_PRODUCT_ID?>"></select>
function add_to_basket(product_id){
var product = {};
product['prod_id'] = product_id;
//here you get count of current product
product['count'] = $("count_"+product_id).val();
$.ajax({
type: "GET",
url: "your_domain/basket.php?todo=add_to_basket",
data: "product",
success:function () {
}
});
}
on your basket.php you handle this request like that
if ($GET['todo'] == "add_to_basket"){
// here you add your data(witch comes from ajax) to SESSION
$_SESSION['basket'] [$GET['prod_id']] = $GET['count'];
return true;
}
then when user click on basket image you redirect him to basket page, in wich you display all product in session
<?foreach ($_SESSION['basket'] as $item){?>
// here you get product info by product id from your database and product count from $_SESSION print it to view , price wille be count*price
<?}?>

Help creating an ajax method to add to basket

I have the following code that I want to turn into a silent ajax function so the page does not need to refresh, when a user adds a product the basket.
Currently I have the following code,
PHP
function showProduct($productId)
{
if (!$productId > 0)
{
redirect('home');
}
$this->load->model('Product_model');
$this->load->model('Checkout_model');
$this->data['items'] = $this->Checkout_model->GetProducts();
// Check to see if the Add To Bag button has been clicked.
if ($this->input->post('btnAddToBag'))
{
$derivativeId = $this->input->post('selDerivative-1');
$quantity = $this->input->post('selQuantity');
$derivative = $this->Product_model->GetProducts(array('pdId' => $derivativeId), 'small');
// Add item to shopping bag.
$attributes = $this->Product_model->GetProductDerivatives(array('pdId' => $derivativeId));
$this->Checkout_model->AddProduct($derivative, $attributes, $quantity);
$this->data['message'] = 'Item added to Shopping Bag.';
// Update Delivery Price
$this->Checkout_model->updateDelivery(49);
//get the bag details
}
$this->data["product_details"] = $this->Product_model->GetProducts(array('productId' => $productId, 'pdOrdering' => 1), 'detail');
$this->data['product_images'] = $this->Product_model->GetProductImages(array('productId' => $productId, 'ipPosition' => 'detail'));
//send the new filename to the view
//die(print_r($this->data['product_images']));
//die(print_r($this->data['sliderUrl']));
// Retrieve images for initial derivative.
$this->load->model('Attribute_model');
$this->data['derivative_images'] = $this->Attribute_model->GetAttributeValues(array('pdavProductDerivativeId' => $this->data["product_details"]->pdId), 'small', TRUE);;
$derivatives = $this->Product_model->GetProductDerivatives(array('pdProductId' => $productId));
$this->data["product_derivatives"] = $derivatives['derivatives'];
$this->data["product_attribute_names"] = $derivatives['attributeNames'];
// Retrieve details of this range.
$subRangeId = $this->data["product_details"]->productRangeId;
$this->data["sub_range_details"] = $this->Range_model->GetRanges(array('range.rangeId' => $subRangeId));
$rangeId = $this->data["sub_range_details"]->rangeParentId;
$this->data['active_menu_item'] = "subrange";
if ($rangeId == 0)
{
$rangeId = $subRangeId;
$this->data['active_menu_item'] = "full";
}
$this->data["range_details"] = $this->Range_model->GetRanges(array('range.rangeId' => $rangeId, 'range.rangeParentId' => 0), 'navigation');
if ($rangeId != $subRangeId)
$this->template->set_breadcrumb($this->data["range_details"]->rangeTitle, $this->data["range_details"]->rangePath);
//$this->data["rangePath"] = $this->uri->segment(1).'/';
$this->data["rangeId"] = $rangeId;
$this->data["subRangeId"] = $subRangeId;
// Retrieve list of sub ranges in this range.
$this->data["sub_ranges"] = $this->Range_model->GetRanges(array('range.rangeParentId' => $rangeId));
$this->data["individual_products"] = $this->Product_model->GetProducts(array('psiParentId' => $productId), 'small');
$this->data["related_products"] = $this->Product_model->GetRelatedProducts(array('product_related.prProductId' => $productId, 'ordering' => 'productActualPrice'), 'small');
$this->data['bestsellers'] = $this->Product_model->GetProducts(array('productBestSeller' => '1', 'range.rangeParentId' => $rangeId, 'ordering' => 'range.rangeOrder'), 'small');
$this->data["multibuy"] = $this->Product_model->GetProducts(array('psi.psiChildId' => $productId, 'productSavingType' => 'multi-buy'));
//$this->load->view('collection', $data);
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']=="XMLHttpRequest") {
$this->template
->build('product/quickview', $this->data); // views/product/product.php
}else{
$this->template
->set_layout('default') // application/views/layouts/default.php
->title(($this->data["product_details"]->productMetadataTitle != '' ? $this->data["product_details"]->productMetadataTitle : $this->data["product_details"]->productTitle))
->set_metadata('keywords', $this->data["product_details"]->productMetadataKeywords, 'meta')
->set_metadata('description', ($this->data["product_details"]->productMetadataDescription != '' ? $this->data["product_details"]->productMetadataDescription : $this->data["product_details"]->productShortDesc), 'meta')
->set_metadata('', site_url('assets/js/product.js'), 'js')
->set_metadata('', site_url('assets/js/jquery.nivo.slider.js'), 'js')
->set_metadata('', site_url('assets/js/slider.js'), 'js')
->set_partial('left-nav', 'blocks/ranges_sub_menu') // application/views/blocks/ranges_sub_menu.php
->set_breadcrumb($this->data["sub_range_details"]->rangeTitle, $this->data["sub_range_details"]->fullRangePath)
->set_breadcrumb($this->data["product_details"]->productTitle, $this->data["product_details"]->fullProductPath)
->build('product/product', $this->data); // views/product/product.php
}
}
HTML FORM
<?php echo form_open(current_url(), array('id' => 'frmProducts'), array('submitted' => '1')); ?>
<div class="formRow">
<label for="rattanType"><?php echo $product_attribute_names; ?> </label><br />
<?php
$options = array();
foreach ($product_derivatives as $derivative) :
$options[$derivative['derivativeId']] = $derivative['attributeValues'];
endforeach;
?>
<?php echo form_dropdown('selDerivative-1', $options, $product_details->pdId, 'class="select clear" id="selDerivative-1"'); ?>
</div>
<?php if (count($individual_products) > 0) : ?>
<div class="formRow">
<label for="itemType">Item</label><br />
<select class="select clear" id="selURL-1" name="selURL-1">
<option value="<?php echo current_url(); ?>">Full Set</option>
<?php foreach ($individual_products as $product) : ?>
<option value="<?php echo site_url($product->fullProductPath); ?>"><?php echo $product->productTitle; ?> - £<?php echo ($product->productSavingType != 'none' ? $product->productSavingPrice : $product->productPrice); ?></option>
<?php endforeach; ?>
</select>
<input id="btnGo-1" name="btnGo-1" type="submit" value="GO" />
</div>
<?php endif; ?>
<div class="formRow">
<label for="addQty">Quantity</label><br />
<?php
$options = array();
for ($i = 1; $i < 10; $i++) :
$options[$i] = $i;
endfor;
?>
<?php echo form_dropdown('selQuantity', $options, '1', 'class="small select clear"'); ?>
</div>
<input type="submit" value="add to bag" name="btnAddToBag" id="btnAddToBag" />
<?php echo form_close(); ?>
// Reset action of form when add to bag button is clicked.
$("#btnAddToBag").click(function () {
$("#frmProducts").attr("action","<?php echo current_url(); ?>");
});
How can I change this code that it makes an ajax request and updates the cart without refreshing the page?
You need to split off your basket display into another file so that you can use:
$.load()
Example basic usage:
$('#ShoppingCart').load('Cart.php', function() {
alert('Load was performed.');
});
<div id="ShoppingCart"> </div>
Or a little more like youve currently got it set up:
$("#btnAddToBag").click(function () {
$('#ShoppingCart').load('Cart.php', function() {
alert('Load was performed.');
});
});
Edit:
Re-reading your post, do you not want the cart to update atall?
In that case you would need to use $.post()
See: http://api.jquery.com/jQuery.post/
I have already answered a question for JSP/SERVLET here this one should work by changing the URL to a PHP page...
You need to make an XML Http request to the Servlet for that you need to make a XML Http object in the javascript in your HTML/PHP page
var myxmlhttpobj=new GetXmlHttpObject();
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
Now you need to make a request to the PHP from the javascript
var url="urlofPHPpage";
var para="parmeter1=value1&parameter2=valu2;
myxmlhttpobj.open("POST",url,true);
myxmlhttpobj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
myxmlhttpobj.setRequestHeader("Content-length", para.length);
myxmlhttpobj.setRequestHeader("Connection", "close");
myxmlhttpobj.onreadystatechange=ajaxComplete;
myxmlhttpobj.send(para);
At the PHP page you need to process the result and sent it back as string:
When the request comes back the myxmlhttpobj.onreadystatechange=ajaxComplete; will be called
function ajaxComplete(){
if(myxmlhttpobj.readyState==4){
///Display the result on the HTML/PHP Page BASKET
}
}
That should help...
Also have a look at jQuery Ajax API.

Categories