kunalchauhanok

AjaxCategory

Oct 17th, 2021 (edited)
993
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Dashboard.php
  2. _______________________________________________
  3. <?php
  4. include "../include/config.php";
  5.  
  6. session_start();
  7.  
  8. if (!isset($_SESSION['session_email'])) {
  9.     header('Location: login.php');
  10. }
  11. $user_id = $_SESSION['session_id'];
  12. $sql = "SELECT * FROM users WHERE id='$user_id'";
  13. $result = mysqli_query($conn, $sql);
  14. $profiledata = mysqli_fetch_assoc($result);
  15.  
  16. ?>
  17. <style>
  18.     .error {
  19.         color: red;
  20.     }
  21. </style>
  22. <?php include "header.php" ?>
  23. <div class="container">
  24.     <div class="card">
  25.         <div class="card-header">
  26.             <h5 class="card-title"><a href="#" class="btn btn-primary"><?php echo $profiledata['name']; ?></a></h5>
  27.             <h5 class="card-title" style="float: right;"><a href="logout.php" class="btn btn-primary">Logout</a></h5>
  28.         </div>
  29.     </div>
  30.     <form method="POST" id="myForm">
  31.         <div class="form-group row">
  32.             <div class="row">
  33.                 <div class="offset-lg-12 col-4 mt-5">
  34.                     <label for="ex2">Category List</label>
  35.                     <select class="selectCatlist form-select" name="parent_cat_id" id="responsecontainer">
  36.  
  37.                     </select>
  38.                 </div>
  39.             </div>
  40.             <div class="row">
  41.                 <div class="offset-lg-12 col-4 mt-5">
  42.                     <label for="ex2">Category</label>
  43.                     <input name="cat_name" class="categoryName form-control" id="ex2" type="text" value="" />
  44.                 </div>
  45.             </div>
  46.             <div class="offset-lg-12 col-4 mt-3">
  47.                 <button name="cat_register_btn" class="btn btn-success catsubbtn" type="submit" style="float:right">Submit</button>
  48.             </div>
  49.             <div class="container">
  50.                 <table id="displayTable" class="table">
  51.  
  52.                 </table>
  53.             </div>
  54.     </form>
  55. </div>
  56. <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  57. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
  58. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.3/jquery.validate.min.js"></script>
  59. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.3/additional-methods.min.js"></script>
  60. <script>
  61.     $('#myForm').validate({
  62.         rules: {
  63.             cat_name: {
  64.                 required: true,
  65.             },
  66.         },
  67.         messages: {
  68.             cat_name: {
  69.                 required: "Enter category to continue."
  70.             },
  71.         }
  72.     });
  73. </script>
  74. <script>
  75.     $(document).ready(function() {
  76.         var category_name = "";
  77.         var parent_cat_id = "";
  78.         $(".catsubbtn").on('click', function(e) {
  79.             e.preventDefault();
  80.             category_name = $(".categoryName").val();
  81.             parent_cat_id = $('.selectCatlist').val();
  82.             $.ajax({
  83.                 url: "registerCategory.php",
  84.                 type: "POST",
  85.                 data: {
  86.                     category_name: category_name,
  87.                     parent_cat_id: parent_cat_id,
  88.                 },
  89.                 success: function() {
  90.                     display();
  91.                     $("#myForm")[0].reset();
  92.                 }
  93.             });
  94.         });
  95.  
  96.         $.ajax({
  97.             url: "fetchCategory.php",
  98.             type: "POST",
  99.             success: function(data) {
  100.                 $('#responsecontainer').html(data);
  101.             }
  102.         });
  103.  
  104.         function display() {
  105.             $.ajax({
  106.                 url: "fetchCategory.php",
  107.                 type: "POST",
  108.                 success: function(data) {
  109.                     $('#responsecontainer').html(data);
  110.                 }
  111.             });
  112.         }
  113.  
  114.         $.ajax({
  115.             url: "fetchsubcat.php",
  116.             type: "POST",
  117.             success: function(response) {
  118.                 $('#displayTable').html(response);
  119.             }
  120.         });
  121.     });
  122. </script>
  123.     __________________________________________________________________________________________________________________________________
  124.    
  125.     fetchsubcat.php
  126.    
  127.     <?php
  128. include "../include/config.php";
  129.  
  130. session_start();
  131.  
  132. if (!isset($_SESSION['session_email'])) {
  133.     header('Location: login.php');
  134. }
  135.  
  136. $sql = "SELECT * FROM category";
  137. $result = mysqli_query($conn,$sql);
  138. $cat_array=[];
  139. while ($row = mysqli_fetch_assoc($result)) {
  140.     $par_cat_id = $row['id'];
  141.     $cat_array[$par_cat_id] = ["cat_name"=>$row['cat_name'],"parent_id"=>$row['parent_id']];
  142. }
  143.  
  144. echo "<table>
  145. <tr>
  146. <th>Sr No</th>
  147. <th>Category</th>
  148. <th>Parent Category</th>
  149. <th>Actions</th>
  150. </tr>";
  151. $data = "";
  152. $i=1;
  153. foreach ($cat_array as $key => $value) {
  154.  
  155.     $data.='<tr>';
  156.     $data.='<td>' . $i++ . '</td>';
  157.     $data.='<td>' . $value['cat_name'] . '</td>';
  158.     $data.='<td>';
  159.     $data.= $value['parent_id']!='0' ? $cat_array[$value['parent_id']]['cat_name']:'';
  160.     $data.='</td>';
  161.     $data.='<td><button class="btn btn-success"><a href=editCategories.php?id='.$key. '>Edit</a></button></td>';
  162.     $data.='<td><button class="btn btn-warning"><a href=deleteCategories.php?id='.$key. '>Delete</a></button></td>';
  163.     echo "</tr>";
  164. }
  165. echo "</table>";
  166. echo json_encode($data);
  167. exit;
  168. ?>
  169.     _______________________________________________________________________________________________________________________________
  170.     fetchCategory.php
  171.    
  172.     <?php
  173. include "../include/config.php";
  174.  
  175. session_start();
  176.  
  177. if (!isset($_SESSION['session_email'])) {
  178.     header('Location: login.php');
  179. }
  180.  
  181.  
  182. $sql = "SELECT * FROM category";
  183. $results = mysqli_query($conn,$sql);
  184. $html="";
  185. $html ='<option>select category</option>';
  186. while ($row = mysqli_fetch_assoc($results)) {
  187. //    $html.='<option value='.$row["id"].'>'.$row["cat_name"].'</option>';
  188.     $html .= "<option value=" . $row['id'] . ">" . $row['cat_name'] . "</option>";
  189. }
  190. echo json_encode($html);
  191. exit;
  192. ?>
RAW Paste Data