Solved

Update page information without refreshing

Posted on 2008-10-20
4
346 Views
Last Modified: 2013-12-13
I want to show the number of items in my shopping cart and update it at the moment customer add another item in to the cart without refreshing the page. I am using the mysql database to keep all the everything works great except the total number of items in the shoping cart update only when I refresh the page (read the data from the database). How can I solve this problem without using iframes.
0
Comment
Question by:ppeiris
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
aldanch earned 500 total points
Comment Utility
This sounds like a job for AJAX.

Say you have a SPAN or DIV with the ID of "totalitems" (this can be a textbox as well). You could use AJAX to update it "behind the scenes" like this.


=== Javascript ===

var ie5 = (document.getElementById && document.all); 

var ns6 = (document.getElementById && !document.all); 

var ua = navigator.userAgent.toLowerCase();

var isapple = (ua.indexOf('applewebkit') != -1 ? 1 : 0);
 

function createRequestObject() {

	var ro;

	var browser = navigator.appName;

	if(browser == "Microsoft Internet Explorer") {

		ro = new ActiveXObject("Microsoft.XMLHTTP");

	}

	else {

		ro = new XMLHttpRequest();

	}

	return ro;

}
 

function getTotalItems() {

	var ge = createRequestObject();

	var rn = Math.floor(Math.random() * 1000); // Don't get cached data

	ge.open('get', 'aj_gettotalitems.php?rn=' + rn);

	ge.onreadystatechange = function() {

		if(ge.readyState == 4) {

			// Use this if you are updating a SPAN or DIV

			document.getElementById('totalitems').innerHTML = ge.responseText;

			// Use this if you are updating a textbox

			//document.getElementById('totalitems').value = ge.responseText;

			ge = null;

		}

	}

	ge.send(null);

}
 

=== PHP ===

The PHP script called in the function above (aj_gettotalitems.php) should return the number of items in the cart. This is where it reads the database, does a little magic, and spits out the total thus far.
 

=== HTML ===

Your HTML should include an "onclick", "onchange", or whatever to call the getTotalItems() function. I imagine this would be the button or link the user clicks to add something to their cart.
 
 

More info on AJAX can be found here:

http://www.xul.fr/en-xml-ajax.html
 

Or on Google:

http://www.google.com/search?hl=en&q=ajax+tutorials&btnG=Google+Search&aq=f&oq=

Open in new window

0
 
LVL 3

Expert Comment

by:sbickerstaff
Comment Utility
you mite also want to check out this site http://jibbering.com/2002/4/httprequest.html (about 2/3 way down page 'Calling a server-side Script without refreshing the page')

I also came across this in the past: http://developer.apple.com/internet/webcontent/xmlhttpreq.html

I use to have a great link but lost it :(

Let me know if either of these links are of any use to you
0
 
LVL 1

Author Comment

by:ppeiris
Comment Utility
The AJAX script is working for me but still have a small problem. I being a floating div tag (Using thickbox) to add items in to the shopping cart, when it is done you can close the thickbox but i don't know how to call a javascript function in thickbox that update the contents in the main window ?

Thank you

~Prabath
0
 
LVL 8

Expert Comment

by:aldanch
Comment Utility
What method are you using to close the window (box)? I have never used thickbox but I would look for the code that is executed when the window is closed and add a line to call the function there.

If the thickbox is closed via a link chances are it's Javascript performing the close. That being the case, have it call the function at that time.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now