New here and new to coding (kinda). I'm learing by making practical db. Part of code are from tutorials but i understand why and how it works mostly and if not world is full with answers, and most of them here :)
With this particular problem, i believe i just miss something and need fresh look because i'm stuck for 3 days and don't see why it's not working by myself.
Problem: Fetch.php was fetching data from sql and give it to main.php
by echo $output
(html code). Then I decided I want to make my own paging system and need to get rowcount from fetch.php
too. So and json_encode
to fuction and dataType: json
to caller. And it shoud work but not retrieve any data. It works just fine with other mine function.
So what do i don't see?
P.S. I put alerts before ajax and after and inside success function. First two are triggering. So I assume something wrong with fetch and it don't want to return data.
P.P.S. function fetch_pasutijumi doesn't get anything from echo json_encode($out);
... without dataType:'json',
in ajax request and with echo ($outputs)
(without using array) it works and html data is given back.
function fetch_pasutijumi(query, lim, from)
{
$.ajax({
url:"fetch_pasutijumi.php",
method:"POST",
data:{query:query, lim:lim, from: from},
dataType:'json',
success:function(data)
{
result = data.outputs;
$("#result").html(result);
}
});
}
if(isset($_POST["query"]))
{
$lim = $_POST["lim"];
$from = $_POST["from"];
$search = $_POST["query"];
$sql = "SELECT job.id AS id, job.red_nr as red_nr, job.name as name, date(job.date) as date, status.name as status_name FROM job LEFT JOIN status ON job.status_id = status.id WHERE job.red_nr LIKE '%".$search."%' OR job.name LIKE '%".$search."%' ORDER BY job.id DESC LIMIT ".$from.", ".$lim;
}
else
{
$sql = "SELECT job.id AS id, job.red_nr as red_nr, job.name as name, date(job.date) as date, status.name as status_name FROM job LEFT JOIN status ON job.status_id = status.id ORDER BY job.id DESC LIMIT ".$from.", ".$lim;
}
$outputs = '
<table class="tbl_1">
<tr class="tbl_head">
<th>Red NR</th>
<th>Nosaukums</th>
<th>Datums</th>
<th>Status</th>
<th><div name="" id=""></th>
<th></th>
</tr>
';
$query = $pdo->prepare($sql);
$query->execute();
$result = $query->fetchAll();
$total_row = $query->rowCount();
if($total_row > 0)
{
$outputs .='
<tr class="tbl_row">
<td class="td_red_nr"><div id="rednew"><i class="fa fa-plus-square pogas new" id="new"/>Jauns darbs</div></td>
<td class="td_name"><div id="namenew"></div></td>
<td class="td_date"><div id="datenew"></div></td>
<td class="td_status"><div id="statusnew"></div></td>
<td class="td_buttons1">
<i style="display:none;" class="fa fa-check-square pogas save" id="new"/>
<i style="display:none;" class="fa fa-ban pogas cancel" id="new"/>
</td>
</tr>
';
foreach($result as $row)
{
$date = date_create($row["date"]);
$outputs .= '
<tr class="tbl_row" id="'.$row["id"].'">
<td class="td_red_nr"><div id="red'.$row["id"].'">'.$row["red_nr"].'</div></td>
<td class="td_name"><div id="name'.$row["id"].'">'.$row["name"].'</div></td>
<td class="td_name"><div id="date'.$row["id"].'">'.date_format($date, 'j M Y').'</div></td>
<td class="td_name"><div id="status'.$row["id"].'">'.$row["status_name"].'</div></td>
<td class="td_buttons1">
<i style="display:none;" class="fa fa-pencil-square pogas edit" id="'.$row["id"].'" />
<i style="display:none;" class="fa fa-check-square pogas save" id="'.$row["id"].'" />
<i style="display:none;" class="fa fa-ban pogas cancel" id="'.$row["id"].'" />
<i style="display:none;" class="fa fa-trash pogas delete" id="'.$row["id"].'" />
</td>
</tr>
';
}
$outputs .='</table>';
$out['outputs'] = $outputs;
echo json_encode($out);
}
else
{
$out['outputs'] = $outputs;
echo json_encode($out);
}
What a ...
Problem was that json_encode
somehow don't like <html>
tag in php file!!! Who could imagine that?
I removed both html tags and it started to work as it should be 3 days ago. There is no such problem with echo $something
to ajax.