1493
0
Mungkin ini bisa jadi referensi gan

1. Struktur databasenya nama database "dbsaya"
  1. -- phpMyAdmin SQL Dump
  2. -- version 4.5.1
  3. -- http://www.phpmyadmin.net
  4. --
  5. -- Host: 127.0.0.1
  6. -- Generation Time: Jan 11, 2017 at 03:58 AM
  7. -- Server version: 10.1.19-MariaDB
  8. -- PHP Version: 5.5.38
  9.  
  10. SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
  11. SET time_zone = "+00:00";
  12.  
  13. CREATE TABLE `tabel1` (
  14. `id_tbl` varchar(5) NOT NULL,
  15. `na_tbl` varchar(10) NOT NULL,
  16. `al_tbl` varchar(10) NOT NULL,
  17. `no_tbl` varchar(10) NOT NULL
  18. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  19.  
  20.  
  21. INSERT INTO `tabel1` (`id_tbl`, `na_tbl`, `al_tbl`, `no_tbl`) VALUES
  22. ('001', 'asri', 'bjb', '0811');
  23.  
  24.  
  25. CREATE TABLE `tabel2` (
  26. `id_tbl` varchar(5) NOT NULL,
  27. `na_tbl` varchar(10) NOT NULL,
  28. `al_tbl` varchar(10) NOT NULL,
  29. `no_tbl` varchar(10) NOT NULL
  30. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  31.  
  32.  
  33. INSERT INTO `tabel2` (`id_tbl`, `na_tbl`, `al_tbl`, `no_tbl`) VALUES
  34. ('001', 'rian', 'bjm', '0812');
  35.  
  36. ALTER TABLE `tabel1`
  37. ADD PRIMARY KEY (`id_tbl`);
  38.  
  39. --
  40. -- Indexes for table `tabel2`
  41. --
  42. ALTER TABLE `tabel2`
  43. ADD PRIMARY KEY (`id_tbl`);
  44.  
  45.  

Buat file dengan nama proses-ajax.php
  1. <?php
  2. include 'koneksi.php';
  3. $idterima = $_GET['idkirim'];
  4. $tabelterima = $_GET['tabelkirim'];
  5. $query = mysqli_query($koneksi, "select * from $tabelterima where id_tbl='$idterima'");
  6. $ambil = mysqli_fetch_array($query);
  7. $data = array(
  8. 'data1' => $ambil['na_tbl'],//na_tbl adalah nama field dari tabel di database
  9. 'data2' => $ambil['al_tbl'], //al_tbl adalah nama field dari tabel di database
  10. 'data3' => $ambil['no_tbl'],); //no_tbl adalah nama field dari tabel di database
  11. echo json_encode($data);
  12. ?>

File koneksi.php
  1. <?php
  2. // konfigurasi database
  3. $host = "localhost";
  4. $user = "root";
  5. $password = "";
  6. $database = "dbsaya";
  7. // perintah php untuk akses ke database
  8. $koneksi = mysqli_connect($host, $user, $password, $database);
  9. ?>

Untuk menampilkan nya yaitu file form-ajax.php
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Ajax Jquery - Belajarphp.net</title>
  5. </head>
  6. <body>
  7. <form action="">
  8. <table>
  9. <tr><td>ID</td><td><input type="text" id="id"></td></tr>
  10. <tr><td>TABEL</td><td><select type="text" onchange="isi_otomatis()" id="tabel">
  11. <option value="">Pilih tabel</option>
  12. <option value="tabel1">tabel1</option>
  13. <option value="tabel2">tabel2</option>
  14. </select></td></tr>
  15. <tr><td>NAMA</td><td><input type="text" id="nama"></td></tr>
  16. <tr><td>ALAMAT</td><td><input type="text" id="alamat"></td></tr>
  17. <tr><td>NOHP</td><td><input type="text" id="nohp"></td></tr>
  18. </table>
  19. </form>
  20. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  21. <script type="text/javascript">
  22. function isi_otomatis(){
  23. var idambil = $("#id").val();
  24. var tabelambil = $("#tabel").val();
  25. $.ajax({
  26. url: 'proses-ajax.php',
  27. data:"idkirim="+idambil+"&tabelkirim="+tabelambil,
  28. }).success(function (data) {
  29. var json = data,
  30. obj = JSON.parse(json);
  31. $('#nama').val(obj.data1);
  32. $('#alamat').val(obj.data2);
  33. $('#nohp').val(obj.data3);
  34. });
  35. }
  36. </script>
  37. </body>
  38. </html>

Saat di jalankan form-ajax.php masukkan dulu id baru pilih tabel
  • Jan 11, 2017
  • 0 suka, 0 tidak suka
4959
Please login to answer!
/sangkep/answer/add-json/nogzip/1
/sangkep/answer/send-notification
User Paling Aktif Bulan Lalu

Asrianor

Point Bulan Lalu: 6