ทำยังไงให้กดปุ่มแก้ไข แล้วนำค่าจากตารางมาใส่ใน textbox (ในที่นี้ยังไม่มี) ที่ไฟล์ view-user.php แล้วพอแก้ไขข้อมูลเสร็จกด อัพเดท ค่าในตารางก็จะเปลี่ยนเป็นค่าที่เราอัพเดทล่าสุดครับ.
connect.php
<?php
$objConnect = mysql_connect("localhost","username","password") or die(mysql_error());
$objDB = mysql_select_db("database");
mysql_query("SET NAMES utf8", $objConnect);
?>
view-user.php
<?php
ob_start();
session_start();
include "connect.php";
?>
<!doctype html>
<head>
<script type="text/javascript" src="scripts/user.js"></script>
</head>
<body>
<tr>
<th>Member ID</th>
<th>Email</th>
<th>Display Name</th>
<th>Grant</th>
<th>Active</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<tr>
<?php
$sql = "select * from tb_member";
$result = mysql_query($sql);
$num = mysql_num_rows($result);
$i = 0;
while ($i < $num) {
$row = mysql_fetch_array($result);
$userid = $row['member_id'];
$email = $row['member_email'];
$name = $row['member_disname'];
$grant = $row['grant'];
$active = $row['active'];
echo "<tr>";
echo "<td>$userid</td>";
echo "<td>$email</td>";
echo "<td>$name</td>";
echo "<td>$grant</td>";
echo "<td>$active</td>";
$show_id = $row['member_id'];
echo "<td><span id='edit' OnClick='show_members($show_id)'>
<img border=0 src='images/icn_edit.png'/></span></td>";
$del_id = $row['member_id'];
echo "<td><span id='del' onclick='del_members($del_id)'>
<img border=0 src='images/icn_trash.png'></span></td>";
echo "</tr>";
$i ;
}
?>
</tr>
user.js
/*-----------------------SHOW----------------------*/
function show_members(show_id){
alert(show_id);
var str=Math.random();
var datastring='str=' str '&show_id=' show_id ;
$.ajax({
type:'POST',
url:'user_process.php',
data:datastring,
beforeSend: function(){
$("#show_id").html('<img src="loading2.gif">');
},
success:function(data){
$("#show_id").html(data);
}
});
}
/*-----------------------EDIT----------------------*/
function edit_members(edit_id){
alert(edit_id);
var str=Math.random();
var username = document.myform.username.value;
var password = document.myform.password.value;
var email = document.myform.email.value;
var datastring='str=' str '&edit_id=' edit_id '&agent=' grant
'&active=' active;
$.ajax({
type:'POST',
url:'user_process.php',
data:datastring,
beforeSend: function(){
$("#show_id").html('<img src="loading2.gif">');
},
success:function(data){
window.location.reload("view-user.php");
}
});
}
/*-----------------------DEL----------------------*/
function del_members(del_id){
alert(del_id);
var str=Math.random();
var datastring='str=' str '&del_id=' del_id;
$.ajax({
type:'POST',
url:'user_process.php',
data:datastring,
beforeSend: function(){
$("#show_id").html('<img src="loading2.gif">');
},
success:function(data){
alert (data);
window.location.reload("view-user.php");
}
});
}
user_process.php
<?php
include 'connect.php';
/*-----------------SHOW MEMBERS FOR EDIT--------------*/
$show_id = $_POST['show_id'];
if($_POST['show_id']){
$sql = "select * from tb_member where member_id = '$show_id'";
$result = mysql_query($sql);
$row= mysql_fetch_array($result);
$edit_id = $row['member_id'];
$edit_grant = $row['grant'];
$edit_active = $row['active'];
echo "<div id = 'edit_id'>";
echo "<form name = myform>";
echo "<div style='margin-left:22px;margin-bottom:8px;'>
grant:
<input type='text' name='grant' value='$grant'size=25></div>";
echo "<div style='margin-left:25px;margin-bottom:8px;'>
active:
<input type='text' name='active' value='$active'size=25></div>";
echo "<div>
<input type='button' onclick='edit_members($edit_id)'
value='Edit'></div>";
echo "</form>";
echo "</div>";
}
/*--------------------EDIT MEMBERS---------------------*/
$edit_id = $_POST['edit_id'];
$grant = $_POST['grant'];
$active = $_POST['active'];
if($_POST['edit_id']){
if($grant =="" || $active ==""){
echo "error";
}
else{
$sql = "update tb_member set
grant = '$grant',
active = '$active',
where member_id = '$edit_id'";
mysql_query($sql);
echo "success";
}
}
/*--------------------DEL MEMBERS---------------------*/
$del_id = $_POST['del_id'];
if($_POST['del_id']){
$sql = "delete from tb_member where member_id = '$del_id'";
echo $sql;
$result = mysql_query($sql);
echo $result;
}
ปัญหาคือว่า
1. ปุ่ม Edit กดแล้วไม่ทำงานเลยครับ
2. ปุ่ม delete ทำงาน แต่ไม่ลบข้อมูลให้ครับ
:wanwan017:
ไม่รู้ว่าไปพลาดตรงไหน[/code]
Tag : PHP, HTML/CSS, Ajax, jQuery