AjAX

Use to ajax for php page

AJAX Code:

ajaxpg.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
var http_request=false;
 
  function send_request(url){
 
    http_request=false;
 
    if(window.XMLHttpRequest){
 
     http_request=new XMLHttpRequest();
 
     if(http_request.overrideMimeType){
 
       http_request.overrideMimeType("text/xml");
 
     }
 
    }
 
    else if(window.ActiveXObject){
 
     try{
 
      http_request=new ActiveXObject("Msxml2.XMLHttp");
 
     }catch(e){
 
      try{
 
      http_request=new ActiveXobject("Microsoft.XMLHttp");
 
      }catch(e){}
 
     }
 
    }
 
    if(!http_request){
 
     window.alert("XMLHttp object creation failed");
 
     return false;
 
    }
 
    http_request.onreadystatechange=processrequest;
 
    http_request.open("GET",url,true);
 
    http_request.send(null);
 
  }
 
  function processrequest(){
 
   if(http_request.readyState==4){//Object state
 
     if(http_request.status==200){//return Successful 
 
      document.getElementById(reobj).innerHTML=http_request.responseText;
 
     }
 
     else{ //Exception Errors
 
      alert("Exception Errors!");
 
     }
 
   }
 
  }
 
  function dopage(obj,url){
 
   document.getElementById(obj).innerHTML="Loading...";
 
   send_request(url);
 
   reobj=obj;
 
   }

index.php:

I am a content div on the display, when the flip action arises, the use of AJAX to update DIV to achieve the effect of this is the contents page shows the page code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
< ?php header("Content-type: text/html;charset=utf-8"); ?>
 
<html>
 
<head>
 
<script language="javascript" src="ajaxpg.js"></script>
 
</head>
 
<body>
<div id="result">
 
< ?php
 
$page=isset($_GET['page'])?intval($_GET['page']):1; 
 
$num=10;  
 
$db=mysql_connect("www.easemarry.com","root","root"); 
 
mysql_select_db("cr_download"); 
 
$result=mysql_query("select * from cr_userinfo");
 
$total=mysql_num_rows($result); 
 
$url='test.php';
$pagenum=ceil($total/$num);
$page=min($pagenum,$page);
 
$prepg=$page-1;
 
$nextpg=($page==$pagenum ? 0 : $page+1);
 
$offset=($page-1)*$num;  
 
$pagenav=":<B>".($total?($offset+1):0)."-<b>".min($offset+10,$total)."</b>,Total:$total";
 
if($pagenum< =1) return false;
 
$pagenav.=" <a href=javascript:dopage('result','$url?page=1');> < < </a> ";
 
if($prepg) $pagenav.=" <a href=javascript:dopage('result','$url?page=$prepg');> < </a> "; else $pagenav.=" < ";
 
if($nextpg) $pagenav.=" <a href=javascript:dopage('result','$url?page=$nextpg');> > </a> "; else $pagenav.=" > ";
 
$pagenav.=" <a href=javascript:dopage('result','$url?page=$pagenum');> >> </a> ";
 
$pagenav.=" page,Total page: $pagenum";
 
If($page>$pagenum){
 
       Echo "Error : Can Not Found The page ".$page;
 
       Exit;
 
}
 
$info=mysql_query("select * from cr_userinfo limit $offset,$num"); 
 
While($it=mysql_fetch_array($info)){
 
       Echo $it['username'];
 
       echo "";
 
} 
 
  echo"";
 
  echo $pagenav;
?>
</div>
 
</body>
</html>

test.php:

The key to flip when the flip is called dopage() function, and then use the callback information to update the content div. The core server-side code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
< ?php
header("Content-type: text/html;charset=utf-8");
 
$page=isset($_GET['page'])?intval($_GET['page']):1; 
 
$num=10;  
 
$db=mysql_connect("www.easemarry.com","root","root"); 
 
mysql_select_db("cr_download"); 
 
$result=mysql_query("select * from cr_userinfo");
 
$total=mysql_num_rows($result); 
 
$url='test.php';
 
$pagenum=ceil($total/$num);
$page=min($pagenum,$page);
 
$prepg=$page-1;
 
$nextpg=($page==$pagenum ? 0 : $page+1);
 
$offset=($page-1)*$num;  
 
$pagenav=":<B>".($total?($offset+1):0)."-<b>".min($offset+10,$total)."</b>,Total:$total";
 
if($pagenum< =1) return false;
 
$pagenav.=" <a href=javascript:dopage('result','$url?page=1');> < < </a> ";
 
if($prepg) $pagenav.=" <a href=javascript:dopage('result','$url?page=$prepg');> < </a> "; else $pagenav.=" < ";
 
if($nextpg) $pagenav.=" <a href=javascript:dopage('result','$url?page=$nextpg');> > </a> "; else $pagenav.=" > ";
 
$pagenav.=" <a href=javascript:dopage('result','$url?page=$pagenum');> >> </a> ";
 
$pagenav.=" page,Total page: $pagenum";
 
If($page>$pagenum){
       Echo "Error : Can Not Found The page ".$page;
       Exit;
}
 
$info=mysql_query("select * from cr_userinfo limit $offset,$num"); 
 
While($it=mysql_fetch_array($info)){
 
       Echo $it['username'];
 
       echo "";
 
} 
 
  echo"";
 
  echo $pagenav;
 
