ช่วยแก้ไขคำสั่งแก้ไขข้อมูลให้ทีครับ.

ทำยังไงให้กดปุ่มแก้ไข  แล้วนำค่าจากตารางมาใส่ใน textbox (ในที่นี้ยังไม่มี) ที่ไฟล์ view-user.php แล้วพอแก้ไขข้อมูลเสร็จกด อัพเดท ค่าในตารางก็จะเปลี่ยนเป็นค่าที่เราอัพเดทล่าสุดครับ



smiley



connect.php



[code]<?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



[code]/*-----------------------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;

    }
09 ต.ค. 2556 1 2,517

ให้เป็นแนวคิดไปละกัน

1.ต้องทำการส่งข้อมูลด้วย Ajax เพื่อให้สามารถรับค่ากลับได้ โดยอ่านบทความเกี่ยวกับ Ajax ทั่วไป บนเว็บนี้ก้มี

2.เมื่อทำการ ส่งค่าไปบันทึกแล้ว ให้ส่งค่าที่ต้องการแสดงผลกลับมาด้วย ซึ่งถ้าใช้ XML หรือ JSON ก็สามารถส่งค่ากลับได้หลายค่า

3.นำค่าที่ได้รับการส่งกลับจาก Ajax ในข้อ 2 ไปใส่ที่ input ที่ต้องการ



ถ้าไม่รู้อะไรเลยลองทำตาม step และแก้ปัญหาเป็นข้อๆตามด้านบนครับ เริ่มต้นจาก

1.ใช้ Ajax ส่งค่าไปบันทึกยังฐานข้อมูลให้ได้ ศึกษาเรื่อง Ajax

2.ศึกษาเรื่อง JSON เพิ่มเติม (แนะนำ) หากต้องการส่งค่ากลับมากกว่า 1 ค่า แต่ถ้าต้องการส่งค่ากลับมาเพียงรายการเดียวก็ไม่จำเป็น

3.ก็นำค่าที่ได้ไปใส่ input ถ้าไม่รู้จะใส่ยังไง ก็ศึกษาเรื่อง DOM เพิ่มเติม แต่โดยทั่วไปจะใช input.value = 'xxx';



ทั้งหมดนี้หาตัวอย่างได้ไม่ยากครับ และบนเว็บนี้ก็มีครับ
#1
ความคิดเห็น
ไฟล์อัปโหลด ชนิด jpg, jpeg ขนาดไฟล์ไม่เกิน 1024
^