|
|
|
ช่วยหน่อยครับ ทำ PHP MySql ส่ง เข้าไปฐานข้อมูลไม่ได้ (Ajax Realtime + PHP กับเทคนิคการ Submit Form แบบไม่เปลี่ยนหน้าโดยใช้ Target ไปยัง Iframe) |
|
|
|
|
|
|
|
ช่วยหน่อยครับ ทำ PHP mysql ส่ง เข้าไปฐานข้อมูลไม่ได้ (Ajax Realtime + PHP กับเทคนิคการ Submit Form แบบไม่เปลี่ยนหน้าโดยใช้ Target ไปยัง Iframe)
ถ้าเอา Ajax Realtime มาใส่มัน Add เข้า mysql ไม่ได้เเต่ถ้าเอา Ajax Realtime ออก มันจะ Add ได้ ต้องแก้ยังไงบอกหน่อยครับ
Code (PHP)
<?php
/*** By Weerachai Nukitram ***/
/*** http://www.ThaiCreate.Com ***/
?>
<html>
<head>
<title>ThaiCreate.Com Ajax Tutorial</title>
</head>
<script language="JavaScript">
var HttPRequest = false;
function doCallAjax(Sort) {
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 = 'chat_main.php';
var pmeters = 'mySort='+Sort;
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>
<body Onload="bodyOnload();">
<h1>My Customer</h1>
<form name="frmMain" action="" method="post">
<script language="JavaScript">
var refresh = '';
function bodyOnload()
{
doCallAjax('chat_id')
refresh = setTimeout("doLoop();",3000);
}
function doLoop()
{
bodyOnload();
}
function stopRefresh()
{
clearTimeout(refresh);
}
</script>
<span id="mySpan" onMouseOver="stopRefresh()" onMouseOut="doLoop()"></span>
<form action="chat_add.php" name="frmMain" method="post" target="iframe_target">
<iframe id="iframe_target" name="iframe_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>
<script language="JavaScript">
function showResult(result)
{
if(result==1)
{
document.getElementById("divResult").innerHTML = "<font color=green> Save successfully! </font> <br>";
}
else
{
document.getElementById("divResult").innerHTML = "<font color=red> Error!! Cannot save data </font> <br>";
}
}
</script>
<div id="divResult"></div>
<table width="320" border="1">
<tr>
<th>chat_detail </th>
<td><input type="text" name="chat_detail" size="20" id="chat_detail"></td>
</tr>
<tr>
<th>chat_send_user</th>
<td><input type="text" name="chat_send_user" size="20" id="chat_send_user"></td>
</tr>
</table>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Code (PHP)
<?
$objConnect = mysql_connect("localhost","???????????","???????????") or die("Error Connect to Database");
$objDB = mysql_select_db("???????????");
$chat_detail = $_POST['chat_detail'];
$chat_send_user = $_POST['chat_send_user'];
$result = mysql_query("insert into chat (chat_detail,chat_send_user)
values ('$chat_detail','$chat_send_user')") ;
if($result)
{
echo "<script>alert('Save successfully!');</script>";
/* echo "<script>window.top.window.showResult('1');</script>"; */
}
else
{
echo "<script>alert('Error! Cannot save data');</script>";
/* echo "<script>window.top.window.showResult('2');</script>"; */
}
mysql_close($objConnect);
?>
Code (PHP)
<?php
/*** By Weerachai Nukitram ***/
/*** http://www.ThaiCreate.Com ***/
echo "Load Time : ".date("Y-m-d H:i:s");
$strSort = $_POST["mySort"];
$objConnect = mysql_connect("localhost","???????????","???????????") or die("Error Connect to Database");
$objDB = mysql_select_db("???????????");
$strSQL = "SELECT * FROM chat ORDER BY $strSort ASC ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<table width="600" border="1">
<tr>
<th> </th>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td><div align="center"><?=$objResult["chat_detail"];?></div></td>
</tr>
<?
}
?>
</table>
<?
mysql_close($objConnect);
?>
Tag : PHP, MySQL, HTML/CSS, JavaScript, CakePHP
|
ประวัติการแก้ไข 2013-08-24 03:17:15
|
|
|
|
|
Date :
2013-08-24 03:16:48 |
By :
aibot |
View :
766 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ไม่เปลี่ยนไปใช้พวก jQuery + Ajax ล่ะครับ เขียนง่ายกกว่าเยอะเลย
|
|
|
|
|
Date :
2013-08-24 11:45:27 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|