Postingan lainnya
Bagaimana menampilkan notifikasi/pesan ketika input data sukses atau gagal, dengan oop php pdo ajax
Bagaimana menampilkan notifikasi/pesan ketika input data sukses atau gagal, dengan oop php pdo ajax.? file crud.php https://pastebin.com/raw/tx7Dm8xH
file formadd.php https://pastebin.com/raw/4LPCJcHi
Tanggapan
kodingnya lngsung taruh didalem tag kode aja gan, biar temen2 bisa langsung liat kodingnya, (diedit pertanyaannya)
8 Jawaban:
file crud.php <pre> include_once "dbConfig.php"; class crud extends dbConfig{
public function insert_pegawai(){
$nip = htmlspecialchars($_POST['nip']);
$nama = htmlspecialchars($_POST['nmPeg']);
$gol = htmlspecialchars($_POST['golPEg']);
$gender = htmlspecialchars($_POST['jkPeg']);
$cekNip = $this-&gt;db-&gt;prepare("SELECT * FROM ssh_pegawai WHERE peg_nip = ?");
$cekNip-&gt;execute(array($nip));
$pesan = "";
$sql = "INSERT INTO ssh_pegawai SET peg_nip = ?, peg_nama = ?, peg_gol = ?, peg_gender = ?";
$stmt = $this-&gt;db-&gt;prepare($sql);
$stmt-&gt;bindParam(1, $nip);
$stmt-&gt;bindParam(2, $nama);
$stmt-&gt;bindParam(3, $gol);
$stmt-&gt;bindParam(4, $gender);
$stmt-&gt;execute();
/*ambil AUTO INCREMENT terakhir dari tabel ssh_pegawai*/
$query = "SELECT LAST_INSERT_ID()";
$stmt1 = $this-&gt;db-&gt;prepare($query);
$stmt1-&gt;execute();
while($r = $stmt1-&gt;fetch(PDO::FETCH_LAZY)){
$ID = $r[0];
}
/*simpan ke tabel ssh_login*/
$statusLog = htmlspecialchars($_POST['status']);
$pass_u = htmlspecialchars($_POST['logPass']);
$query_login = "INSERT INTO ssh_login SET login_peg = ?, login_user = ?, login_status = ?, login_pass = ?";
$stmt2 = $this-&gt;db-&gt;prepare($query_login);
$stmt2-&gt;bindParam(1, $ID);
$stmt2-&gt;bindParam(2, $nip);
$stmt2-&gt;bindParam(3, $statusLog);
$stmt2-&gt;bindParam(4, password_hash($pass_u, PASSWORD_DEFAULT));
$stmt2-&gt;execute();
$response = array('pesan'=&gt;"Data Berhasil dihapus");
echo json_encode($response);
$response = array('pesan',$pesan);
echo json_encode($response);
return $pesan;
}
} </pre>
file frmAdd.php (ajax)
<pre>
<?php include_once "_header.php"; include_once "class/crud.php";
$new = new crud();
if(isset($_POST['nmPeg'])){
$new->insert_pegawai();
}elseif(isset($_GET['hapus'])){
$id = $_GET['hapus'];
$new->delete_pegawai($id);
}
?>
//perintah untuk menampilkan pesan/notifikasi <small id="result_info" style="color:red"></small>
<script type="text/javascript">
$(document).on('click','#btnSimpan',function(e){
e.preventDefault();
var nmPeg = $("#nmPeg").val();
var nip = $("#nip").val()
var jkPeg = $("#jkPeg").val();
var status = $("#status").val();
var logPass = $("#logPass").val();
var KonPass = $("#KonPass").val();
if(nmPeg == ""){
$("#lblNm").html("Tidak Boleh Kosong");
$("#lblNm").fadeIn('fast').show().delay(3000).fadeOut('fast');
}else if(nip == ""){
$("#lblNip").html("Tidak Boleh Kosong");
$("#lblNip").fadeIn('fast').show().delay(3000).fadeOut('fast');
}else if(status == ""){
$("#lblStatus").html("Tidak Boleh Kosong");
$("#lblStatus").fadeIn('fast').show().delay(3000).fadeOut('fast');
}else if(jkPeg == ""){
$("#lblJk").html("Tidak Boleh Kosong");
$("#lblJk").fadeIn('fast').show().delay(3000).fadeOut('fast');
}else if(logPass == ""){
$("#lblPass").html("Tidak Boleh Kosong");
$("#lblPass").fadeIn('fast').show().delay(3000).fadeOut('fast');
}else if(logPass.length < 4){
$("#lblPass").html("Password Minimal 7 Karakter");
$("#lblPass").fadeIn('fast').show().delay(3000).fadeOut('fast');
}else if(KonPass == ""){
$("#lblKon").html("Tidak Boleh Kosong");
$("#lblKon").fadeIn('fast').show().delay(3000).fadeOut('fast');
}else if(KonPass !== logPass){
$("#lblKon").html("Konfirmasi Password Salah");
$("#lblKon").fadeIn('fast').show().delay(3000).fadeOut('fast');
}else{
$.ajax({
url: "<?php echo $_SERVER['PHP_SELF'];?>",
type: "POST",
data: $('#frmPeg').serialize(),
dataType : "json",
success:function(responseText){
$("#frmPeg")[0].reset();
$("#result_info").html(responseText.pesan);
},
error: function(jqXHR, textStatus, errorThrown){
alert("Gagal Simpan Data");
}
});
}
});
</script>
</pre>
ini coding saya gan... input datanya sukses tersimpan ke database, tapi ajax negeluarin alert (gagal simpan data) kalau dataTypenya saja hapus simpan datanya alert tadi nggak jalan tp nggak ada notifikasi/pesan simpan data sukses
Klo request ajax nya pake json backend nya ndak usah pake return, echo jsone_encode nya salah satu saja, success:function(data){ console_log(data.pesan) },
<pre>
$.ajax({
url: "pegawai.php",//"<?php //echo $_SERVER['PHP_SELF'];?>",
type: "POST",
data: $("#frmPeg").serialize(),
dataType: "json",
//async: false,
success:function(data){
$("#frmPeg")[0].reset();
$("#result_info").html(data.pesan);
//console.log(data.pesan);
},
error: function(jqXHR, textStatus, errorThrown){
alert("Gagal Simpan Data");
}
});
</pre>
Data sukses simpan memang, tp notifikasi/pesan yang muncul alert("gagal simpan data") return nya udah saya hapus. kalau di CI nggak ada masalah. kenapa ya bang.?
Tanggapan
Solved Bang Makasih
outputnya sperti ini bang
[link] https://plus.google.com/photos/107584932249116932416/album/6673839835876873425/6673839835794330962?authkey=CImB8ajXhfOYPQ&hl=id [/link]
databasenya <a href=' https://plus.google.com/photos/107584932249116932416/album/6673839835876873425/6673839835326376482?authkey=CImB8ajXhfOYPQ&hl=id '> https://plus.google.com/photos/107584932249116932416/album/6673839835876873425/6673839835326376482?authkey=CImB8ajXhfOYPQ&hl=id </a>
saya mengalami masalah yang sama kira2 gmna solusinya ya?
Bikin pesan kaya sekarang saya ketik kemudian tampil d baris komentar pke script gimana gan