justhrun

testajax.pehape

Jan 31st, 2016
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.93 KB | None | 0 0
  1. <?php
  2. if(count($_POST)) {
  3.     $nama = $_POST['nama'];
  4.     $alamat = $_POST['alamat'];
  5.     $usia = $_POST['usia'];
  6.  
  7.     $mysqlId= mysqli_connect('localhost','usernamenya','passwordnya','namadebenya');
  8.     $query = "INSERT INTO daftar_siswa SET nama='".$nama."', alamat='".$alamat."', usia='".$usia."'";
  9.     $result = mysqli_query($mysqlId, $query);
  10.     if(!$result) {
  11.         echo "Error: ".mysqli_error($mysqlId);
  12.         exit;
  13.     }
  14.     $affected = mysqli_affected_rows($mysqlId);
  15.     if($affected) {
  16.         echo "OK, Data sudah disimpan";
  17.     } else {
  18.         echo "Gagal menyimpan Data";
  19.     }
  20.     exit;
  21. }
  22. ?>
  23. <!DOCTYPE HTML>
  24. <html>
  25. <head>
  26. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  27. <title>Tes 1dua3 Percobaan</title>
  28. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  29. <script>
  30. $(document).ready( function() {
  31.     $('#myform').on('submit', function(e) {
  32.         e.preventDefault();
  33.         var dataStr = $('#myform').serialize();
  34.         $.ajax({
  35.             type: 'POST',
  36.             cache: false,
  37.             url: window.location.pathname,
  38.             data: dataStr,
  39.             success: function(reply) {
  40.                 alert(reply);
  41.                 $('#myform')[0].reset();
  42.             }
  43.         });
  44.     });
  45. });
  46. </script>
  47. </head>
  48. <body>
  49. <center>
  50. <br><br><br>
  51. <form id=myform method=post>
  52. Nama: <input type='text' name='nama' id='nama' placeholder='Masukkan Nama Juragan' size='20' maxlength='40' required/><br><br>
  53. Alamak: <input type='text' name='alamat' id='alamat' placeholder='Alamat Juragan?' size='30' maxlength='123' required/><br><br>
  54. Usia: <select name='usia' id='usia' style="color:gray;" required/>
  55.     <option style="display:none;" value="" disabled selected>Silakan Pilih</option>
  56.     <option value='16'>16 Tahun</option>
  57.     <option value='17'>17 Tahun</option>
  58.     <option value='18'>18 Tahun</option>
  59.     <option value='19'>19 Tahun</option>
  60.     <option value='20'>20 Tahun</option>
  61. </select><br><br>
  62. <button>Save Data</buton>
  63. </form>
  64. </center>
  65. </body>
  66. </html>
Advertisement
Add Comment
Please, Sign In to add comment