?>


Google AJAX Language API php class

This class allows you to use the Google AJAX Translation API to translate arbitrary text to the many languages supported by the Google API.

Source versioning added to:
http://code.google.com/p/php-language-api/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
< ?php /** * Translating language with Google API * @author gabe@fijiwebdesign.com * @version $id$ * @license - Share-Alike 3.0 (http://creativecommons.org/licenses/by-sa/3.0/) *  * Google requires attribution for their Language API, please see: http://code.google.com/apis/ajaxlanguage/documentation/#Branding *  */class Google_Translate_API { 	/**	 * Translate a piece of text with the Google Translate API	 * @return String	 * @param $text String	 * @param $from String[optional] Original language of $text. An empty String will let google decide the language of origin	 * @param $to String[optional] Language to translate $text to	 */	function translate($text, $from = '', $to = 'en') {		$url = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q='.rawurlencode($text).'&langpair='.rawurlencode($from.'|'.$to);		$response = file_get_contents(			$url,			null,			stream_context_create(				array(					'http'=>array(					'method'=>"GET",					'header'=>"Referer: http://".$_SERVER['HTTP_HOST']."/\r\n"					)				)			)		);		if (preg_match("/{\"translatedText\":\"([^\"]+)\"/i", $response, $matches)) {			return self::_unescapeUTF8EscapeSeq($matches[1]);		}		return false;	} 	/**	 * Convert UTF-8 Escape sequences in a string to UTF-8 Bytes	 * @return UTF-8 String	 * @param $str String	 */	function _unescapeUTF8EscapeSeq($str) {		return preg_replace_callback("/\\\u([0-9a-f]{4})/i", create_function('$matches', 'return html_entity_decode(\'&#x\'.$matches[1].\';\', ENT_NOQUOTES, \'UTF-8\');'), $str);	}} // example usage$text = 'Welcome to my website.';$trans_text = Google_Translate_API::translate($text, '', 'it');if ($trans_text !== false) {	echo $trans_text;}  ?>< ?php
 
/**
 * Translating language with Google API
 * @author gabe@fijiwebdesign.com
 * @version $id$
 * @license - Share-Alike 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
 * 
 * Google requires attribution for their Language API, please see: http://code.google.com/apis/ajaxlanguage/documentation/#Branding
 * 
 */
class Google_Translate_API {
 
	/**
	 * Translate a piece of text with the Google Translate API
	 * @return String
	 * @param $text String
	 * @param $from String[optional] Original language of $text. An empty String will let google decide the language of origin
	 * @param $to String[optional] Language to translate $text to
	 */
	function translate($text, $from = '', $to = 'en') {
		$url = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q='.rawurlencode($text).'&langpair='.rawurlencode($from.'|'.$to);
		$response = file_get_contents(
			$url,
			null,
			stream_context_create(
				array(
					'http'=>array(
					'method'=>"GET",
					'header'=>"Referer: http://".$_SERVER['HTTP_HOST']."/\r\n"
					)
				)
			)
		);
		if (preg_match("/{\"translatedText\":\"([^\"]+)\"/i", $response, $matches)) {
			return self::_unescapeUTF8EscapeSeq($matches[1]);
		}
		return false;
	}
 
	/**
	 * Convert UTF-8 Escape sequences in a string to UTF-8 Bytes
	 * @return UTF-8 String
	 * @param $str String
	 */
	function _unescapeUTF8EscapeSeq($str) {
		return preg_replace_callback("/\\\u([0-9a-f]{4})/i", create_function('$matches', 'return html_entity_decode(\'&#x\'.$matches[1].\';\', ENT_NOQUOTES, \'UTF-8\');'), $str);
	}
}
 
// example usage
$text = 'Welcome to my website.';
$trans_text = Google_Translate_API::translate($text, '', 'it');
if ($trans_text !== false) {
	echo $trans_text;
}
 
 
?>


Edit this page form and left when prompted to do (jQuery)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
$.fn.enable_changed_form_confirm = function () {
var _f = this;
$(':text, :password, textarea', this).each(function() {
$(this).attr('_value', $(this).val());
});
 
$(':checkbox, :radio', this).each(function() {
var _v = this.checked ? 'on' : 'off';
$(this).attr('_value', _v);
});
 
$('select', this).each(function() {
$(this).attr('_value', this.options[this.selectedIndex].value);
});
 
$(this).submit(function() {
window.onbeforeunload = null;
});
 
window.onbeforeunload = function(_f) {
if(is_form_changed(_f)) {
return "You will lose any unsaved content.";
}
}
}
 
function is_form_changed(f) {
var changed = false;
$(':text, :password, textarea', f).each(function() {
var _v = $(this).attr('_value');
if(typeof(_v) == 'undefined') _v = '';
if(_v != $(this).val()) changed = true;
});
 
$(':checkbox, :radio', f).each(function() {
var _v = this.checked ? 'on' : 'off';
if(_v != $(this).attr('_value')) changed = true;
});
 
$('select', f).each(function() {
var _v = $(this).attr('_value');
if(typeof(_v) == 'undefined') _v = '';
if(_v != this.options[this.selectedIndex].value) changed = true;
});
return changed;
}
 
$(function() {
$('form').enable_changed_form_confirm();
});