Tuesday, October 5, 2010

สร้าง AJAX HttpRequest ด้วย jQuery ง่ายนิดเดียวเอง !

การเขียนโปรแกรม PHP
$name = $_POST['name'];
$password = $_POST['password'];
if($name == 'user' && $password == '1234') {
echo 'OK';
} else {
echo 'FAIL !';
}


การเขียน JavaScript
jQuery(document).ready(function(e) {

check_location();
location_login();
$('#system_btn button,input[type=submit]').button();
$('#caller_name_txt').autocomplete_js("http://10.11.5.212/JSONSERVICE/search.php?jsoncallback=?", {
width: 489,
max: 30,
multiple: true,
scroll: false,
selectFirst: false
});
$('#search_txt').autocomplete_js("http://10.11.5.212/JSONSERVICE/address.php?jsoncallback=?", {
width: 489,
max: 30,
multiple: true,
scroll: false,
selectFirst: false
});
$("#search_txt").result(function(event, data, formatted) {
$('#service_txt').val('');
$('#cusref_txt').val('');
if (data){
//$("#service_no").val(data[3]); // data[1] = account // data[3] = service_no data[4] = customer_ref
//get_account(data[3]);
//alert('0: '+data[0]+'\n1:'+data[1]+'\n2:'+data[2]+'\n3:'+data[3]+'\n4:'+data[4]);
get_name(data[1]);
get_profile(data[1]);
get_list_sum(data[1]);
$('#service_txt').val(data[3]);
$('#cusref_txt').val(data[4]);
$('#account_hidden').val(data[1]);
$('#search_txt').val('');
$('#search_txt').focus();
insert_caller();
}
});

$('#caller_name_txt , #comment_txt').focus(function() {
$(this).addClass('caller_txt_box_focus');
});
$('#caller_name_txt, #comment_txt').blur(function() {
$(this).removeClass('caller_txt_box_focus');
});

});


ตัวอย่างใหม่
var start = window.mozAnimationStartTime;  // for Chrome: moz -> webkit
function step(timestamp) {
var progress = timestamp - start;
d.style.left = Math.min(progress/10, 200) + "px";
if (progress < 2000) {
window.mozRequestAnimationFrame(step); // for Chrome: moz -> webkit
}
}
window.mozRequestAnimationFrame(step); //{} for Chrome: moz -> webkit progressive john winyou