สอนทำเว็บไซต์

Management System : AddData

โมดูลเพิ่มข้อมูล การรันเลขที่เอกสาร การแจ้งปัญหาระบบ

ManageProduct.php

<?php include_once("checklogin.php"); ?> <!DOCTYPE html> <html lang="en"> <head> <title>Lekjew.com</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" type="text/css" href="css/jquery.dataTables.css"> <link rel="stylesheet" type="text/css" href="css/dataTables.bootstrap4.css"> <link rel="stylesheet" type="text/css" href="css/responsive.dataTables.css"> <?php include_once("include/style.php"); ?> </head> <body class="adminbody"> <?php include_once("include/header_login.php"); ?> <div id="main" > <?php include_once("leftmenu.php"); ?> <div class="content-page"> <div class="content"> <div class="col-md-12"> <div class="card pd-10"> Manage Product <br><br> <div class="col-md-12"><div class="row"> <div class="col-md-11"></div> <div class="col-md-1"><button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#ModalA" >Add</button></div> </div></div> <div class="table-responsive"> <table id="tableA" class="table table-bordered nowrap" border="1" cellspacing="1" width="100%"> <thead> <tr> <th>No</th> <th>ProductGroup</th> <th>ProductName</th> </tr> </thead> <tbody id="showtableA"> <tr> <td></td> <td></td> <td></td> </tr> </tbody> <tfoot> <tr> <th></th> <th></th> <th></th> </tr> </tfoot> </table> </div> </div> </div> </div> </div> </body> </html> <div id="ModalA" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title">Add Product</h5> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" id="closeModalA"></button> </div> <div class="modal-body"> <div class="input-group mb-3"> <span class="input-group-text" id="">Product Group</span> <select id="pgroup" name="pgroup" class="form-select" aria-label="Default select example"> </select> </div> <div class="input-group mb-3"> <span class="input-group-text" id="">Product Name</span> <input type="text" id="product_name" class="form-control" placeholder="Product Name" aria-label="Product Name" aria-describedby="basic-addon1"> </div> <div class='text-danger' id='showerr'></div> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> <button type="button" class="btn btn-primary" onclick="ProductSave();">Save changes</button> </div> </div> </div> </div> <?php include_once("include/js.php"); ?> <script src="js/jquery.dataTables.js"></script> <script src="js/dataTables.bootstrap4.min.js"></script> <script> CallDataProductGroup(); RefreshTable(); CallData(); function CallData(){ $.ajax({ url: "ManageProduct_data.php", async: false, cache: false, contentType: false, processData: false, type: 'post', success: function(data) { var tr =""; var JsonData = JSON.parse(data); var resL = JsonData.length; for (var i = 0; i < resL; i++) { tr=tr+"<tr>" ; tr=tr+"<td>"; tr = tr + JsonData[i]["product_code"]; tr=tr+"</td>"; tr=tr+"<td>"; tr = tr + JsonData[i]["group_name"]; tr=tr+"</td>"; tr=tr+"<td>"; tr = tr + JsonData[i]["product_name"]; tr=tr+"</td>"; tr=tr+"</tr>" ; } $('#showtableA').html(''); $('#tableA').DataTable().destroy(); $('#showtableA').html(tr); RefreshTable(); }, error: function(data){ $('#showtableA').html(data); } }); } function RefreshTable(){ var table = $('#tableA').DataTable( { dom: "<'row'<'col-md-1'<'pull-left'>><'col-md-3'<'pull-left'>><'col-md-8'f><'col-sm-12'tr>>p", searching: false, ordering: false, lengthMenu: [10], scrollY: "550px", scrollCollapse: true, scrollX: false, responsive: true, "pagingType": "full_numbers", "fnInitComplete": function (oSettings) { $('.select_broker_wrapper').find('.tblBroker_head').hide(); } }); $('.dataTables_scrollBody #tableA tr:eq(0) ').empty(); } function CallDataProductGroup(){ $.ajax({ url: "ManageProductGroup_data.php", async: false, cache: false, contentType: false, processData: false, type: 'post', success: function(data) { var option =""; var JsonData = JSON.parse(data); var resL = JsonData.length; for (var i = 0; i < resL; i++) { option=option+"<option value='"+JsonData[i]["group_code"]+"'>"+JsonData[i]["group_name"]+"</option>" ; } $('#pgroup').html("<option value=''>-- Select --</option>"+option); }, error: function(data){ $('#pgroup').html("<option value=''>-- Select --</option>"); } }); } function ProductSave(){ var pgroup = $('#pgroup').val(); var product_name = $('#product_name').val(); var form_data = new FormData(); form_data.append("condition", "AddProduct"); form_data.append("group_code", pgroup); form_data.append("product_name", product_name); form_data.append("CREATE_BY", "<?php echo $username; ?>"); $.ajax({ url: "ManageProduct_save.php", contentType: false, processData: false, data: form_data, dataType: "text", type: 'post', success: function(data) { var j = 0; var err = ""; var JsonData = JSON.parse(data); var resL = JsonData['result'].length; for(var i =0; i< resL; i++){ if(typeof JsonData['result'][i]['ERR']!=="undefined" && JsonData['result'][i]['ERR']!==null ){ err = err + JsonData['result'][i]['ERR']; j++; } } if(j>0){ $('#showerr').html(err); }else{ $('#closeModalA').click(); CallData(); } } }); } </script>

