|
|
|
สอบถามการเขียนเว็บเชื่อมกับ Data base หน่อยครับ พรีสส |
|
|
|
|
|
|
|
เหลือแค่เขียนโค้ด ให้กด button "X" แล้วตาราง แถว ID ลบ หรือหายไปครับ
*ช่วยทีคับผมคิดไม่ออกละ ใครพอรู้อะไร หรือต้องการเพิ่มเติมช่วยหน่อยนะครับ
Code (Main.js)
/// <reference path="../../assets/js/jquery.d.ts" />
$(document).ready(main);
function main() {
$(document.body).load("View/main.html", "", function() {
$.ajax("Model/request.php?req=0", {
dataType: "json",
success: function(data) {
if (data.state == 0) { // state = 0 : load login module
login_work();
} else { // state != 0 : load logout module
logout_work(data.username);
}
}
});
});
}
function login_work() {
$("#content_area").html("");
$("#authen_area").html("");
$("#authen_area").load("View/module_login.html", "", function() {
$("#btn_do_login").click(function() {
var username = $("#txt_username").val();
var password = $("#txt_password").val();
$.ajax("Model/request.php?req=1", {
data: {"username":username, "password":password},
dataType: "json",
method: "post",
success: function(data) {
if (data.status == true) { // login success
logout_work(data.username);
} else { // login failed
alert("LOGIN FAILED : username OR password INVALID!");
}
}
});
});
});
}
function logout_work(username) {
$("#authen_area").html(""); // clear all contents in #authen_area
$("#authen_area").load("View/module_logout.html", "", function() {
$("#username").html(username);
$("#btn_logout").click(function() {
$.ajax("Model/request.php?req=2", {
success: function() {
login_work();
}
});
});
// call user_work()
users_work();
});
}
function users_work(){
$("#content_area").load("View/module_users.html", function() {
update_users();
$("#btn_add").click(function() {
var username = $("#new_username").val();
var password = $("#new_password").val();
console.log (username,password);
$.ajax("Model/request.php?req=3",{
data: {"username":username,"password":password},
method: "post",
dataType: "json",
success: function(data) {
if (data.status == true){
$("#new_username").val("");
$("#new_password").val("");
// update table
update_users();
} else {
alert(data.message);
}
}
});
});
});
function update_users(){
$.ajax("Model/request.php?req=4", {
dataType: "json",
success: function(data) {
console.log(data);
if (data.status == true) {
var tbody = $("#table_body");
tbody.html (""); // เคลียร์ข้อมูลเก่า
for (var i = 0; i < data.data.length; i++) {
tbody.append(
"<tr>"+
"<td>"+data.data[i][0]+"</td>"+
"<td>"+data.data[i][1]+"</td>"+
"<td>********</td>"+
"<td><button>X</button></td>"+
"</tr>"
);
}
}
}
});
}
}
ส่วนของ request.php (PHP)
<?php
session_start();
include_once("db.php");
$output = array();
$req = (int)$_REQUEST["req"];
if ($req == 0) {
$_SESSION["state"] = (empty($_SESSION["state"]))? 0 : $_SESSION["state"];
$output["state"] = $_SESSION["state"];
$output["username"] = $_SESSION["username"];
}
// req = 1 : login
else if ($req == 1) {
$username = $_REQUEST["username"];
$password = $_REQUEST["password"];
if (get_user($username, $password) == $username) { // login success
$_SESSION["state"] = 1;
$_SESSION["username"] = $username;
$output["status"] = true;
$output["username"] = $username;
} else { // login failed
$output["status"] = false;
}
}
// req = 2 : logout
else if ($req == 2) {
$_SESSION["state"] = 0;
}
// req = 3 : add users
else if ($req == 3) {
$username = $_REQUEST["username"];
$password = $_REQUEST["password"];
$result = add_user($username, $password);
if ($result = "succes") {
$output ["status"] = true;
} else {
$output ["status"] = false;
$output ["message"] = $result;
}
}
// req = 4 : get users data
else if ($req == 4) {
$data = read_users($_SESSION["username"]);
if ($data) {
$output ["status"] = true;
$output ["data"] = $data;
} else {
$output ["status"] = false;
}
}
echo json_encode($output);
?>
Db .php (PHP)
<?php
define("DB_HOST", "localhost");
define("DB_USERNAME", "root");
define("DB_PASSWORD", "1234"); // 1234
define("DB_NAME", "mvc_sample_03");
define("DB_CHARSET", "utf8");
$linker = mysql_pconnect(DB_HOST, DB_USERNAME, DB_PASSWORD);
if (mysql_errno($linker) != 0) {
echo mysql_error($linker);
exit(0);
}
mysql_set_charset(DB_CHARSET, $linker);
mysql_select_db(DB_NAME, $linker);
function get_user($username, $password) {
global $linker;
//$password = md5($password);
$sql = "SELECT * FROM users WHERE username = '$username' AND password = PASSWORD('$password');";
$result = mysql_query($sql, $linker);
if ($result) {
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_row($result);
return $row[1];
}
} else {
echo $sql;
}
return "";
}
function add_user ($username, $password) {
global $linker;
$sql = "INSERT INTO users (username, password) VALUES('$username',PASSWORD('$password'));";
$result = mysql_query($sql,$linker);
if (mysql_errno() == 1062){
return "Duplicate Username \"$username\"";
}
return "success";
}
function read_users ($username) {
global $linker;
$sql = "SELECT id, username FROM users WHERE username NOT IN ('admin','$username');";
$result = mysql_query($sql, $linker);
if ($result) {
if (mysql_num_rows($result) > 0 ) {
$records = array();
while ($row = mysql_fetch_row($result)) {
$records[count($records)] = $row;
}
return $records;
}
}
return null;
}
?>
Tag : HTML/CSS, JavaScript, Ajax, jQuery, Web Hosting
|
|
|
|
|
|
Date :
2016-11-28 11:33:31 |
By :
autindy44 |
View :
942 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ใน button x กำหนด id class name หรืออะไรสักอย่างให้มันแตกต่างกัน และให้มันอ้างอิง id ตรงกับแถวที่เราจะลบครับ
เคยทำไว้ง่าย ๆ ลองเล่นดู กดเพิ่ม แล้วลบ กดเพิ่ม แล้วลบ http://jsfiddle.net/kla_61/p41c9cv4/28/
|
|
|
|
|
Date :
2016-11-30 09:54:21 |
By :
apisitp |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|