 |
รบกวนคนเก่ง AJAX กับ JAVA ช่วยหน่อยครับ โค๊ต Ajax ของผมเป็นแบบนี้ |
|
 |
|
|
 |
 |
|
โค๊ต Ajax ของผมเป็นแบบนี้
Code (PHP)
function ajaxLoad(method, URL, data, displayId) {
var ajax = null;
if(window.ActiveXObject){
ajax = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
ajax = new XMLHttpRequest();
}
else{
alert("Your browser doesn't support Ajax");
return;
}
method = method.toLowerCase();
URL += "?dummy=" + (new Date()).getTime();
if(method.toLowerCase() == "get") {
URL += "&" + data;
data = null;
}
ajax.open(method, URL);
if(method.toLowerCase() == "post"){
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
}
ajax.onreadystatechange = function() {
if(ajax.readyState==4 && ajax.status==200){
var ctype = ajax.getResponseHeader("Content-Type");
ctype = ctype.toLowerCase();
if(ctype.match("text/plain")){
ajaxCallback(ctype, displayId, ajax.responseText);
}
else if(ctype.match("text/javascript")){
eval(ajax.responseText);
}
delete ajax;
ajax = null;
}
}
ajax.send(data);
}
function ajaxCallback(contentType, displayId, responseText){
if(contentType.match("text/javascript")){
eval(responseText);
}
else {
var el = document.getElementById(displayId);
el.innerHTML = responseText;
}
}
ส่วนอันนี้ฟังก์ชั่นในการเรียกใช้
Code (PHP)
function SendMail(){
var idQ = document.getElementById("idQ").value;
var mailcus = document.getElementById("mailcus").value;
var mailcus2 = document.getElementById("mailcus2").value;
var mailother = document.getElementById("mailother").value;
var mailother2 = document.getElementById("mailother2").value;
var data = "stap2=yes&mailcus=" + mailcus + "&mailcus2=" + mailcus2 + "&mailother=" + mailother + "&mailother2=" + mailother2 + "&idQ=" + idQ ;
var URL = "Quotation_ajax.php";
ajaxLoad("post", URL, data, "");
}
คำถามคือ ในส่วนของการเรียกใช้ด้วยจาวาน่ะครับ พอดีผมเคยไปอ่านหนังสือมาเล่มนึง ขั้นตอนตรงนี้
var idQ = document.getElementById("idQ").value;
var mailcus = document.getElementById("mailcus").value;
var mailcus2 = document.getElementById("mailcus2").value;
var mailother = document.getElementById("mailother").value;
var mailother2 = document.getElementById("mailother2").value;
var data = "stap2=yes&mailcus=" + mailcus + "&mailcus2=" + mailcus2 + "&mailother=" + mailother + "&mailother2=" + mailother2 + "&idQ=" + idQ ;
เขาไม่ใช้เลย เขาใช้คำสั่งอ้างอิง form แล้วส่งไปแบบยกชุดเลย พูดง่ายๆคือ เขาเขียนแค่บรรทัดเดียวเองน่ะครับ
ไม่ทราบว่า เทพจาวาท่านใดจำคำสั่งนี้ได้บ้างครับ รบกวนด้วยครับ
ขอบคุณครับ
Tag : - - - -
|
|
 |
 |
 |
 |
Date :
2010-07-21 18:00:53 |
By :
awachai007 |
View :
1227 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าใช้ framework (prototype ,jquery,dojo ,....) จะมีฟังก์ชันส่งฟอร์มแบบนี้ครับ เราเรียกว่า serialize()
ถ้าจาวาสคริปธรรมดาไม่มีครับ ต้องสร้างฟังก์ชันขึ้นเอง ไม่เคยเขียนเหมือนกัน แต่ถ้ามีหนังสือ ajax คิดว่ามีฟังก์ชันนี้ครับ
|
 |
 |
 |
 |
Date :
2010-07-21 18:18:10 |
By :
xbeginner01 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช่แล้วครับ ไปแอบยืนอ่านใน SEED มา เขาใช้ฟังก์ชั่น serialize() นี่แหละครับ ตามที่คุณ xbeginner01 บอกเลย รูปแบบการเรียกใช้งานคือ
Code (PHP)
function SendMail(){
var value = FORM.serialize('form1');
var data = value;
var URL = "Quotation_ajax.php";
ajaxLoad("post", URL, data, "");
}
นี่แหละที่รอมานาน ฟอร์มข้อมูลมันเยอะ ก็เลยอยากได้แบบนี้และครับ
|
 |
 |
 |
 |
Date :
2010-07-21 20:53:14 |
By :
awachai007 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|