Advertisement
fakhrycodepolitan

Array

May 11th, 2024
839
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.76 KB | Source Code | 0 0
  1. <?php
  2. // Mulai session
  3. session_start();
  4.  
  5. // Inisialisasi array dari session jika belum ada
  6. if (!isset($_SESSION['data'])) {
  7.     $_SESSION['data'] = array();
  8. }
  9.  
  10. // Memeriksa apakah ada inputan baru
  11. if(isset($_POST['tambah'])){
  12.     // Mengambil nilai inputan dari form
  13.     $new_item = $_POST['new_item'];
  14.  
  15.     // Menambahkan nilai baru ke dalam array
  16.     $_SESSION['data'][] = $new_item;
  17. }
  18. ?>
  19. <!-- Form HTML untuk inputan baru -->
  20. <form method="post" action="index.php">
  21.     <label for="new_item">Tambahkan item baru:</label>
  22.     <input type="text" id="new_item" name="new_item">
  23.     <button type="submit" name="tambah">Tambah</button>
  24. </form>
  25. <?php
  26. // Menampilkan isi array
  27. echo "Isi Array:";
  28. echo "<pre>";
  29. print_r($_SESSION['data']);
  30. echo "</pre>";
  31. ?>
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement