Using Php+javascript to create Ajax search Windows

Source: Internet
Author: User
Tags echo command requires urlencode

First, the introduction

One of the most widely used features in the Web world is search. With the development of web technology, in order to better meet customer demand, the regular search engine began to more unconventional way "open the door." In this respect, Yahoo! is the first to provide its Y! Q Service. This new service enables you to search any Web page, provided that the author of the page must be included in their web page. It is the service technology that enables you to present the relevant search results to the reader so that you can show more information to the reader without leaving their current page.

Yahoo! 's Y! Q Service is indeed a great idea, but it has also been criticized for its emergence. Why? First, it requires the client to use Yahoo! JavaScript and you must add a

element to satisfy the Yahoo! search requirements. For many web site authors, the provision of this service requires too much effort. And, after all these conditions are met, the search results will be displayed in Yahoo! style, thereby damaging the appearance of the user's site.

Fortunately, Yahoo! is not the only search engine that offers "search results from your site" service. MSN Search also provides a similar service, except that it enables Web developers to control the look and feel. This ability comes from the RSS version of MSN Search that provides its results, which makes it possible to subscribe to a specific search or use Ajax methods to add the results to your page.

Although Google has pioneered this new "Search from your site" technology, Google-related Google Blogsearch Beta has been able to provide returns in RSS or Atom format at the time of this article.

Second, server-side components

When searching using MSN Search, you will see an orange-colored XML image appear at the bottom of the resulting page. Clicking on this image will take you to a new page and provide you with a URL to subscribe to the search.

In this way, you can write server-side code to retrieve remote feeds. For the Search window in this article, you will use PHP to retrieve the search feed. The URL from the server application request information looks like the following:

websearch.php?search=[SEARCHTERM]

The query string has only one variable: "search"; Therefore, the application should look for this query item. On the server side, you need to create a page to "pull" this data:

<?php
header("Content-Type: text/xml");
header("Cache-Control: no-cache");if ( isset($_GET["search"]) )
{
 $searchTerm = urlencode( stripslashes($_GET["search"]) );
 $url = "http://search.msn.com/results.aspx?q=$searchTerm&format=rss";
 $xml = file_get_contents($url);
 echo $xml;
}
?>

The first two lines set the required headers so that the browser can handle the data correctly (in XML form and without buffering the results). The next line of code uses the Isset () function to determine whether the search key exists in the query string.

In order to send a suitable request to a remote host, the search term should be "filtered" by many functions. First, it is passed to the stripslashes () function. If the "Magic quotes" (which is supported by default) is started in the PHP configuration, any quotes that arrive at the PHP engine are automatically taken off using a slash (such as, "Search Query\"). The Stripslashes () function deletes these symbols and leaves only "search query". After the slash is removed, go to the UrlEncode () function, which is responsible for encoding characters for the query string. Symbols such as spaces, quotes, "&" are encoded.

Note If search terms do not "filter" through these functions, the MSN Server will return a code 400-"bad Request".

When the search term is ready for conversion, it is included in the URL and stored in the $url variable. Finally, the file_get_contents () function opens the remote file, reads its contents, returns it to the $xml variable as a string, and then prints it to the page using the echo command.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.