亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 42
粉絲 2
評(píng)論 0
訪問(wèn)量 63569
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
完善員工管理系統(tǒng)實(shí)現(xiàn)添加刪除修改功能(實(shí)現(xiàn)員工的增刪改 作業(yè)軟刪除)2019年2月27日22時(shí)
小明的博客
原創(chuàng)
3963人瀏覽過(guò)

今天,課上學(xué)習(xí)了員工管理系統(tǒng)的添加 刪除 修改,作業(yè)時(shí)作出軟刪除。廢話不多說(shuō)上代碼,談邏輯。

一、實(shí)現(xiàn)添加功能

在staff_list上的添加按鈕設(shè)置導(dǎo)航到,staff_add.php,然后新建staff_add.php,然后構(gòu)建前端頁(yè)面,在新增按鈕設(shè)置add方法傳入this.form,重點(diǎn)在js的add方法,add方法中要做ajax數(shù)據(jù)提交將form下的name age position sex mobile的值傳遞到staff_manage.php,這里需要注意的是,提交按鈕里面需要連接到staff_manage,將action=add通過(guò)get方法傳過(guò)來(lái);然后在staff_manage連接數(shù)據(jù)庫(kù),將cation復(fù)制給$action,switch判斷,當(dāng)action為add時(shí),那么就執(zhí)行添加數(shù)據(jù)到數(shù)據(jù)庫(kù)的操作,然后判斷,當(dāng)數(shù)據(jù)庫(kù)添加操作被執(zhí)行時(shí)——》(然后再進(jìn)行判斷,影響行數(shù)為1時(shí),status為1,message為添加數(shù)據(jù)成功;影響行數(shù)為0時(shí),status為0,message為沒(méi)有添加數(shù)據(jù));數(shù)據(jù)庫(kù)沒(méi)有執(zhí)行操作時(shí)——》(status為-1,message為操作錯(cuò)誤),判斷完成后,把status和message專程json格式傳回前端staff_add,staff_add頁(yè)面在ajax返回響應(yīng)數(shù)據(jù)階段,判斷如果status時(shí)1添加正確css樣式,否則為錯(cuò)誤樣式,然后把message賦給tips的內(nèi)容,再添加定時(shí)器要2秒鐘后返回index,至此添加功能完成。

實(shí)例

<?php session_start(); ?>
<?php if (!isset($_SESSION['username'])): ?>
    <style>h2,p{text-align: center;margin-top: 50px;}</style>
    <h2>您還沒(méi)有登錄呢~~</h2>
    <p>正在跳轉(zhuǎn)到登錄頁(yè)面...</p>
    <script>
        setTimeout("location.assign('login.php')", 2000);
    </script>
<?php else:?>
<!doctype html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>添加員工</title>
    <style>
        h3 {
            text-align: center;
        }

        div {
            width: 300px;
            height: 260px;
            /*background-color: lightblue;*/
            margin: 0 auto;
            text-align: center;
            padding: 20px;
            border: 1px dashed #888;
            border-radius: 5%;
        }

        div input {
            background-color: transparent;
            border: none;
            border-bottom: 1px solid #333;
        }

        button:hover {
            cursor: pointer;
            background-color: lightblue;
        }

        .success {
            color: green;
        }
        .error {
            color: red;
        }

    </style>
</head>
<body>
<h3>添加員工</h3>
<div>
    <form name="user">

        <p>
            <label for="name">姓名:</label>
            <input type="text" name="name" id="name" value="">
        </p>
        <p>
            <label for="age">年齡:</label>
            <input type="number" name="age" id="age" value="">
        </p>
        <p>
            <label for="position">職位:</label>
            <input type="text" name="position" id="position" value="">
        </p>
        <p>
            <label for="mobile">手機(jī):</label>
            <input type="text" name="mobile" id="mobile" value="">
        </p>
        <p style="margin-left: -55px;">性別:
            <input type="radio" id="male" name="sex" value="1" checked><label for="male">男</label>
            <input type="radio" id="female" name="sex" value="0"><label for="female">女</label>
        </p>
        <p>
            <button type="button" onclick="add(this.form)">新增</button>
            <button type="button" onclick="history.back()">取消</button>
        </p>
    <!--    提示占位符-->
        <p></p>
    </form>
