I am trying to display an image on a webpage that I retrieved from the database using PHP and JavaScript in JSON format. I have tried looking for related questions with answers but I can't seem to comprehend anything. Please can someone help me in checking this out and provide some solution?
JavaScript/AJAX code
function load_data(query = '', page_number = 1)
{
var form_data = new FormData();
form_data.append('query', query);
form_data.append('page', page_number);
var ajax_request = new XMLHttpRequest();
ajax_request.open('POST', 'process_data.php');
ajax_request.send(form_data);
ajax_request.onreadystatechange = function()
{
if(ajax_request.readyState == 4 && ajax_request.status == 200)
{
var response = JSON.parse(ajax_request.responseText);
var html = '';
var serial_no = 1;
if(response.data.length > 0)
{
for(var count = 0; count < response.data.length; count++)
{
html += '<tr>';
html += '<td>'+"<img src='"+filename+"'>"+'</td>';
html += '<td>'+response.data[count].tag+'</td>';
html += '</tr>';
serial_no++;
}
}
else
{
html += '<tr><td colspan="3" class="text-center">No Data Found</td></tr>';
}
document.getElementById('post_data').innerHTML = html;
document.getElementById('total_data').innerHTML = response.total_data;
document.getElementById('pagination_link').innerHTML = response.pagination;
}
}
}
Thats the javascript file that I am using to pass data to html page
I get this error when running it:
The requested URL was not found on this server
Here is the PROCESS_DATA.PHP file
if(isset($_POST["query"]))
{
$a=1;
$data = array();
$limit = 5;
$page = 1;
if($_POST["page"] > 1)
{
$start = (($_POST["page"] - 1) * $limit);
$page = $_POST["page"];
}
else
{
$start = 0;
}
if($_POST["query"] != '')
{
$condition = preg_replace('/[^A-Za-z0-9\- ]/', '', $_POST["query"]);
$condition = trim($condition);
$condition = str_replace(" ", "%", $condition);
$sample_data = array(
':tag' => '%' . $condition . '%',
);
$query = " SELECT filename, tag FROM tags WHERE tag LIKE :tag ";
$filter_query = $query . ' LIMIT ' . $start . ', ' . $limit . '';
$statement = $conn->prepare($query);
$statement->execute($sample_data);
$total_data = $statement->rowCount();
$statement = $conn->prepare($filter_query);
$statement->execute($sample_data);
$result = $statement->fetchAll();
$replace_array_1 = explode('%', $condition);
foreach($replace_array_1 as $row_data)
{
$replace_array_2[] = '<span style="background-color:#'.rand(100000, 999999).'; color:#fff">'.$row_data.'</span>';
}
foreach($result as $row)
{
$data[] = array(
'image' => str_ireplace($replace_array_1, $replace_array_2, $row["filename"]),
'tag' => str_ireplace($replace_array_1, $replace_array_2, $row["tag"])
);
}
}
else
{
$query = "SELECT filename, tag FROM tags";
$filter_query = $query . ' LIMIT ' . $start . ', ' . $limit . '';
$statement = $conn->prepare($query);
$statement->execute();
$total_data = $statement->rowCount();
$statement = $conn->prepare($filter_query);
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $row)
{
$data[] = array(
'image' => $row['filename'],
'tag' => $row['tag']
);
}
}
$pagination_html = '
<div align="center">
<ul class="pagination">
';
$total_links = ceil($total_data/$limit);
$previous_link = '';
$next_link = '';
$page_link = '';
if($total_links > 4)
{
if($page < 5)
{
for($count = 1; $count <= 5; $count++)
{
$page_array[] = $count;
}
$page_array[] = '...';
$page_array[] = $total_links;
}
else
{
$end_limit = $total_links - 5;
if($page > $end_limit)
{
$page_array[] = 1;
$page_array[] = '...';
for($count = $end_limit; $count <= $total_links; $count++)
{
$page_array[] = $count;
}
}
else
{
$page_array[] = 1;
$page_array[] = '...';
for($count = $page - 1; $count <= $page + 1; $count++)
{
$page_array[] = $count;
}
$page_array[] = '...';
$page_array[] = $total_links;
}
}
}
else
{
for($count = 1; $count <= $total_links; $count++)
{
$page_array[] = $count;
}
}
for($count = 0; $count < count($page_array); $count++)
{
if($page == $page_array[$count])
{
$page_link .= '
<li class="page-item active">
<a class="page-link" href="#">'.$page_array[$count].' <span class="sr-only">(current)</span></a>
</li>
';
$previous_id = $page_array[$count] - 1;
if($previous_id > 0)
{
$previous_link = '<li class="page-item"><a class="page-link" href="javascript:load_data(`'.$_POST["query"].'`, '.$previous_id.')">Previous</a></li>';
}
else
{
$previous_link = '
<li class="page-item disabled">
<a class="page-link" href="#">Previous</a>
</li>
';
}
$next_id = $page_array[$count] + 1;
if($next_id >= $total_links)
{
$next_link = '
<li class="page-item disabled">
<a class="page-link" href="#">Next</a>
</li>
';
}
else
{
$next_link = '
<li class="page-item"><a class="page-link" href="javascript:load_data(`'.$_POST["query"].'`, '.$next_id.')">Next</a></li>
';
}
}
else
{
if($page_array[$count] == '...')
{
$page_link .= '
<li class="page-item disabled">
<a class="page-link" href="#">...</a>
</li>
';
}
else
{
$page_link .= '
<li class="page-item">
<a class="page-link" href="javascript:load_data(`'.$_POST["query"].'`, '.$page_array[$count].')">'.$page_array[$count].'</a>
</li>
';
}
}
}
$pagination_html .= $previous_link . $page_link . $next_link;
$pagination_html .= '
</ul>
</div>
';
$output = array(
'data' => $data,
'pagination' => $pagination_html,
'total_data' => $total_data
);
echo json_encode($output);
}