|
|
|
โค๊ด ajax ไม่แสดงผลภาษาไทย แก้ยังไงก็ไม่หาย ช่วยทีครับท่าน |
|
|
|
|
|
|
|
นำ AJAX โค๊ดจากที่นี่ไปประยุกต์ใช้แต่มันไม่แสดงผลภาษาไทยลองหาวิธีดูแล้วก็ยังไม่ได้
ผมทำดังนี้
-ฐานข้อมูลเป็น utf-8
-ไฟล์เซฟเป็น utf-8
AjaxPHPContent1.php
Code (PHP)
<?php require_once('Connections/gd.php'); ?>
<?php require_once('Connections/ajax.php'); ?>
<?php
mysql_select_db($database_gd, $gd);
$query_Recordset1 = "SELECT * FROM product";
$Recordset1 = mysql_query($query_Recordset1, $gd) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<?php
/*** By Weerachai Nukitram ***/
/*** http://www.ThaiCreate.Com ***/
?>
<html>
<head>
<title></title>
<script language="JavaScript">
var HttPRequest = false;
function doCallAjax(ID) {
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 = 'AjaxPHPContent2.php';
var pmeters = "tID="+ID;
HttPRequest.open('POST',url,true);
HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf8");
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;
}
}
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body Onload="JavaScript:doCallAjax('1');">
<h1>My Content</h1>
<table width="577" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="104" valign="top"><select name="ddlGeo" size="<? echo $totalRows_type?>" id="ddlGeo" onChange = "doCallAjax(this.value)">
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<option value="<?=$objResult["rid"];?>" >
<?=$objResult["rnam"];;?>
</option>
<?
}
?>
</select>
<?
mysql_close($objConnect);
?>
</td>
<td width="540" valign="top"><span id="mySpan"></span></td>
</tr>
</table>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>
AjaxPHPContent2.php
Code (PHP)
<?php
header("Content-Type: text/plain; charset=utf-8");
$strID = $_POST["tID"];
$objConnect = mysql_connect("localhost","root","admin") or die("Error Connect to Database");
$objDB = mysql_select_db("gd_system");
$strSQL = "SELECT * FROM product WHERE rid = '".$strID."' ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$objResult = mysql_fetch_array($objQuery);
mysql_query("SET NAMES UTF8");
echo nl2br($objResult["rpri"]);
mysql_close($objConnect);
?>
Tag : PHP, Ajax
|
ประวัติการแก้ไข 2012-06-24 17:02:35
|
|
|
|
|
Date :
2012-06-24 15:31:24 |
By :
sawadeethai |
View :
1228 |
Reply :
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ไฟล์ที่ require สองอันบน อันไหนติดต่อฐานข้อมูลครับ แล้วได้ SET NAME มั้ยครับ
|
|
|
|
|
Date :
2012-06-24 22:27:39 |
By :
Pine |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
อันนี้สคริปต์ทั้งหมดที่มี
gd.php
Code (PHP)
<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_gd = "localhost";
$database_gd = "gd_system";
$username_gd = "root";
$password_gd = "admin";
$gd = mysql_pconnect($hostname_gd, $username_gd, $password_gd) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_query("SET NAMES UTF8")
?>
ajax.php
Code (PHP)
<?
$objConnect = mysql_connect("localhost","root","admin") or die("Error Connect to Database");
$objDB = mysql_select_db("gd_system");
$strSQL = "SELECT * FROM product ORDER BY rnam ASC ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
mysql_query("SET NAMES UTF8");
$totalRows_type = mysql_num_rows($objQuery);;
?>
AjaxPHPContent1.php
Code (PHP)
<?php require_once('Connections/gd.php'); ?>
<?php require_once('Connections/ajax.php'); ?>
<?php
mysql_select_db($database_gd, $gd);
$query_Recordset1 = "SELECT * FROM product";
$Recordset1 = mysql_query($query_Recordset1, $gd) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
mysql_query("SET NAMES UTF8")
?>
<?php
/*** By Weerachai Nukitram ***/
/*** http://www.ThaiCreate.Com ***/
?>
<html>
<head>
<title>ThaiCreate.Com Ajax Tutorial</title>
<script language="JavaScript">
var HttPRequest = false;
function doCallAjax(ID) {
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 = 'AjaxPHPContent2.php';
var pmeters = "tID="+ID;
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;
}
}
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>
<body Onload="JavaScript:doCallAjax('1');">
<h1>My Content</h1>
<table width="577" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="104" valign="top"><select name="ddlGeo" size="<? echo $totalRows_type?>" id="ddlGeo" onChange = "doCallAjax(this.value)">
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<option value="<?=$objResult["rid"];?>" >
<?=$objResult["rnam"];;?>
</option>
<?
}
?>
</select>
<?
mysql_close($objConnect);
?>
</td>
<td width="540" valign="top"><span id="mySpan"></span></td>
</tr>
</table>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>
AjaxPHPContent2.php
Code (PHP)
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
$strID = $_POST["tID"];
$objConnect = mysql_connect("localhost","root","admin") or die("Error Connect to Database");
$objDB = mysql_select_db("gd_system");
$strSQL = "SELECT * FROM product WHERE rid = '".$strID."' ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$objResult = mysql_fetch_array($objQuery);
mysql_query("SET NAMES UTF8");
echo nl2br($objResult["rpri"]);
mysql_close($objConnect);
?>
|
|
|
|
|
Date :
2012-06-25 18:56:40 |
By :
sawadeethai |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ให้ SET NAME หลังจากเชื่อมต่อกับฐานข้อมูลครับ
ajax.php
Code (PHP)
<?
$objConnect = mysql_connect("localhost","root","admin") or die("Error Connect to Database");
mysql_query("SET NAMES UTF8");
$objDB = mysql_select_db("gd_system");
$strSQL = "SELECT * FROM product ORDER BY rnam ASC ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$totalRows_type = mysql_num_rows($objQuery);
?>
ปล. ไฟล์ AjaxPHPContent1.php ติด BOM นะครับ ถ้ามีไม่แสดงภาษาไทย ให้เซฟแอสใหม่นะครับ
|
|
|
|
|
Date :
2012-06-25 21:09:16 |
By :
Pine |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|