![]() |
![]() |
Web Design Information |
|
![]() |
![]() |
PHP On-The-Fly!Dennis Pallett Introduction PHP can be used for a lot of different things, and is one of the most powerful scripting languages available on the web. Not to mention its extremely cheap and widely used. However, one thing that PHP is lacking, and in fact most scripting languages are, is a way to update pages in real-time, without having to reload a page or submit a form. The internet wasnt made for this. The web browser closes the connection with the web server as soon as it has received all the data. This means that after this no more data can be exchanged. What if you want to do an update though If youre building a PHP application e.g. a high-quality content management system, then itd be ideal if it worked almost like a native Windows/Linux application. But that requires real-time updates. Something that isnt possible, or so you would think. A good example of an application that works in almost real-time is Googles GMail http://gmail.google.com. Everything is JavaScript powered, and its very powerful and dynamic. In fact, this is one of the biggest selling-points of GMail. What if you could have this in your own PHP websites as well Guess what, Im going to show you in this article. How does it work If you want to execute a PHP script, you need to reload a page, submit a form, or something similar. Basically, a new connection to the server needs to be opened, and this means that the browser goes to a new page, losing the previous page. For a long while now, web developers have been using tricks to get around this, like using a 1x1 iframe, where a new PHP page is loaded, but this is far from ideal. Now, there is a new way of executing a PHP script without having to reload the page. The basis behind this new way is a JavaScript component called the XML HTTP Request Object. See http://jibbering.com/2002/4/httprequest.html for more information about the component. It is supported in all major browsers Internet Explorer 5.5+, Safari, Mozilla/Firefox and Opera 7.6+. With this object and some custom JavaScript functions, you can create some rather impressive PHP applications. Lets look at a first example, which dynamically updates the date/time. Example 1 First, copy the code below and save it in a file called script.js: var xmlhttp=false; /*@cc_on @*/ /*@if @_jscript_version >= 5 // JScript gives us Conditional compilation, we can cope with old IE versions. // and security blocked creation of the objects. try { xmlhttp = new ActiveXObject"Msxml2.XMLHTTP"; } catch e { try { xmlhttp = new ActiveXObject"Microsoft.XMLHTTP"; } catch E { xmlhttp = false; } } @end @*/ if !xmlhttp && typeof XMLHttpRequest!=undefined { xmlhttp = new XMLHttpRequest; } function loadFragmentInToElementfragment_url, element_id { var element = document.getElementByIdelement_id; element.innerHTML = <em>Loading ...</em>; xmlhttp.open"GET", fragment_url; xmlhttp.onreadystatechange = function { if xmlhttp.readyState == 4 && xmlhttp.status == 200 { element.innerHTML = xmlhttp.responseText; } } xmlhttp.sendnull; } Then copy the code below, and paste it in a file called server1.php: <php echo date"l dS of F Y h:i:s A"; > And finally, copy the code below, and paste it in a file called client1.php. Please note though that you need to edit the line that says http://www.yourdomain.com/server1.php to the correct location of server1.php on your server. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN"> <html> <head> <title>Example 1</title> <script src="script.js" type="text/javascript"></script> <script type="text/javascript"> function updatedate { loadFragmentInToElementhttp://www.yourdomain.com/server1.php, currentdate; } </script> </head> <body> The current date is <span id="currentdate"><php echo date"l dS of F Y h:i:s A"; ></span>.<br /><br /> <input type="button" value="Update date" OnClick="updatedate;" /> </body> </html> Now go to http://www.yourdomain.com/client1.php and click on the button that says Update date. The date will update, without the page having to be reloaded. This is done with the XML HTTP Request object. This example can also be viewed online at http://www.phpit.net/demo/php%20on%20the%20fly/client1.php. Example 2 Lets try a more advanced example. In the following example, the visitor can enter two numbers, and they are added up by PHP and not by JavaScript. This shows the true power of PHP and the XML HTTP Request Object. This example uses the same script.js as in the first example, so you dont need to create this again. First, copy the code below and paste it in a file called server2.php: <php // Get numbers $num1 = intval$_GET[num1]; $num2 = intval$_GET[num2]; // Return answer echo $num1 + $num2; > And then, copy the code below, and paste it in a file called client2.php. Please note though that you need to edit the line that says http://www.yourdomain.com/server2.php to the correct location of server2.php on your server. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN"> <html> <head> <title>Example 2</title> <script src="script.js" type="text/javascript"></script> <script type="text/javascript"> function calc { num1 = document.getElementById num1.value; num2 = document.getElementById num2.value; var element = document.getElementByIdanswer; xmlhttp.open"GET", http://www.yourdomain.com/server2.phpnum1= + num1 + &num2= + num2; xmlhttp.onreadystatechange = function { if xmlhttp.readyState == 4 && xmlhttp.status == 200 { element.value = xmlhttp.responseText; } } xmlhttp.sendnull; } </script> </head> <body> Use the below form to add up two numbers. The answer is calculated by a PHP script, and <em>not</em> with JavaScript. Whats the advantage to this You can execute server-side scripts PHP without having to refresh the page.<br /><br /> <input type="text" id="num1" size="3" /> + <input type="text" id="num2" size="3" /> = <input type="text" id="answer" size="5" /> <input type="button" value="Calculate!" OnClick="calc;" /> </body> </html> When you run this example, you can add up two numbers, using PHP and no reloading at all! If you cant get this example to work, then have a look on http://www.phpit.net/demo/php%20on%20the%20fly/client3.php to see the example online. Any Disadvantages... There are only two real disadvantages to this system. First of all, anyone who has JavaScript turned off, or their browser doesnt support the XML HTTP Request Object will not be able to run it. This means you will have to make sure that there is a non-JavaScript version, or make sure all your visitors have JavaScript enabled e.g. an Intranet application, where you can require JS. Another disadvantage is the fact that it breaks bookmarks. People wont be able to bookmark your pages, if there is any dynamic content in there. But if youre creating a PHP application and not a PHP website, then bookmarks are probably not very useful anyway. Conclusion As Ive shown you, using two very simple examples, it is entirely possible to execute PHP scripts, without having to refresh the page. I suggest you read more about the XML HTTP Request Object http://jibbering.com/2002/4/httprequest.html and its capabilities. The things you can do are limitless. For example, you could create an extremely neat paging system, that doesnt require reloading at all. Or you could create a GUI for your PHP application, which behaves exactly like Windows XP. Just think about it! Be aware though that JavaScript must be enabled for this to work. Without JavaScript this will be completely useless. So make sure your visitors support JavaScript, or create a non-JavaScript version as well.
| ![]() | ![]() | ![]() | RELATED ARTICLES Making Your Website Work for You: Attracting Visitors Just building a website is not enough. You need to get people to visit it. And just submitting to the search engines won’t do much.Here is a brief overview of 3 effective strategies to get people to your website: 1Search Engines, 2 PR and Marketing and 3 Email Newsletters. Here is a quick list of do’s and don’ts for each: Web Hosting Basics So youre about to embark on your first web project and youre wondering where on earth to start Well, one of the first things youll need is a web hosting package. There are so many out there it can seem daunting to try and choose one. In this article well teach you the basics to help you make an informed decision. Unlimited Wealth Creation Through Reseller Hosting Web hosting can be an exciting business start up for aspiring entrepreneurs. All you have to do to set up your business is become a hosting reseller, i.e., provide hosting facilities to website owners. Here as a business owner, you are buying space from a big hosting company, and redistributing the hosting space to other website owners. All About Google If you read The Search Engine Showdown at www.abalone.ca/resources/searchenginetest.html, you know Google is my favourite search engine. Why Google always offers the most results for any given search they currently have over 8 billion pages indexed, its faster than the Audi Quattro we test drove this morning, and 9 times out of ten, in my experience, all the front page results are relevant to my search. In fact, I usually find what Im looking for within the first few sites listed. I also really respect the fact that two college kids started it kinda like Abalone! and that those two college kids seem not to have forgotten where they came from. If you check out the images at Googles press center http://www.google.com/press/images.html and scroll down to the Everyday Life Inside Google section, youll get a feeling that life at Google is fun. what Do Your Site Statistics Mean, Anyway What Do Your Site Statistics Mean, Anyway 50 Surefire Web Design Tips Tips to brand your websiteInclude your logo in all pages. Position it at the top left or each page.Complement your logo with a tagline or catchy sentence that summarizes your business purpose. For example "Always low prices" is the tagline for Wal-Mart.Create a favicon. A favicon is that small graphic that appears next to the URL in the address bar.Have a consistent look and feel in all your pages. Use a color scheme and layout that are clearly recognized across your site.Have an About Us section, that includes all relevant information about you and your business.Include a copyright statement at the bottom of each page. Using Google AdWords to Increase your Adsense Profits I had been using Googles Adsense program to generate some revenue from my various web sites.Prior to Adsense, I was never previously paid to run these and did so for a hobby.Overall I was pleased with the income I was generating.The only drawback was that I was always looking for ways to get people to my sites.I enjoyed working to bring in more visitors but felt that if I looked at my earnings vs. the hours I worked, the economics werent making sense. The Reality of Successful Web Site Today For a number of reasons, the analysis of customers feedback is too often given little attention. Still, people who ignore research are as dangerous as generals who ignore decodes of enemy signals c David Ogilvy. Its an inexcusable omission for those, who strive for having a successful web site. There are dozens of ways to receive feedback and unlimited ideas for your customer service improvement in it. But you cant afford phone everybody from your CRM list or correspond with each customer via email or regular mail. It will flatter them but bother you. Fortunately there is such a magic tool as web-based forms, which allow site owners to automate the process and to use the received data in your analysis; the analysis, aimed at your web site usability optimizing. Choosing the Right Website Designer Youve made up your mind. You want to have a website to represent your business. The next decision to be made . . . who should you hire to develop your site Tips to Maintain your Website to Keep Visitors Coming Back When you design a website you should create it so that your visitors are constantly coming back for more. In order for you to keep your visitors coming back you need to keep upwith maintaining your website, and offering things thatwill keep your visitors busy. Choosing a Hosting Service: A Checklist for Business Owners For a website to appear on the Internet, you require a “server” that is usually provided by a web hosting company. Hosting companies are paid monthly, quarterly or yearly. Some companies come with guarantees, too. Recommendations from other business owners and web designers are excellent ways to find a good hosting service. Color Psychology in Marketing On the internet we dont deal with face to face selling. The internet is a visual and psychological medium. The words, or sales copy, on your website have by far the greatest psychological impact on your visitors and thus becomes your most important communication and sales tool. Be Creative before you Purchase or Create a Website Before you jump into spending coutnless hours on designing and publishing your website, or spending hundreds onhaving it done by anyone else you should plan out your full design or structure. This is very important so that whenit comes time to do this long-lasting project you will be well prepared. Weave Your Own Web Many elementary school children know the miracle of Charlottes Web.Weaving the words "Some Pig" into the center of her web, Charlotte keeps Wilbur from the frying pan.E.B. Whites story provides some fascinating guidelines for web spinning. Cheap Web Hosting - When Cheaper is Better Anyone who has ever shopped for web hosting knows that you can spend anywhere from nothing up to several hundred dollars to host your web site. So what’s the difference Well often times if you’re talking about typical shared web hosting in the $6 to $20 range – nothing. That’s right – nothing! Nothing that is, except good old fashioned marketing and hype! Eliciting Constructive Website Feedback "Its good." "Youve done a nice job." "I dont like it." Link Popularity for a Successful Website: How Not to Go About Getting It A classic way of getting seen and noticed on the web is to make sure your site is linked to as many other sites as possible. The strategy of link-exchange has been so overused and abused that search engines are now wary of it and may penalize sites that are linking to "link farms", or to any other sites with non-relevant content. web page building for beginners 3 An inexperienced website builder must understand that free web hosting is a great way to get more than one site up and running quickly, and that www.articlecity.com is the fastest and easiest way to fill those sites with relevant material. For instance, my site at www.saquoyah.com has one hundred and sixty pages that were written by various authors and posted at ArticleCity by the writers. This arrangement fulfills five purposes to the satisfaction of everyone involved. First the writers of the articles get to help the readers and themselves by having links back to their websites. Second, ArticleCity gets many links to their index page and that in turn keeps their site in a top placement with search engines. Third, the little website owner saves an enormous amount of writing, yet gets his or her website full of pages that link back to their index page, so the search engines will place their site a little higher in placement. Fourth, the person doing a search gets to find a lot of points of view by various writers on the one subject they are poring over. Fifth, the search engines have more pages in their database for the searchers to look through. Microsoft and Webmasters Does Microsoft care for Webmasters 404 Error Pages-What are they and how do you create one We’ve all seen them, you’ve been browsing a website and you click a link and nothing loads apart from the words ‘404 Error’ along with the usual stuff. ![]() |
home | site map |
© 2005 |