Post qua Ajax (cho nhiều ID) 2007-07-12 12:12:46
TG đã test thữ và đã chạy rất tốt nên post lên cho anh em tham khảo, trong code này có thể post và gửi nội dung đến một id theo ý mình
<script>
var loadstatustext = 'Loading....';
function makePOSTAddRequest(url,id, parameters) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
// set type accordingly to anticipated content type
//http_request.overrideMimeType('text/xml');
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 = loadstatustext;
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.');
}
}
}
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);
}
function get_dulieu(id,obj) {
var poststr = "data="+'[phpbasicDATA]'+encodeURIComponent( document.getElementById("name").value )+'[phpbasicDATA]'+encodeURIComponent( document.getElementById("form_dulieu").value );
//alert(poststr);
makePOSTAddRequest('add_article.php',id, poststr);
}
</script>
<div id="loading"></div>
<div id="show_noidung"></div>
<form name="myform" method="post" action="javascript:get_dulieu('show_noidung',document.getElementById('myform'));">
<input type="text" name="name" id="name" />
<textarea name="form_dulieu" id="form_dulieu"></textarea>
<input type="submit" name="Submit" value="Submit">
</form>
file add_article.php
<?php
$data = explode('[phpbasicDATA]',$_POST['data']);
$name = $data[0];
$desc = $data[1];
//insert vào databse 2 giá trị này
echo "Insert thành công";
?>
Tác giả TG
Tra loi 12 comment(s) 2007-07-12 12:12:46
Post qua Ajax (cho nhiều ID) 2008-02-03 07:36:09
Tra loi
xe_đạp 2008-02-15 10:37:00
var poststr = "data="+'[phpbasicDATA]'+encodeURIComponent( document.getElementById("name").value )+'[phpbasicDATA]'+encodeURIComponent( document.getElementById("form_dulieu").value );
đặc biệt là hàm encodeURIComponent
Tra loi
toiyeuphp 2008-02-27 10:18:06
Tra loi
mnminh 2008-02-27 05:53:59
Tra loi
TG 2008-02-27 08:33:21
Tra loi
cocbay 2008-02-28 09:18:57
Tra loi
tét 2008-04-03 05:36:03
Comprehensive support for testing Ajax / RIA / Web 2.0 applications Test Ajax Applications
Comprehensive support for testing Ajax / RIA / Web 2.0 applications Test Ajax Applications
Comprehensive support for testing Ajax / RIA / Web 2.0 applications
Tra loi
tét 2008-04-03 05:36:11
Comprehensive support for testing Ajax / RIA / Web 2.0 applications
Tra loi
kieunga 2008-04-24 03:18:49
câu lệnh này có nghĩa là gì vậy?em ko hiểu từ explode.
Tra loi
kirk 2008-04-25 01:19:23
Tra loi
Ngọc Bảo 2008-07-20 12:58:54
Tra loi
ThanhAn 2008-08-31 10:54:49
Tra loi
Y kien