4

The short of what I'm trying to do is search for a file and display a picture from the server. The HTML has a simple search bar that allows you to type in a search term. The JavaScript uses an ajax request to call the PHP file, and the PHP finds the image on the server and sends it back to be displayed.

What happens right now is that the image isn't displayed, and I get an icon indicating some invalid image. The ajax call appears to be working, but I think the data I'm sending back isn't correct. I've been trying to search for it but everyone seems to have a different opinion on how to do it and it's kind of confusing.

HTML

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">    </script>
<script src="search.js"></script>
<style type="text/css"></style>
</head>
</body>
    <header>
    <h1>My Page</h1>
    </header>
    <input type=text name="search" id="searchbox" placeholder="Search...">
    <input type=button value="Search" id=search><br>
    <div id="images"></div>
</body>

JavaScript

$(document).ready(function() {
    $("#search").on('click', function(){
        $.ajax({
           url: '/search.php',
           type: 'POST',
           data: $("#searchbox").serialize(),
           success: function(data) {
            $('#images').append(data);
           },
           error: function() {
            alert('failure');
           }
        });
    });
});

PHP

<?php
if(isset($_POST['search'])) {
    $searchterm = $_POST['search'];
    $image = "images/".$searchterm.".jpeg";
    echo '<img src="data:image/jpeg;base64,'.base64_encode($image).'">';
}
else {
    echo 'Error: image not found';
}

PS. For the moment, I'm ignoring any sort of error checking, I'm just trying to get it working and assuming the input is all valid

5
  • 1
    have you logged it in console or made alert to check the value. Commented Sep 19, 2016 at 4:17
  • try to change .append with .html, but fisrt check the preview from PHP file. Commented Sep 19, 2016 at 4:20
  • I have, and I get something that looks like an encoded image in an image tag: <img src="data:image/jpeg;base64,aW1hZ2VzL2hpdG9taV90YW5ha2FqcGVn"> Commented Sep 19, 2016 at 4:22
  • 1
    Try my suggestions below. Note the use of mime_content_type(). Note, too, that "aW1hZ2VzL2hpdG9taV90YW5ha2FqcGVn‌​" looks awfully short to be a legitimate jpeg file :) I'd strongly suggest looking at your HTTP request/response with a tool like Fiddler, FF/Firebug and/or Chrome Developer Tools. To make sure you're actually sending what you think you're sending. IMHO... Commented Sep 19, 2016 at 4:27
  • @paulsm4 yeah you were right, it's a lot longer now and that makes more sense. However, my page still isn't displaying the image. Commented Sep 19, 2016 at 4:32

3 Answers 3

1

Suggestions:

  1. Try this link: Image Data URIs with PHP

Code:

<?php
if(isset($_POST['search'])) {
    $searchterm = $_POST['search'];
    $image = "images/".$searchterm."jpeg";
    $imageData = base64_encode(file_get_contents($image));
    $src = 'data: '.mime_content_type($image).';base64,'.$imageData;
    echo '<img src="', $src, '">';
    ...
  1. Debug the actual HTTP traffic between your jQuery/Browser and your PHP/back-end server. You can use a tool like Telerek Fiddler or Firebug, among many others.
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this, I get back a very long return from the PHP file with what I assume is the image file I requested. However, the HTML doesn't display anything.
Q: So what did Firebug show you? Q: What happens if you browse directly to the image (httpd://myserver/images/myimage.jpeg), without PHP?
$image = "images/".$searchterm."jpeg"; goes $image = "images/".$searchterm.".jpeg";
1

Use file_get_contents it will display the image on browser.

$image = "images/".$searchterm.".jpeg";
echo '<img src="data:image/jpeg;base64,'.base64_encode(file_get_contents($image)).'">';

Comments

0

Please change the url property in the object, used in your .ajax() method call. The path to your search.php is incorrect.

$.ajax({
    url: '/search.php',

goes to:

$.ajax({
    url: './search.php',

1 Comment

Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.