|
|
|
สอบถามเรื่อง ajax หน่อยครับอยากทราบว่านำค่ามาแสดงมากกว่า 1 ทำยังไง |
|
|
|
|
|
|
|
จาก code ด้านล่าง ผมได้ส่งค่า pmeters ไปสองค่าคือ newpass กับ confirmpass การส่งไม่มีปัญหา
เวลาแสดงก็ต้องใช้ <span id="mySpan"></span>เรียกมาแสดง
แต่ผมอยากทราบว่าถ้าจะให้ myspan แสดงค่า newpass myspan2 แสดงค่า confirmpass ต้องเพิ่มอะไรตรงไหนบ้างเหรอครับ
Code (PHP)
<script language="JavaScript">
var HttPRequest = false;
function doCallAjax() {
HttPRequest = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
HttPRequest = new XMLHttpRequest();
if (HttPRequest.overrideMimeType) {
HttPRequest.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!HttPRequest) {
alert('Cannot create XMLHTTP instance');
return false;
}
var url = 'modules/users/post.php';
var pmeters = "newpass=" + encodeURI( document.getElementById("newpass").value);
"confirmpass=" + encodeURI( document.getElementById("confirmpass").value);
HttPRequest.open('POST',url,true);
HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
HttPRequest.setRequestHeader("Content-length", pmeters.length);
HttPRequest.setRequestHeader("Connection", "close");
HttPRequest.send(pmeters);
HttPRequest.onreadystatechange = function()
{
if(HttPRequest.readyState == 3) // Loading Request
{
document.getElementById("mySpan").innerHTML = "..";
}
if(HttPRequest.readyState == 4) // Return Request
{
if(HttPRequest.responseText == 'Y')
{
window.location = 'AjaxPHPRegister3.php';
}
else
{
document.getElementById("mySpan").innerHTML = HttPRequest.responseText;
}
}
}
}
function doCallAjax1() {
HttPRequest = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
HttPRequest = new XMLHttpRequest();
if (HttPRequest.overrideMimeType) {
HttPRequest.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!HttPRequest) {
alert('Cannot create XMLHTTP instance');
return false;
}
var url = 'modules/users/post.php';
var pmeters = "newpass=" + encodeURI( document.getElementById("newpass").value) + "&confirmpass=" + encodeURI( document.getElementById("confirmpass").value);
HttPRequest.open('POST',url,true);
HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
HttPRequest.setRequestHeader("Content-length", pmeters.length);
HttPRequest.setRequestHeader("Connection", "close");
HttPRequest.send(pmeters);
HttPRequest.onreadystatechange = function()
{
if(HttPRequest.readyState == 3) // Loading Request
{
document.getElementById("mySpan1").innerHTML = "..";
}
if(HttPRequest.readyState == 4) // Return Request
{
if(HttPRequest.responseText == 'Y')
{
window.location = 'AjaxPHPRegister3.php';
}
else
{
document.getElementById("mySpan").innerHTML = HttPRequest.responseText;
}
}
}
}
</script>
ปล.ขอบคุณทุกท่านที่เข้าช่วยตอบคำถามน่ะครับ
Tag : PHP, Ajax, jQuery
|
|
|
|
|
|
Date :
2012-11-05 11:39:32 |
By :
ninjadark |
View :
889 |
Reply :
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ถ้าถึงจะต้องมี JSON มาช่วยครับ
Code (PHP)
<?php
$str = '[ {"CustomerID":"C001", "Name":"Weerachai Nukitram", "Email":"[email protected]", "CountryCode":"TH", "Budget":"1000000", "Used":"600000"} ] ';
?>
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function(){
var obj = jQuery.parseJSON('<?=$str;?>');
$.each(obj, function(key, val) {
$("#div1").append('[' + key + '] ' + 'CustomerID=' + val["CustomerID"] +'<br />');
$("#div1").append('[' + key + '] ' + 'Name=' + val["Name"] +'<br />');
$("#div1").append('[' + key + '] ' + 'Email=' + val["Email"] +'<br />');
$("#div1").append('[' + key + '] ' + 'CountryCode=' + val["CountryCode"] +'<br />');
$("#div1").append('[' + key + '] ' + 'Budget=' + val["Budget"] +'<br />');
$("#div1").append('[' + key + '] ' + 'Used=' + val["Used"] +'<br />');
});
});
</script>
<body>
<span id="div1"></span>
</body>
</html>
Go to : jQuery Ajax กับ JSON (Web Service) ทำความเข้าใจ การรับส่งข้อมูล JSON ผ่าน jQuery กับ Ajax
|
|
|
|
|
Date :
2012-11-05 11:53:08 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตอบความคิดเห็นที่ : 1 เขียนโดย : mr.win เมื่อวันที่ 2012-11-05 11:53:08
รายละเอียดของการตอบ ::
ใช่แบบนี้หรือป่าวครับ
Code
<script language="JavaScript">
var HttPRequest = false;
function doCallAjax() {
HttPRequest = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
HttPRequest = new XMLHttpRequest();
if (HttPRequest.overrideMimeType) {
HttPRequest.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!HttPRequest) {
alert('Cannot create XMLHTTP instance');
return false;
}
var url = 'modules/users/post.php';
var pmeters = "newpass=" + encodeURI( document.getElementById("newpass").value);
"confirmpass=" + encodeURI( document.getElementById("confirmpass").value);
HttPRequest.open('POST',url,true);
HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
HttPRequest.setRequestHeader("Content-length", pmeters.length);
HttPRequest.setRequestHeader("Connection", "close");
HttPRequest.send(pmeters);
HttPRequest.onreadystatechange = function()
{
if(HttPRequest.readyState == 3) // Loading Request
{
document.getElementById("mySpan").innerHTML = "..";
}
if(HttPRequest.readyState == 4) // Return Request
{
if(HttPRequest.responseText == 'Y')
{
window.location = 'AjaxPHPRegister3.php';
}
else
{
document.getElementById("mySpan").innerHTML = HttPRequest.responseText;
}
}
}
}
function doCallAjax1() {
HttPRequest = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
HttPRequest = new XMLHttpRequest();
if (HttPRequest.overrideMimeType) {
HttPRequest.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!HttPRequest) {
alert('Cannot create XMLHTTP instance');
return false;
}
var url = 'modules/users/post.php';
var pmeters = "newpass=" + encodeURI( document.getElementById("newpass").value) + "&confirmpass=" + encodeURI( document.getElementById("confirmpass").value);
HttPRequest.open('POST',url,true);
HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
HttPRequest.setRequestHeader("Content-length", pmeters.length);
HttPRequest.setRequestHeader("Connection", "close");
HttPRequest.send(pmeters);
HttPRequest.onreadystatechange = function()
{
if(HttPRequest.readyState == 3) // Loading Request
{
.success(function(result) {
$("#mySpan1").empty();
$("#mySpan2").empty();
var obj = jQuery.parseJSON(result);
$.each(obj, function(key, val) {
$("#mySpan1").append('[' + key + ']')' + 'newpass=' + val["newpass"]);
$("#mySpan1").append('[' + key + ']')' + 'newconfirm=' + val["newconfirm"]);
});
});
}
if(HttPRequest.readyState == 4) // Return Request
{
if(HttPRequest.responseText == 'Y')
{
window.location = 'AjaxPHPRegister3.php';
}
else
{
document.getElementById("mySpan").innerHTML = HttPRequest.responseText;
}
}
}
}
</script>
|
|
|
|
|
Date :
2012-11-05 12:08:15 |
By :
ninjadark |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
คุณจะต้องเปลี่ยนรูปแบบการเขียนเป็น jQuery + Ajax + JSON ให้หมดครับ มันเขียนง่ายกกว่าเดิมเยอะเลยครับ
|
|
|
|
|
Date :
2012-11-05 12:55:52 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|