Browse Source

write stock in data folder using post requests

keplyx 4 years ago
parent
commit
c4e6bd641b
3 changed files with 51 additions and 3 deletions
  1. 12
    3
      admin/index.php
  2. 18
    0
      admin/write_json.php
  3. 21
    0
      assets/js/stock.js

+ 12
- 3
admin/index.php View File

@@ -42,10 +42,10 @@ ob_start();
42 42
                 <input type="text" class="form-control" id="imageInput" placeholder="Lien Image">
43 43
             </td>
44 44
             <td>
45
-                <button type="submit" class="btn btn-success" onclick="addNewItem()"><i class="fas fa-check"></i></button>
45
+                <button type="submit" class="btn btn-success" onclick="addNewItem()"><i class="fas fa-check"></i>
46
+                </button>
46 47
             </td>
47 48
         </tr>
48
-
49 49
         </tbody>
50 50
     </table>
51 51
 
@@ -55,7 +55,16 @@ ob_start();
55 55
         <?= $tableHeader ?>
56 56
         </tbody>
57 57
     </table>
58
-</div>
58
+
59
+    <div style="display: flex; margin-top: 100px">
60
+        <button class="btn btn-success btn-lg" style="margin: auto"
61
+                onclick="saveDataset()">
62
+            Sauvegarder le stock
63
+        </button>
64
+    </div>
65
+
66
+
67
+</>
59 68
 
60 69
 <link type="text/css" rel="stylesheet" href="<?= $relativePath ?>assets/css/stock.css" media="screen,projection"/>
61 70
 

+ 18
- 0
admin/write_json.php View File

@@ -0,0 +1,18 @@
1
+<?php
2
+$rest_json = file_get_contents("php://input");
3
+$_POST = json_decode($rest_json, true);
4
+
5
+
6
+//var_dump($_POST);
7
+
8
+$fp = fopen('../data/stock.json', 'w');
9
+$result = fwrite($fp, json_encode($_POST));
10
+fclose($fp);
11
+
12
+if ($result) {
13
+    echo 'Réussite !';
14
+} else {
15
+    echo 'Echec!
16
+    '; // Allows to create a newline
17
+    var_dump($_POST);
18
+}

+ 21
- 0
assets/js/stock.js View File

@@ -174,3 +174,24 @@ function isItemInputFilled() {
174 174
         $.selector_cache('#imageInput').val() !== '' &&
175 175
         getTypesChecked().length > 0;
176 176
 }
177
+
178
+
179
+
180
+function saveDataset() {
181
+    let finalDataset = {
182
+        types: defaultTypes,
183
+        articles: currentDataset
184
+    };
185
+    console.log(finalDataset);
186
+    $.ajax({
187
+        type: "POST",
188
+        url: "write_json.php",
189
+        data: JSON.stringify(finalDataset),
190
+        dataType: "json",
191
+        contentType: "application/json; charset=utf-8",
192
+        complete: function (data) {
193
+            alert(data.responseText);
194
+            console.log(data);
195
+        },
196
+    });
197
+}

Loading…
Cancel
Save