Sonny
Sonny

Reputation: 23

Is there a way to just refresh a PHP include file without refreshing the entire page?

I have a products page with a link "Add to Cart", when this is clicked it sends an ajax request via jquery to a page called cart_process.php and adds the item to the cart.

The cart works via a cookie called cart:

$_COOKIE['cart'];

which stores the mysql record ids of the added products along with a hyphen separator like so:

11-36-33-

To display the number of items in the cart I simply do a count of the number of hyphens in the cookie:

substr_count($_COOKIE['cart'], '-');

and I echo this in the header file which is called header.php and is "included" in the cart page.

Now the problem is, when the user adds an item to cart the cart_process.php file alerts a message "Item added successfully" but the number of items in the cart that is being shown from header.php is still not updated.

This is because the header.php file is not refreshed, the only way I see how to make it refresh is to refresh the entire product page. But that defeats the beauty of ajax.

So my question is, is there a way to refresh just one of the include files on a page without refreshing the page itself. In this case header.php on the products page?

Upvotes: 2

Views: 2678

Answers (4)

Marc B
Marc B

Reputation: 360762

Instead of just returning a simple true value or whatever from your ajax-handling PHP script, why not return a JSON object? Part of that object can be the total number of items now in the cart, which your client-side script can insert into the DOM wherever you're outputting the cart count.

When you get right down to it, a shopping cart is the absolutely most trivial thing to implement. It's the checkout/charging/inventory stuff that comes afterwards that's complicated:

<?php
if ($_SERVER['REQUEST_METHOD'] = 'POST') {
     $itemID = $_POST['itemID'];
     $quantity = $_POST['quantity'];

     $_SESSION['cart'][$itemID] = $quantity;

     $status['error'] = false;
     $status['items_in_cart'] = count($_SESSION['cart']);

     return(json_encode($status));
}

And client-side you'd have somewhere in your page:

 <div id="cart_item_count"></div>

and do

 $('#cart_item_count').html(response['items_in_cart']);

Upvotes: 0

Richard Harrison
Richard Harrison

Reputation: 19403

Modify the DOM directly; I tend to use classes on elements to identify them so I can do

<span class='total_cart_quantity'>$value</span>

Then in the Ajax callback in the JS

$(".total_cart_quantity").html(new_quantity)

Mostly I tend to return JSON back from my Ajax routines containing a list of elements that need to be updated, but it is sufficient to simply echo the new count from the PHP and grab that and display it. The beauty of identifying using by adding a class to the HTML is that you can get all of the values on the page updated in one hit, without needing to know where they are by simply adding the class.

The examples above uses JQuery; but the concept will work in most good quality scripting languages.

Upvotes: 2

Josh
Josh

Reputation: 11070

What you'll want to do is update the header using JavaScript. Make the cart_process.php page return either the new contents for the header, or just the new contents for the <div>, <p>, or whatever other tag the cart contents is in. Then, using jQuery, update the contents of that element with the new HTML returned by the server.

There's no other way to just refresh the header than using AJAX. If the above doesn't fit into your code structure easily, you could make a second AJAX request after the item is added to the cart to a page which just returns the contents of header.php, and use jQuery to update the header with that new content.

Upvotes: 0

orlp
orlp

Reputation: 117771

In PHP, if you set a cookie, but you are still in the same session, you need to explicitly define it in the $_COOKIE array like this:

setcookie("TestCookie", "test");
$_COOKIE["TestCookie"] = "test";

Yes, I learned this the hard way.

Upvotes: 0

Related Questions