I want to select data from my database using PHP and ajax call function.
In the select function, I use $_POST from a textbox
. I have made a PHP and ajax call function on my page but something is wrong. I use isset[]
for checking the $_POST
data that I get from my textbox
. I use an if
condition in there - when the $_POST
is empty it selects all data without a where clause, and when a specific $_POST
has a value, it will select data with a where clause.
The problem here is that when I use data in my textbox
for rendering a string to my PHP $_POST
, the else condition on my PHP doesn't run.
This is my code that I use:
JavaScript/jQuery:
$(document).ready(function(e) {
var data = $("#report_all").serialize();
$('#all_report thead').empty();
$('#all_report tbody').empty();
$.ajax({
data: data,
type: "Post",
url: "../php/report/report_all_wjm.php",
success: function(data){
var list = JSON.parse(data);
var th = "";
th += "<th>"+"<center>"+'No'+"</center>"+"</th>";
th += "<th>"+"<center>"+'Storage Location'+"</center>"+"</th>";
th += "<th>"+"<center>"+'Kode Material'+"</center>"+"</th>";
th += "<th>"+"<center>"+'Nama Material'+"</center>"+"</th>";
th += "<th>"+"<center>"+'No.Polisi'+"</center>"+"</th>";
th += "<th>"+"<center>"+'Id Identifier'+"</center>"+"</th>";
th += "<th>"+"<center>"+'Date'+"</center>"+"</th>";
th += "<th>"+"<center>"+'Netto'+"</center>"+"</th>";
th += "<th>"+"<center>"+'Uses'+"</center>"+"</th>";
th += "<th>"+"<center>"+'Unit'+"</center>"+"</th>";
th += "<th>"+"<center>"+'Payroll'+"</center>"+"</th>";
th += "</th>";
$("#all_report thead").append(th);
for(var i = 0; i < list.length; i++){
var tr = "<tr>";
tr += "<td>" +(i+1)+"</td>";
tr += "<td>" +list[i]['sloc']+"</td>";
tr += "<td>" +list[i]['kode']+"</td>";
tr += "<td>" +list[i]['nama']+"</td>";
tr += "<td>" +list[i]['no_pol']+"</td>";
tr += "<td>" +list[i]['id']+"</td>";
tr += "<td>" +list[i]['date']+"</td>";
tr += "<td>" +list[i]['netto']+"</td>";
tr += "<td>" +list[i]['uses']+"</td>";
tr += "<td>" +list[i]['unit']+"</td>";
tr += "<td>" +list[i]['payroll']+"</td>";
tr += "</tr>";
$("#all_report tbody").append(tr);
$("#all_report").show();
}
return false;
}
});
});
PHP:
<?php
include("../../Connections/koneksi.php");
$date_awal=$_POST['date_start'];
$date_akhir=$_POST['date_end'];
$kode=$_POST['kode_mat'];
$kode1=$_POST['kode_mat1'];
$sloc=$_POST['s_loc'];
$sloc1=$_POST['s_loc1'];
$type=$_POST['get_type'];
//Display all data
if (isset($date_awal) == "" || isset($date_akhir) == "" || isset($sloc)== "" || isset($sloc1)== "" || isset($kode)== "" || isset($kode1)== "" || isset($type)== "" ){
$sql = "SELECT * FROM wjm ORDER by no asc";
$query = mysqli_query($db,$sql);
$rows = array();
while($tmp= mysqli_fetch_assoc($query)) {
$rows[] = $tmp;
}
}
//Display all data by one date
else {
// Data for Titik1
$sql = "SELECT * FROM wjm WHERE date='$date_awal' order by kode asc ";
$query = mysqli_query($db,$sql);
$rows = array();
while($tmp= mysqli_fetch_assoc($query)) {
$rows[] = $tmp;
}
echo json_encode($rows);
}
mysqli_close($db);
?>