Form nhập liệu sử dụng AJAX 2007-07-08 08:55:44
Mẫu: form nhập liệu TG đang dùng để soạn bài mới,ý kiến
HTML:
<html>
<script>
function $$(url,id, parameters) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
http_request.onreadystatechange = function()
{
document.getElementById('loading').innerHTML = 'loading...';
if (http_request.readyState == 4) {
if (http_request.status == 200) {
//alert(http_request.responseText);
result = http_request.responseText;
document.getElementById('loading').innerHTML = '';
document.getElementById(id).innerHTML = result;
} else {
alert('There was a problem with the request.');
return false;
}
}
}
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(parameters);
return true;
}
function ajaxPost(){
poststr="DATA="+ encodeURIComponent(document.getElementById("name").value)+
"BASIC_SPACE"+ encodeURIComponent(document.getElementById("noidung").value);
$$('path_to/insertdb.php','hienthi',poststr);
}
</script>
<div id="loading"></div>
<div id="hienthi">
<form action="javascript:ajaxPost();" method="post">
Tên: <input type="text" name="name" id="name"><br />
Nội dung: <textarea name="noidung" id="noidung">Nội dung mẫu</textarea>
<input type="submit" value="submit">
</form>
</html>
file insertdb.php
<?php
$data = $_POST['DATA'];
$tmp = explode('BASIC_SPACE',$data);
$name = $tmp[0];
$noidung = $tmp[1];
print 'Xử lý name: '. $tmp[0].' và noidung '.$tmp[1];
?>
Tra loi 31 comment(s) 2007-07-08 08:55:44
NữThần 2007-09-21 01:43:07
Tra loi
kirk 2007-09-21 01:30:14
tại sao nó chỉ chạy trên IE, còn trên FF đứng im re.
Tra loi
NữThần 2007-09-22 09:54:01
Tra loi
AFK 2007-09-22 10:03:10
Tui nghỉ lỗi ở chổ chử status. Phải viết là http_request.Status thì mới Ok!
Thử đi nha!
Tra loi
NữThần 2007-09-24 02:06:39
Tra loi
AFK 2007-09-24 10:35:01
Bạn bỏ phần kiểm tra status bằng 200 đi,như sau:
{
document.getElementById('loading').innerHTML = 'loading...';
if (http_request.readyState == 4) {
//alert(http_request.responseText);
result = http_request.responseText;
document.getElementById('loading').innerHTML = '';
document.getElementById(id).innerHTML = result;
}
}
}
Vậy là ok!!!
Tra loi
TG 2007-09-24 08:48:22
Tra loi
NữThần 2007-09-25 09:31:56
Tra loi
ngonchan 2007-09-26 02:06:36
Coi lại thử, lúc copy, rồi sửa, có sai sót gì không ?
Tra loi
afk 2007-09-28 08:57:26
Viết xong mọi thứ rồi, IE thì chạy, FF thì không phải ko.
Vậy có phải bản FF của bạn là bản 2. không vậy?
Nếu vậy thì cài bản khác đi.
Lúc trước tui cũng gặp tình huống vậy, sau khi cài lại bản khác là ok! Không hiểu vì sao????
Tra loi
phuongnam 2007-09-28 04:29:28
Tra loi
NữThần 2007-10-04 12:32:36
Lúc trước tui cũng gặp tình huống vậy, sau khi cài lại bản khác là ok! Không hiểu vì sao????
Bản khác là bản nào vậy đại ca, bản mới nhất mà không được à?Chắc là code của pa TG chưa rào hết các tình huống đấy,mình phải tập trung tấn công TG, để pa hoàn chỉnh nó, hiểu chưa?
:D
Tra loi
TG 2007-10-04 12:52:48
Tra loi
NữThần 2007-10-27 11:02:37
TG xem lại coi sao nha !
Tra loi
changtraingheo 2007-10-31 03:21:03
Tra loi
cảm ơn 2008-03-19 06:08:08
Tra loi
hachikun 2008-05-02 08:56:42
Tra loi
huy 2009-02-16 11:45:15
Tra loi
huy 2009-02-16 11:45:33
Tra loi
bài hay quá 2009-07-12 04:25:44
Tra loi
cóc 2009-07-13 10:14:42
Pa TG này lăng xê 1 cách trắng trợn :D
Tra loi
TG 2009-07-13 02:32:19
Tra loi
cóc 2009-07-13 02:35:38
Koi nó kìa, chịu nổi hem, xấu hổ ghê >_<
Tra loi
changtraingheo 2009-07-13 03:53:16
Tra loi
Về Đâu 2009-07-13 08:26:48
Tra loi
Ngoc Tan 2009-08-04 04:30:28
Ví dụ:
Mới vào mình có forum nhập liệu
Nhưng sau khi bấm nút Submit thì nó xuất ra giá
Ý của tôi là muốn nó chỉ RELOAD lại cái nội dung xuất ra cuối thôi.(tức là khi bấm SUBMIT thì cái form vẫn nằm ở đó đồng thời nó chỉ RELOAD phần xuất nội dung)
Mong được giúp đỡ
Tra loi
choncon 2009-08-04 07:34:14
Tra loi
boyboy 2010-04-17 01:20:22
Tra loi
Y kien