</div>

<script>
    function add(form) {

        // ajax方式交互數(shù)據(jù)
        var request = new XMLHttpRequest();
        //    請(qǐng)求完成獲得相應(yīng)數(shù)據(jù)
        request.onreadystatechange = function () {
            if (request.readyState === 4 && request.status === 200) {
                // 將返回的json字符串解析成對(duì)象
                var data = JSON.parse(request.responseText);
            //    獲取tips元素
                var tips = form.lastElementChild;
                tips.innerText = data.message;
            //    判斷返回來(lái)的status值  1 成功  反之錯(cuò)誤
                if (data.status === 1) {
                    tips.classList.add('success');
                } else {
                    tips.classList.add('error');
                }
                // 兩秒后  返回上一層也就是staff_list
                setTimeout(function(){
                    history.back();
                    top.location.reload();
                },2000);
            }
        };
        //配置請(qǐng)求信息
        request.open('POST', 'staff_manage.php?action=add');
        // ajax請(qǐng)求頭
        request.setRequestHeader('content-type','application/x-www-form-urlencoded');
        // 發(fā)送的數(shù)據(jù)集合成json數(shù)據(jù)
        var data = {
            name:form.name.value,
            age:form.age.value,
            position:form.position.value,
            mobile:form.mobile.value,
            sex:form.sex.value
        };
        var staff = 'name=' + data.name +
                '&age=' + data.age +
                '&position=' + data.position +
                '&sex=' + data.sex +
                '&mobile=' + data.mobile;

        // 發(fā)送請(qǐng)求數(shù)據(jù)
        request.send(staff);
    }


</script>
</body>
</html>
<?php endif?>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例


二、更新操作

更新操作總體思路和添加一樣,也是通過(guò)get傳給staff_manage一個(gè)action=save值 ,通過(guò)判斷save后然后在執(zhí)行更新操作,根據(jù)執(zhí)行結(jié)果返回響應(yīng)的status值和message值給前臺(tái),前臺(tái)在進(jìn)行相應(yīng)的樣式添加。這里的細(xì)節(jié)主要有,在staff_list的編輯按鈕通過(guò)隱藏域,把數(shù)據(jù)庫(kù)中的id用get方式傳遞到新建頁(yè)面staff_edit頁(yè)面,這也頁(yè)面的樣式同添加頁(yè)面一直,只不過(guò)表單的值時(shí)通過(guò)id然后數(shù)據(jù)庫(kù)查詢添加上去的,這里的修改提交按鈕還是通過(guò)ajax異步post傳給staff_manage,然后在進(jìn)行數(shù)據(jù)庫(kù)操作。

實(shí)例

<?php
//echo $_GET['id'];
//連接數(shù)據(jù)庫(kù)
$pdo = new PDO('mysql:dbname=php', 'root', 'root');
//獲取準(zhǔn)備對(duì)象
$stmt = $pdo->prepare("SELECT * FROM `staff` WHERE `id`=:id");
//$stmt->debugDumpParams();
//執(zhí)行操作
$stmt->execute(['id'=>$_GET['id']]);
//獲取記錄
$staff = $stmt->fetch(PDO::FETCH_ASSOC);
//print_r($staff);
?>
<!doctype html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>編輯員工</title>
    <style>
        h3 {
            text-align: center;
        }

        div {
            width: 300px;
            height: 260px;
            /*background-color: lightblue;*/
            margin: 0 auto;
            text-align: center;
            padding: 20px;
            border: 1px dashed #888;
            border-radius: 5%;
        }

        div input {
            background-color: transparent;
            border: none;
            border-bottom: 1px solid #333;
            color: #777;
            text-align: center;
            font-size: inherit;
        }

        button:hover {
            cursor: pointer;
            background-color: lightblue;
        }

        .success {
            color: green;
        }
        .error {
            color: red;
        }

    </style>