ManageProduct_save.php

<?php header('Content-Type: application/json'); date_default_timezone_set("Asia/Bangkok"); include('connectDBMS.php'); $UPDATE_AT = date("Y-m-d H:i:s"); $result = array(); $ACTION = $_POST['condition']; $CREATE_BY = $_POST['CREATE_BY']; $CREATE_BY = $_SESSION['username']; if ($ConnDB) { try { switch($ACTION){ case "AddProduct": $product_name = $_POST['product_name']; $group_code = $_POST['group_code']; $sqlrundoc = "SELECT top 1 (CASE WHEN substring(product_code,2,6) is null or substring(product_code,2,6)='' THEN 1 ELSE substring(product_code,2,6) +1 END) as RUNNING FROM product ORDER BY product_code desc "; $rsr = sqlsrv_query($ConnDB, $sqlrundoc); $rr = sqlsrv_fetch_object($rsr); $running = $rr->RUNNING; $product_code = "W". sprintf("%06d", $running); $INSPRODUCT = "INSERT INTO product (product_code,group_code, product_name ) values ( '$product_code','$group_code','$product_name' )"; $RSINS = sqlsrv_prepare($ConnDB, $INSPRODUCT); if (!$RSINS) { throw new Exception($INSPRODUCT); $result[] = array("ERR" => $INSPRODUCT, "CSS" => "text-danger"); } if (!sqlsrv_execute($RSINS)) { throw new Exception( $INSPRODUCT); $result[] = array("ERR" => $INSPRODUCT, "CSS" => "text-danger"); } break; } } catch (Exception $ex) { $result[] = array("ERR" => $ex->getMessage(), "CSS" => "text-danger"); sqlsrv_rollback($ConnDB); sqlsrv_close($ConnDB); } sqlsrv_close($ConnDB); }else{ $result[] = array("ERR" =>"ERROR CONNECTION SQL", "CSS" => "text-danger"); } $json = array( 'result' => $result); echo json_encode($json, JSON_UNESCAPED_UNICODE); ?>

ManageProduct_data.php

<?php include('connectDBMS.php'); $data = array(); $sql = "SELECT p.product_code , p.product_name , sc.group_name FROM product as p LEFT JOIN product_group as sc on sc.group_code = p.group_code ORDER BY p.product_code desc "; $rsc = sqlsrv_query($ConnDB, $sql); while($rowp = sqlsrv_fetch_object($rsc)) { $data[] = array( "product_code" => $rowp->product_code , "product_name" => $rowp->product_name , "group_name" => $rowp->group_name ); } echo json_encode($data, JSON_UNESCAPED_UNICODE); sqlsrv_close($ConnDB); ?>

ManageProductGroup_data.php

<?php include('connectDBMS.php'); $data = array(); $sql = "SELECT group_code, group_name FROM product_group "; $rsc = sqlsrv_query($ConnDB, $sql); while($rowp = sqlsrv_fetch_object($rsc)) { $data[] = array( "group_code" => $rowp->group_code , "group_name" => $rowp->group_name ); } echo json_encode($data, JSON_UNESCAPED_UNICODE); sqlsrv_close($ConnDB); ?>

leftmenu.php

<div id="navbar-toggler-success" class="align-self-center collapse flex-fill d-lg-flex justify-content-lg-between" style="z-index:1;display:block;" > <?php $page = explode("/", $_SERVER['REQUEST_URI']); echo $page[2]; ?> <div id="menuleft" class=" left main-sidebar"> <div class="sidebar-inner leftscroll"> <div id="sidebar-menu" style=""> <a class="" href="ManageProduct.php"><li class="submenu <?php if($page[2]=="ManageProduct.php"){ echo " bg-light active"; } ?>"><span> Product </span> </li></a> <a class="<?php if($page[2]=="logout.php"){ echo " active"; } ?>" href="logout.php"><li class="submenu"><span> Logout </span> </li></a> </div> </div> <div class="clearfix"></div> </div> </div>

connectDBMS.php

<?php define("SERVERNAME","xxx.xxx.xxx.xxx"); define("DATABASENAME","DATABASENAME"); define("UID","USERNAME"); define("PWD", "PASSWORD"); define("CHARSET","UTF-8"); $connect1 = array("Database"=>DATABASENAME,"UID"=>UID,"PWD"=>PWD,"MultipleActiveResultSets"=>true,"CharacterSet"=> CHARSET); $ConnDB = sqlsrv_connect(SERVERNAME, $connect1); if(!$ConnDB){ echo "Fail Connection"; } ?>