1. Trang chủ
  2. » Công Nghệ Thông Tin

Plug in PHP 100 POWER SOLUTIONS- P69 pdf

5 187 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 277,29 KB

Nội dung

306 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s 306 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s Whenever the user types a letter into the input field, an Ajax request is issued to the program wordsfromroot.php, which then returns a list of words that begin with the letters so far entered. Each of these words is then placed in <option> fields of a <select> statement in order to display them in a drop-down list. If the user then clicks any of these offered words, a function is called to copy the word into the input field, and the list of suggested words is then cleared. In the process, a lot of use is made of hiding and revealing elements using their style.display properties, and the size of the displayed list is also manipulated according to the number of words to show. Due to appending a random number in $id to all ID names, this function can be successfully reused within a web page, or even the same form since the code is smart enough to realize it has been called one or more additional times and only creates the form elements required, without re-creating the JavaScript functions. It manages this by implementing the static variable counter $PIPHP_PW_NUM, which retains its value between calls to the function. How to Use It Use this plug-in as a replacement for a standard <input> tag inside an HTML form, like this: echo "<form method='post'>"; echo PIPHP_PredictWord("name='word' type='text'", 5, 20); echo "<input type='submit'></form>\n"; Here some standard parameters used in an <input> tag have been passed, along with two additional arguments: the maximum number of suggested words to show at a time in the drop-down box (the number of lines on view), and the maximum number of words to suggest in total. If the second parameter is larger, then the drop-down list will become scrollable. In the preceding case, the list will often be scrollable since up to 20 words can be offered, with only 5 displayed at any time. The supplied dictionary.txt file on the companion web site at www.pluginphp.com only includes single words, but you can also replace the contents of the file (or use a different one), with a list of useful phrases (perhaps gleaned from extracting common search terms from your log files). Just ensure you don’t add any punctuation or you may get unexpected results. If you use a different PHP program to supply the suggested words or phrases, remember to change the reference in the code to wordsfromroot.php to that of the new program. The Plug-in function PIPHP_PredictWord($params, $view, $max) { $id = rand(0, 1000000); $out = "<input id='PIPHP_PWI_$id' $params" . "onKeyUp='PIPHP_JS_PredictWord($view, $max, $id)'><br />" . "<select id='PIPHP_PWS_$id' style='display:none' />\n"; for ($j = 0 ; $j < $max ; ++$j) $out .= "<option id='PIPHP_PWO_$j" . "_$id' " . "onClick='PIPHP_JS_CopyWord(this.id, $id)'>"; $out .= '</select>'; static $PIPHP_PW_NUM; C h a p t e r 1 1 : I n c o r p o r a t i n g J a v a S c r i p t 307 C h a p t e r 1 1 : I n c o r p o r a t i n g J a v a S c r i p t 307 if ($PIPHP_PW_NUM++ == 0) $out .= <<<_END <script> function PIPHP_JS_CopyWord(id1, id2) { $('PIPHP_PWI_' + id2).value = $(id1).innerHTML $('PIPHP_PWS_' + id2).style.display = 'none'; } function PIPHP_JS_PredictWord(view, max, id) { if ($('PIPHP_PWI_' + id).value.length > 0) { PIPHP_JS_GetAjaxRequest2('wordsfromroot.php', 'word=' + $('PIPHP_PWI_' + id).value + '&max=' + max, view, max, id) $('PIPHP_PWS_' + id).scrollTop = 0 $('PIPHP_PWO_0_' + id).selected = true } else $('PIPHP_PWS_' + id).style.display = 'none' } function PIPHP_JS_GetAjaxRequest2(url, params, view, max, id) { nocache = "&nocache=" + Math.random() * 1000000 request = new PIPHP_JS_AjaxRequest() request.onreadystatechange = function() { if (this.readyState == 4) if (this.status == 200) if (this.responseText != null) { a = this.responseText.split('|') c = 0 for (j in a) { $('PIPHP_PWO_' + c + '_' + id). innerHTML = a[j] $('PIPHP_PWO_' + c++ + '_' + id). style.display = 'block' } n = c > view ? view : c while (c < max) { $('PIPHP_PWO_' + c++ + '_' + id). style.display = 'none' } $('PIPHP_PWS_' + id).size = n; $('PIPHP_PWS_' + id).style.display = 'block' } 308 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s // You can remove these two alerts after debugging else alert("Ajax error: No data received") else alert( "Ajax error: " + this.statusText) } request.open("GET", url + "?" + params + nocache, true) request.send(null) } function PIPHP_JS_AjaxRequest() { try { var request = new XMLHttpRequest() } catch(e1) { try { request = new ActiveXObject("Msxml2.XMLHTTP") } catch(e2) { try { request = new ActiveXObject("Microsoft.XMLHTTP") } catch(e3) { request = false } } } return request } function $(id) { return document.getElementById(id) } </script> _END; return $out; } CHAPTER 12 Diverse Solutions 310 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s 310 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s A number of plug-ins I wanted to include in this book didn’t quite fit into any of the categories of the other chapters. So here are the final ten plug-ins which, as the chapter title suggests, offer diverse solutions to a variety of programming problems, including geo-location, “bot” detection, data about books, word and spelling functions, language translation, rounded table borders, and Bing maps. Get Country by IP Knowing which country a web visitor is from can be extremely useful to a webmaster. For example, a personal ads site could use this information to match people up from the same country, as could an auction or classifieds web site. Or perhaps you have servers in different countries and want to refer visitors to the one nearest to them, or maybe you simply want to show different advertising or other content to different territories. Whatever the reason, this plug-in, which comes with its own data file, will tell you where a surfer is located, as can be seen from Figure 12-1, in which Google’s IP number has been correctly identified as being located in the U.S. About the Plug-in This plug-in takes an IP address and then returns the name of the country to which the IP has been allocated. Upon failure, it returns FALSE. It requires the following argument: • $ip An IP address Variables, Arrays, and Functions $iptemp Array containing the four parts of $ip $ipdec Integer containing the IP address after conversion to decimal $file String containing contents of the file ips.txt $lines Array of all data lines extracted from $file $line String containing a single line from $lines $parts Array containing all the parts of $line FIGURE 12-1 Provided with google.com’s IP number, this plug-in correctly identifies its country. 9 1 . decimal $file String containing contents of the file ips.txt $lines Array of all data lines extracted from $file $line String containing a single line from $lines $parts Array containing all the parts of $line FIGURE. u t i o n s A number of plug- ins I wanted to include in this book didn’t quite fit into any of the categories of the other chapters. So here are the final ten plug- ins which, as the chapter. located, as can be seen from Figure 12-1, in which Google’s IP number has been correctly identified as being located in the U.S. About the Plug- in This plug- in takes an IP address and then returns

Ngày đăng: 07/07/2014, 08:20