</head>
<body>
<h3>編輯員工</h3>
<div>
    <form name="user">
        <p>
            <label for="name">姓名:</label>
            <input type="text" name="name" id="name" value="<?=$staff['name']?>">
        </p>
        <p>
            <label for="age">年齡:</label>
            <input type="number" name="age" id="age" value="<?=$staff['age']?>">
        </p>
        <p>
            <label for="position">職位:</label>
            <input type="text" name="position" id="position" value="<?=$staff['position']?>">
        </p>
        <p>
            <label for="mobile">手機(jī):</label>
            <input type="text" name="mobile" id="mobile" value="<?=$staff['mobile']?>">
        </p>
        <p style="margin-left: -55px;">性別:
            <input type="radio" id="male" name="sex" value="1"><label for="male">男</label>
            <input type="radio" id="female" name="sex" value="0"><label for="female">女</label>
        </p>
        <script>
            var male = document.getElementById('male');
            var female = document.getElementById('female');
            // 將male female的值變成整數(shù) 然后判斷是否checked
            if (parseInt(male.value) === <?= $staff['sex']?>) {
                male.checked = true;
            }
            if (parseInt(female.value) === <?= $staff['sex']?>) {
                female.checked = true;
            }
        </script>
        <p><input type="hidden" name="id" value="<?= $staff['id']?>"></p>
        <p>
            <button type="button" onclick="save(this.form)">修改</button>
            <button type="button" onclick="history.back()">取消</button>
        </p>
        <!--    提示占位符-->
        <p></p>
    </form>
</div>
</body>
<script>
    function save(form) {
        // 用ajax方式將數(shù)據(jù)異步提交
        var request = new XMLHttpRequest();
        // 請(qǐng)求完成獲得響應(yīng)數(shù)據(jù)
        request.onreadystatechange = function () {
            if (request.readyState === 4 && request.status === 200) {
                var data = JSON.parse(request.responseText);
                var tips = form.lastElementChild;
                tips.innerText = data.message;
                if (data.status === 1) {
                    tips.classList.add('success');
                } else {
                    tips.classList.add('error');
                }
                setTimeout(function () {
                    history.back();
                    top.location.reload();
                }, 2000);
            }
        };
    //    配置請(qǐng)求信息
        request.open('POST', "staff_manage.php?action=save");
        // post方式請(qǐng)求頭
        request.setRequestHeader('content-type','application/x-www-form-urlencoded');
        // 設(shè)置數(shù)據(jù)
        var data = {
            name:form.name.value,
            age:form.age.value,
            position:form.position.value,
            sex:form.sex.value,
            mobile:form.mobile.value,
            id:form.id.value
        };
        var staff = 'name=' + data.name +
                    '&age=' + data.age +
                    '&position=' + data.position +
                    '&sex=' + data.sex +
                    '&mobile=' + data.mobile +
                    '&id=' + data.id;
        // 發(fā)送請(qǐng)求數(shù)據(jù)
        request.send(staff);
    }
</script>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

三、刪除操作

在staff_list里的按鈕設(shè)置超鏈接 confirm(‘是否刪除’)?location.assign('staff_manage.php?action=del&id=<?=$staff['id']?>') : false"   然后在staff_manage  del方法,如果執(zhí)行成功就彈窗刪除成功。

實(shí)例

