Saya belajar insert data dengan Ajax.. kenapa tidak ter insert ya ?

respon posnya ada tp tdk ada respon sukses..

 <?php

include "koneksi.php";
?>
<html>
<head>
 <title> Tambah </title>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js" type="text/javascript" charset="utf-8" async defer>
  </script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">

</head>
<body style = 'margin : 20px; font : 16px arial;'>
 <center>
<div class="col-md-3">
</div>
<div class="col-md-6">
 <div class="container">

<div class="alert alert-success" style="display: none;"></div>

 <form action="" method="POST" id="#myForm" class="form-horizontal">

  <div class="form-group">
    <label for="exampleInputEmail1">Nama</label>
    <input type="nama" class="form-control" name="nama" id="nama" aria-describedby="emailHelp" value="" placeholder="Masukkan Nama">
    <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
  </div>
  <div class="form-group">
    <label for="exampleInputEmail1">Email address</label>
    <input type="text" class="form-control" name="email" id="email" aria-describedby="emailHelp" value="" placeholder="Enter email">
    <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
  </div>

 <input type = "submit"  id="btnSave" value = 'Submit'/>
</form>

</div>
</div>
<div class="col-md-3">
</div>
</body>
</html>

<script type="text/javascript">

$('#btnSave').click(function(){
  var data = $('#myForm').serialize();

  var nama = $('input[name=nama]');
  var email = $('input[name=email]');
  var result = '';

  //jika kosong maka error dg merubah Bapaknya ditambahi class has-error
  if(nama.val()==''){
    nama.parent().addClass('has-error');
    $("#nama").append("<b>nama harus diisi</b>");
  }else{
    nama.parent().removeClass('has-error');
    result +='1';
  }
  if(email.val()==''){
    email.parent().addClass('has-error');
     $("#email").append("<b>email harus diisi</b>");
  }else{
    email.parent().removeClass('has-error');
    result +='2';
  }

  if(result=='12'){
          $.ajax({
             //type: 'ajax',
             method: "POST",
             url: "aksi_tambah.php",
             data: data,
          // dataType: 'html',
             success: function(data){


                  $('.alert-success').html('<b>Data berhasil di tambah</b>').fadeIn().delay(4000).fadeOut('slow');

                //reloadTable();
                },
                error:function() {
            console.log(data);
            }

             });
          }

  });

</script>

.........................................................

 <?php
//error_reporting(0);
include "koneksi.php";

$nama = $_POST['nama'];
$email = $_POST['email'];

$save="INSERT INTO data (`nama`, `email`) VALUES ('$nama', '$email')";
      $res=mysql_query ($save) or die (mysql_error());


?>
avatar Irfanmuhluster
@Irfanmuhluster

69 Kontribusi 8 Poin

Diperbarui 7 tahun yang lalu

2 Jawaban:

ooohh udah done.. ternyata inputnya harus diletakkan di luar form.. thanks

avatar Irfanmuhluster
@Irfanmuhluster

69 Kontribusi 8 Poin

Dipost 7 tahun yang lalu

mantap semoga lebih semangat lagi!

http://wapdam.tech

avatar Almafazi
@Almafazi

10 Kontribusi 1 Poin

Dipost 7 tahun yang lalu

Login untuk ikut Jawaban