Mohon bantuannya, saya bingung melakukan get textboxt sebagai nama tabel database yang ingin digunakan.
| <?php |
| include 'koneksi.php'; |
| $nim = $_GET['nim']; |
| $nama = "mahasiswa"; |
| $query = mysqli_query($koneksi, "select * from $nama where nim='$nim'"); |
| $mahasiswa = mysqli_fetch_array($query); |
| $data = array( |
| 'jurusan' => $mahasiswa['jurusan'], |
| 'alamat' => $mahasiswa['alamat'],); |
| echo json_encode($data); |
| ?> |
ini form ajaxnya
| <!DOCTYPE html> <!--form-ajax.php--> |
| <html> |
| <head> |
| <title>Ajax Jquery - Belajarphp.net</title> |
| </head> |
| <body> |
| <form action=""> |
| <table> |
| <tr><td>NIM</td><td><input type="text" onkeyup="isi_otomatis()" id="nim"></td></tr> |
| <tr><td>NAMA TABEL</td><td><input type="text" id="namatabel"></td></tr> |
| <tr><td>JURUSAN</td><td><input type="text" id="jurusan"></td></tr> |
| <tr><td>ALAMAT</td><td><input type="text" id="alamat"></td></tr> |
| </table> |
| </form> |
| <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> |
| <script type="text/javascript"> |
| function isi_otomatis(){ |
| var nimi = $("#nim").val(); |
| $.ajax({ |
| url: 'proses-ajax.php', |
| data:"nim="+nimi, |
| }).success(function (data) { |
| var json = data, |
| obj = JSON.parse(json); |
| $('#jurusan').val(obj.jurusan); |
| $('#alamat').val(obj.alamat); |
| }); |
| } |
| </script> |
| </body> |
| </html> |
Terima kasih sebelumnnya