<?php
//連接數(shù)據(jù)庫(kù)
$pdo = new PDO('mysql:dbname=php','root','root');
//獲取action的get傳值
$action = strtolower(trim($_GET['action']));
//根據(jù)action值判斷應(yīng)該采用的處理方法
switch ($action) {
//    新增方法
    case 'add':
//        新增sql語(yǔ)句
        $sql = 'INSERT INTO `staff`
                (`name`, `age`, `sex`, `position`, `mobile`, `hiredate`) VALUES 
                (:name, :age, :sex, :position, :mobile, :hiredate);';
//        準(zhǔn)備對(duì)象
        $stmt = $pdo->prepare($sql);
//        將post傳過(guò)來(lái)的值賦給相應(yīng)的變量
        $name = trim($_POST['name']);
        $age = trim($_POST['age']);
        $sex = trim($_POST['sex']);
        $position = trim($_POST['position']);
        $mobile = trim($_POST['mobile']);
        $hiredate = time();
//        數(shù)據(jù)綁定
        $stmt->bindValue('name', $name, PDO::PARAM_STR);
        $stmt->bindValue('age', $age, PDO::PARAM_INT);
        $stmt->bindValue('sex', $sex, PDO::PARAM_INT);
        $stmt->bindValue('position', $position, PDO::PARAM_STR);
        $stmt->bindValue('mobile', $mobile, PDO::PARAM_STR);
        $stmt->bindValue('hiredate', $hiredate, PDO::PARAM_INT);
//        如果執(zhí)行成功,切影響行數(shù)等于1那么就是新增成功了
       if($stmt->execute()){

            if ($stmt->rowCount() == 1) {
                $status = 1;
                $message = '新增成功';
            } elseif ($stmt->rowCount() == 0) {
                $status = 0;
                $message = '沒(méi)有新增';
            }
        } else {
           $status = -1;
           $message = '發(fā)生錯(cuò)誤';
        }
//        輸出json格式的status message值
        echo json_encode(['status'=>$status, 'message'=>$message]);
        break;
    case 'save':

        $sql = "UPDATE `staff` 
                SET `name`=:name, `age`=:age, `position`=:position, `sex`=:sex, `mobile`=:mobile
                WHERE `id`=:id";
        $stmt = $pdo->prepare($sql);
        $name = trim($_POST['name']);
        $age = trim($_POST['age']);
        $position = trim($_POST['position']);
        $sex = trim($_POST['sex']);
        $mobile = trim($_POST['mobile']);
        $id = trim($_POST['id']);
        $stmt->bindValue('name', $name, PDO::PARAM_STR);
        $stmt->bindValue('age', $age, PDO::PARAM_INT);
        $stmt->bindValue('position', $position, PDO::PARAM_STR);
        $stmt->bindValue('sex', $sex, PDO::PARAM_INT);
        $stmt->bindValue('mobile', $mobile, PDO::PARAM_STR);
        $stmt->bindValue('id', $id, PDO::PARAM_INT);


        if ($stmt->execute()) {

            if ($stmt->rowCount() == 1) {
                $status = 1;
                $message = '更新成功';
            } else if ($stmt->rowCount() == 0) {
                $status = 0;
                $message = '沒(méi)有更新數(shù)據(jù)';
            }
        } else {

            $status = -1;
            $message = '更新發(fā)送錯(cuò)誤';
        }
        echo json_encode(['status'=>$status, 'message'=>$message]);
        break;
    case 'del':
        /*
         * 這是正常刪除
         * $sql = 'DELETE FROM `staff` WHERE `id`=:id';
        $stmt = $pdo->prepare($sql);
        if ($stmt->execute(['id'=>$_GET['id']])) {
            echo "<script>alert('刪除成功');location.assign('index.php')</script>";
        }*/

        /*軟刪除*/
        $sql = "UPDATE `staff` SET `is_show`=0 WHERE `id`=:id";
        $stmt = $pdo->prepare($sql);
        if ($stmt->execute(['id'=>$_GET['id']])) {
            echo "<script>alert('刪除成功');location.assign('index.php')</script>";
        }
}

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

四、作業(yè):軟刪除

通過(guò)在數(shù)據(jù)庫(kù)添加is_show字段,默認(rèn)值為1顯示,0為不顯示,然后在staff_manage中的del中設(shè)置為id對(duì)應(yīng)的數(shù)據(jù)的is_show值為0,在staff_list設(shè)置is_show的為1的才顯示 就成了

五、總結(jié)

添加刪除修改 這三個(gè)操作相同的都是通過(guò)前端相應(yīng)的數(shù)據(jù)傳輸?shù)胶蠖?然后后段判斷  進(jìn)行相應(yīng)的操做,不同的是添加和修改都需要頁(yè)面展示。

我需要注意的還是對(duì)js的操作,主要對(duì)ajax的操作,對(duì)超鏈接的定向location  還有便捷的找到相應(yīng)的元素不熟悉



批改狀態(tài):未批改

老師批語(yǔ):
本博文版權(quán)歸博主所有,轉(zhuǎn)載請(qǐng)注明地址!如有侵權(quán)、違法,請(qǐng)聯(lián)系admin@php.cn舉報(bào)處理!
全部評(píng)論 文明上網(wǎng)理性發(fā)言,請(qǐng)遵守新聞評(píng)論服務(wù)協(xié)議
0條評(píng)論
關(guān)于我們 免責(zé)申明 意見(jiàn)反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長(zhǎng)!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)