0

I have trouble when insert data into mysql. I'am using 3 file to do it home.php, ajax.js and proses_simpan.php

home.php

<form id="form">                                                
    <div class="form-group">
        <input type="text" class="form-control" id="nama" name="nama" placeholder="Nama">
    </div>
    <div class="form-group">                                
        <input type="text" class="form-control" id="alas_hak" name="alas_hak" placeholder="Alas Hak">
    </div>
</form>
<button type="button" class="btn btn-primary" id="btn-simpan">Simpan</button>

ajax.js

$("#btn-simpan").click(function(){ 
    var data = new FormData();      
    data.append('nama', $("#nama").val());
    data.append('alas_hak', $("#alas_hak").val()); 

    $.ajax({
        url: 'proses_simpan.php', 
        type: 'POST', 
        data: data, 
        processData: false,
        contentType: false, 
        dataType: "json",

        beforeSend: function(e) {
            if(e && e.overrideMimeType) {
                e.overrideMimeType("application/json;charset=UTF-8");
            }
        }
    });
});

proses_simpan.php

<?php   
    include "koneksi.php";

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

    $sql = $pdo->prepare("INSERT INTO tbl_pengadaantanah(nama,alas_hak) 
            VALUES(:nama,:alas_hak)");

    $sql->bindParam(':nama', $nama);
    $sql->bindParam(':alas_hak', $alas_hak);
    $sql->execute();
?>

when insert data, only nama is stored. but alas hak isn't stored and null value in database

please help me. what should I do? thanks

  • May be issue during binding if values is passed successfully in post request. Use try catch block may be it will be helpful for you – Ravi Roshan Sep 02 '17 at 10:31
  • Why are you forcing the sent data to JSON instead of an URL encoded string? I think it should work by commenting out (or removing) `processData: false`, `contentType:false` and the whole `beforeSend`. The rest looks good. – Louys Patrice Bessette Sep 02 '17 at 10:40
  • And `dataType:"json"` is to tell Ajax what data format to expect as a response. It is useless if you do not expect one. – Louys Patrice Bessette Sep 02 '17 at 10:42
  • I have removed processData: false, contentType:false and beforeSend. and still null value in database for alas hak field – Azy Kumbara Sep 02 '17 at 23:54

1 Answers1

0

Change proses_simpan.php to see if you got an error in your sql.

<?php   
    include "koneksi.php";

    try {
        $nama = $_POST['nama']; 
        $alas_hak = $_POST['alas_hak'];

        $sql = $pdo->prepare("INSERT INTO tbl_pengadaantanah(nama,alas_hak) 
                VALUES(:nama,:alas_hak)");

        $sql->bindParam(':nama', $nama);
        $sql->bindParam(':alas_hak', $alas_hak);
        $sql->execute();
    }
    catch(PDOException $e){
        echo $e->getMessage();
    }
?>

It's always helpful to have a try/catch while developing. The rest of your files looks good.

Gerrit Fries
  • 145
  • 1
  • 13