|
|
|
ajax แก้ไขข้อมูลอัพเดท และแบ่งหน้า หน้าถัดไปแก้ไขข้อมูลไม่ได้ครับ |
|
|
|
|
|
|
|
Code (PHP)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<?
if($_COOKIE["username"] == "")
{
echo"<script>alert('กรุณา Login');window.location='index.php';</script>";
exit();
}
?>
<style type="text/css">
<!--
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
}
a:hover {
text-decoration: none;
}
a:active {
text-decoration: none;
}
-->
</style></head>
<style type="text/css">
<!--
.paginate {
font-family: Arial, Helvetica, sans-serif;
font-size: .7em;
}
a.paginate {
border: 1px solid #000080;
padding: 2px 6px 2px 6px;
text-decoration: none;
color: #000080;
}
h2 {
font-size: 12pt;
color: #003366;
}
h2 {
line-height: 1.2em;
letter-spacing:-1px;
margin: 0;
padding: 0;
text-align: left;
}
a.paginate:hover {
background-color: #000080;
color: #FFF;
text-decoration: underline;
}
a.current {
border: 1px solid #000080;
font: bold .7em Arial,Helvetica,sans-serif;
padding: 2px 6px 2px 6px;
cursor: default;
background:#000080;
color: #FFF;
text-decoration: none;
}
span.inactive {
border: 1px solid #999;
font-family: Arial, Helvetica, sans-serif;
font-size: .7em;
padding: 2px 6px 2px 6px;
color: #999;
cursor: default;
}
-->
</style>
<script language="javascript">
function check() {
if(document.checkform1.sell.value=="")
{
alert("กรุณากรอก ราคาขาย") ;
document.checkform1.sell.focus() ;
return false ;
}
else
return true ;
}
</script>
<body>
<?
include("config.inc.php");
include("paging.php");
if($_POST['submit'] == "")
{
$objConnect = mysql_connect("localhost","root","1234") or die("Error Connect to Database");
$objDB = mysql_select_db("datab");
$strSQL = "SELECT * FROM price_sell ";
$objQuery1 = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$Num_Rows = mysql_num_rows($objQuery1);
mysql_query("SET NAMES UTF8");
$Page = (isset($_GET['Page']))? intval($_GET['Page']) : 1;
$Per_Page = 4; // Per Page
//$Page = $_GET["Page"];
$limit_start = ($Page-1)*$Per_Page;
if(!$_GET["Page"])
{
$Page=1;
}
$Prev_Page = $Page-1;
$Next_Page = $Page+1;
$Page_Start = (($Per_Page*$Page)-$Per_Page);
if($Num_Rows<=$Per_Page)
{
$Num_Pages =1;
}
else if(($Num_Rows % $Per_Page)==0)
{
$Num_Pages =($Num_Rows/$Per_Page) ;
}
else
{
$Num_Pages =($Num_Rows/$Per_Page)+1;
$Num_Pages = (int)$Num_Pages;
}
$strSQL .=" order by sell ASC LIMIT $Page_Start , $Per_Page";
$objQuery1 = mysql_query($strSQL);
?>
<table width="378" border="0" align="center" cellpadding="0" cellspacing="0">
<form action="f_specify_sell.php" method="POST" name="checkform1" onSubmit="return check()">
<tr>
<td height="25" colspan="3" scope="col"><div align="center"><strong>เพิ่มราคาขาย</strong></div></td>
</tr>
<tr>
<td width="91" height="25"> </td>
<td width="127" height="25"> </td>
<td width="127"> </td>
</tr>
<tr>
<td height="25">ราคา</td>
<td height="25"><input type="text" name="sell" onKeyPress="return number(event,this.value)" /></td>
<td><a href="f_add_price.php"/><img src="pic/add.png" border='0' title='เพิ่มราคาซื้อ' target='window_name'/>กำหนดราคารถ</a> </td>
</tr>
<tr>
<td height="25"> </td>
<td height="25"> </td>
<td> </td>
</tr>
<tr>
<td height="25"><input type="reset" name="Reset" value="ล้างข้อความ" /></td>
<td height="25"><div align="center">
<input type="submit" name="submit" value="บันทึก" />
</div></td>
<td> </td>
</tr>
<tr>
<td height="25"> </td>
<td height="25"></td>
<td> </td>
</tr>
</form>
</table>
<p>
<? }else{
$sell = trim($_POST['sell']);
$sql = "SELECT * FROM price_sell WHERE sell = '$sell' ";
$query = mysql_query($sql) or die (mysql_error());
$result = mysql_fetch_array($query);
if($result)
{
echo " <script language='JavaScript'>window.history.back();alert('ข้อมูลซ้ำโปรดตรวจสอบใหม่อีกครั้ง');</script> ";
}else{
$sql1 = " insert into price_sell (sell) values ('$sell') ";
$query1 = mysql_db_query($dbname,$sql1) or die (mysql_error());
if($query1)
{
echo"<script>alert('บันทึกข้อมูลแล้ว');window.location='f_specify_sell.php';</script>";
}else{
echo"<script>alert('ไม่สามารถบันทึกข้อมูลได้');window.location='f_specify_sell.php';</script>";
}
}
}
?>
</p>
<p> </p>
<?
$objConnect = mysql_connect("localhost","root","1234") or die("Error Connect to Database");
$objDB = mysql_select_db("datab");
//*** Update Condition ***//
if($_POST["hdnCmd"] == "Update")
{
$strSQL = "UPDATE price_sell SET ";
$strSQL .="sell = '".$_POST["sell"]."' ";
$strSQL .="WHERE id_s = '".$_POST["hid_s"]."' ";
$objQuery = mysql_query($strSQL);
if(!$objQuery)
{
echo "Error Update [".mysql_error()."]";
}
//header("location:$_SERVER[PHP_SELF]");
//exit();
}
//*** Delete Condition ***//
if($_GET["Action"] == "Del")
{
$strSQL = "update price_sell set ";
$strSQL .="id_s = '0' ";
$strSQL .="WHERE id_s = '".$_POST["id_s"]."' ";
$objQuery = mysql_query($strSQL);
if(!$objQuery)
{
echo "Error Delete [".mysql_error()."]";
}
//header("location:$_SERVER[PHP_SELF]");
//exit();
}?>
<form name="frmMain" method="post" action="<?=$_SERVER["PHP_SELF"];?>">
<input type="hidden" name="hdnCmd" value="">
<table width="394" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#000000">
<tr>
<th width="131"> <div align="center">ลำดับ</div></th>
<th width="345"> <div align="center">ราคา </div></th>
<th width="62"> <div align="center">แก้ไข </div></th>
<th width="34"> <div align="center">ลบ </div></th>
</tr>
<?
$i = $limit_start;
while($objResult = mysql_fetch_array($objQuery1))
{
$i++;
if($objResult["id_s"] == $_GET["id_s"] and $_GET["Action"] == "Edit")
{
?>
<tr>
<th width="131"> <div align="center">
<input type="text" name="id_s" size="3" style="width:20px" value="<?=$objResult["id_s"];?>">
<input type="hidden" name="hid_s" size="3" value="<?=$objResult["id_s"];?>">
</div></td>
<th width="345"><input type="text" name="sell" size="20" style="width:60px" value="<?=$objResult["sell"];?>"></td>
<td colspan="2" align="right">
<div align="center">
<input name="btnAdd" type="button" id="btnUpdate" value="Update" OnClick="frmMain.hdnCmd.value='Update';frmMain.submit();">
<input name="btnAdd2" type="button" id="btnCancel" value="Cancel" onclick="window.location='<?=$_SERVER["PHP_SELF"];?>';" />
</div></td>
</tr>
<?
}else{
?>
<tr>
<td><div align="center"><?=$i?></div></td>
<td><?=$objResult["sell"];?></td>
<td align="center"><a href="<?=$_SERVER["PHP_SELF"];?>?Action=Edit&id_s=<?=$objResult["id_s"];?>">แก้ไข</a></td>
<td align="center"><a href="JavaScript:if(confirm('Confirm Delete?')==true){window.location='<?=$_SERVER["PHP_SELF"];?>
?Action=Del&id_s=<?=$objResult["id_s"];?>';}">ลบ</a></td>
</tr>
<?
} // else
} // while
?>
</table>
</form>
<br>
ทั้งหมด <?= $Num_Rows;?> แถว
<?
$pages = new Paginator;
$pages->items_total = $Num_Rows;
$pages->mid_range = 10;
$pages->current_page = $Page;
$pages->default_ipp = $Per_Page;
$pages->url_next = $_SERVER["PHP_SELF"]."?QueryString=value&Page=";
$pages->url_pre = $_SERVER["PHP_SELF"]."?QueryString=value&Page=$Prev_Page";
$pages->paginate();
echo $pages->display_pages()
?>
<?
mysql_close($objConnect);
?>
<? include("pp_number.php");?>
</body>
</html>
ตอนนี้ เจอ 2 ปัญหาครับ
1.สามารถอัพเดทข้อมูลได้เฉพาะ หน้า 1 เท่านั้น
2.หลังจาก อัพเดทข้อมูลแล้ว ข้อมูลไม่มีการเปลี่ยนแปลง ต้องกด Refresh จึงจะแสดงข้อมูลที่เปลี่ยนแปลง
รบกวนช่วยชี้แนะด้วยครับ
Tag : PHP, MySQL, Ajax
|
|
|
|
|
|
Date :
2012-07-24 11:48:15 |
By :
nutsza |
View :
1174 |
Reply :
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ใช้แบบปกติเขียนง่ายกกว่าครับ เลือกใช้ Ajax ให้เหมาะสมกับงานครับ
|
|
|
|
|
Date :
2012-07-24 21:30:49 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
อ่อ ขอบคุณครับผม
|
|
|
|
|
Date :
2012-07-25 09:13:48 |
By :
nutsza |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
jQuery Very Good.
|
|
|
|
|
Date :
2012-07-25 09:14:55 |
By :
Krungsri |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|