简体   繁体   中英

Display Image on HTML image using javascript

I am trying to display an image using javascript from mysql table and also using php. I have a table named ci_images and has a fields id, ci_ID, ciCode, Title, image . After getting the rows using the code sql statement below, I want to display it using javascript. But i am not getting the right result on this code. It always returns an object in Console of the browser (image). Please see code below

FIELDS:

id - int

ci_ID - int

ciCode - VarChar

Title - VarChar

image - LongBlob

PHP:

<?php
    include_once('pConfig.php');
    if (!isset($cID)){
        $cID = filter_input(INPUT_GET, "cIDs");
    }   
    if (!isset($ciCODe)){
        $ciCode = filter_input(INPUT_GET, "ciCodes");
    }   
    $stmt = $db->prepare("SELECT * FROM ci_images WHERE ci_ID = ? AND ciCODe = ?");
    $stmt->bind_param('is', $cID , $ciCode);
    $stmt->execute();
    $result = $stmt->get_result();
    if (!$result) {
        printf("Error: %s\n", mysqli_error($db));
        exit();
    }
    $json = mysqli_fetch_all($result, MYSQLI_ASSOC);
    echo json_encode($json);
?>

JAVASCRIPT:

function previewImages(cID){
        var ciCode = window.localStorage.getItem('ciCode');
        var xdata = ({'cIDs': cID, 'ciCodes': ciCode });
        $.ajax({
        type: 'GET',
        url: '../back_php_Code/pPrevImages.php',
        dataType: 'json',
        data: xdata,
        contentType: 'application/json; charset=utf-8',
        success: function (response) {
            var cells = eval(response);
            alert(JSON.stringify(cells));
            for (var i=0; i < cells.length ; i ++){
                    $('#iSet').append('<div class="col-lg-4 col-sm-6">'
                    + '<div class="thumbnail">'
                    + '<div class="thumb">'
                    + '<a href="..\files\assets\images\gallery-grid\1.png" data-lightbox="9" data-title="' + + '">'
                    + '<img src="'+ + '" alt="" class="img-fluid img-thumbnail">'
                    + '</a>'
                    + '</div></div></div>');            
            }
        },
         error: function (error) {
            console.log(error);
        }
   });  
}

Return on Console.

在此处输入图片说明

Can someone help me to do the trick for this, I am stuck on this.

Thanks and Regards

Also, I have found something similar, but it does not work because , it only uses php and html. I think. Again Thanks

Link:

PHP display image BLOB from MySQL

From PHP you are returning json string, so in javascript you do not have to eval the result, but decode it with JSON.parse .
Other than that you are returing a single record, not a list, why you are doing a for loop in javascript?

Your code will become:

function previewImages(cID) {
    var ciCode = window.localStorage.getItem('ciCode');
    var xdata = ({'cIDs': cID, 'ciCodes': ciCode });
    $.ajax({
    type: 'GET',
        url: '../back_php_Code/pPrevImages.php',
        dataType: 'json',
        data: xdata,
        contentType: 'application/json; charset=utf-8',
        success: function (response) {
            // here you transform your response in a js Object
            var row = JSON.parse(response);

            $('#iSet').append('<div class="col-lg-4 col-sm-6">'
                    + '<div class="thumbnail">'
                    + '<div class="thumb">'
                    + '<a href="..\files\assets\images\gallery-grid\1.png" data-lightbox="9" data-title="' + row.YOUR_FIELD_FROM_DATABASE + '">'
                    + '<img src="' + row.YOUR_FIELD_FROM_DATABASE + '" alt="" class="img-fluid img-thumbnail">'
                    + '</a>'
                    + '</div></div></div>');
        },
         error: function (error) {
            console.log(error);
        }
   });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM