Hello i am bit struggling to find the solution or there is other effective way to do it, so here is the case, i have file named display.php in display folder, then i have add_data.php in kas folder, is there way when i input/edit data in add_data.php then display.php reload itself ? i tried to learn ajax but looks like i find dead end to resolve it.
in Display.php i put this ajax
$.ajax({
type: "GET",
url: "../kas/add_data.php",
dataType: "json",
data: "simpan",
}).done(function(data) {
location.reload();
}).fail(function(msg) {
console.log(msg);
});
this is the add_data.php
if (isset ($_POST['Simpan'])){
$masuk=$_POST['masuk'];
$masuk_hasil=preg_replace("/[^0-9]/", "", $masuk);
//mulai proses simpan data
$sql_simpan = "INSERT INTO kas_masjid (tgl_km,uraian_km,masuk,keluar,jenis) VALUES (
'".$_POST['tgl_km']."',
'".$_POST['uraian_km']."',
'".$masuk_hasil."',
'0',
'Masuk')";
$query_simpan = mysqli_query($koneksi, $sql_simpan);
mysqli_close($koneksi);
if ($query_simpan) {
echo "<script>
Swal.fire({title: 'Tambah Data Berhasil',text: '',icon: 'success',confirmButtonText: 'OK'
}).then((result) => {if (result.value){
window.location = 'index.php?page=i_data_km';
}
})</script>";
}else{
echo "<script>
Swal.fire({title: 'Tambah Data Gagal',text: '',icon: 'error',confirmButtonText: 'OK'
}).then((result) => {if (result.value){
window.location = 'index.php?page=i_add_km';
}
})</script>";
}}
?>
so i when i add input to add_data.php, display.php doing nothing, but when i see from console display.php, echo all add_data.php script.
data: "simpan"is not the correct way to send parameters in AJAX. It should be something likedata: {simpan: 1, masuk: value1, tgl_km: value2, uraian_km: value3}type: "GET"you need to use$_GETin PHP to read the parameters, not$_POST.location.reload()will, without fail, reload the page. I find it likely that it's not running at all, i.e. the request is failing. The PHP is probably raising some 4XX or 5XX, you should use the Network tab in your browser's debugger to find out for sure.simpan!=Simpan. Open your browser's devtools, check the console and network tabs, you will errors and feedback about the requests and responses you are making, those will help you move forward.