|
|
|
เรื่องของ chatroom จะให้แสดงชื่อจาก session แทนกรอกชื่อไปใหม่ ต้องทำยังไงครับ |
|
|
|
|
|
|
|
chatroomของthaicreateถ้าเราต้องการให้ช่องกรอกชื่อให้มันแสดงค่าsessionแทนที่จะต้องกรอกชื่อใหม่ จะต้องทำยังไงครับ
Code
<?php
/*** By Weerachai Nukitram ***/
/*** http://www.ThaiCreate.Com ***/
include "chksession.php";
?>
<html>
<head>
<title>ThaiCreate.Com Ajax Tutorial</title>
<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 = 'AjaxPHPChat2.php';
var pmeters = "tName=" + encodeURI( document.getElementById("txtName").value) +
"&tMessage=" + encodeURI( document.getElementById("txtMessage").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 = "Now is Loading...";
}
if(HttPRequest.readyState == 4) // Return Request
{
document.getElementById('mySpan').innerHTML = HttPRequest.responseText;
// focus //
var el = document.getElementById('mydiv');
el.tabIndex = 32456;
el.focus();
}
}
}
</script>
</head>
<body Onload="JavaScript:doCallAjax()">
<h1>My Chat Room </h1>
<table width="566" height="315" border="1" cellpadding="2" cellspacing="2">
<tr>
<td width="575" height="291" valign="top">
<div style=width:570;height:280;overflow:auto>
<span id="mySpan"></span>
</div>
</td>
</tr>
<tr>
<td height="22" valign="top"><form action="" method="post" name="frmMain" id="frmMain">
<div align="center">
<br>
Name
<input name="txtName" type="text" id="txtName" size="5">
Message
<input name="txtMessage" type="text" id="txtMessage" size="40">
<input name="btnSend" type="button" id="btnSend" value="Send" onClick="JavaScript:doCallAjax();">
</div>
</form></td>
</tr>
</table>
</body>
</html>
Code
<?php
/*** By Weerachai Nukitram ***/
/*** http://www.ThaiCreate.Com ***/
$strName = $_POST["tName"];
$strMessage = $_POST["tMessage"];
$strFileName = "thaicreate.txt";
if($strName != "" and $strMessage != "")
{
//*** Write Text file ***//
$objFopen = fopen("data/".$strFileName, 'a');
$strText1 = "$strName say : $strMessage\r\n";
fwrite($objFopen, $strText1);
fclose($objFopen);
}
//*** Read Text file ***//
$objFopen = fopen("data/".$strFileName, 'r');
if ($objFopen) {
while (!feof($objFopen)) {
$file = fgets($objFopen, 4096);
echo $file."<br>";
}
fclose($objFopen);
}
echo "<div id='mydiv'></div>";
?>
Tag : PHP, Ajax
|
|
|
|
|
|
Date :
2012-02-20 21:24:34 |
By :
matay107 |
View :
1152 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
หรือจะใส่ตรงนี้ก็ได้ครบ
Code (PHP)
<input name="txtName" type="text" id="txtName" size="5" value="<?=$_SESSION["strUser"'];?>">
|
|
|
|
|
Date :
2012-02-21 06:12:34 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|