How to pass an array from php (using json_encode) to javascript

My java script. I want to display an image. javascript requires an image path as an array. I tried to put the ajax path. this does not work. when I use the hard code of his work. my javascipt is below. this does not work. working code code below javascript and my php file code is also there.

$(function () {
    $.get( "php/building_edit_image_get_db.php", function( your_return_data ) {
    alert(your_return_data);

        $("#editimagefile").fileinput({
            showUpload: false,
            showCaption: false,
            overwriteInitial: true,
            initialPreview: [your_return_data],
            initialPreviewAsData: true, // identify if you are sending preview data only and not the raw markup
            initialPreviewFileType: 'image', // image is the default and can be overridden in config below
            browseClass: "btn btn-primary btn-lg",
            allowedFileExtensions : ['jpg', 'png','gif']
        });
    });
});

This does not work. when I install hard code that it works correctly, the script is below

$(function () {
    $.get( "php/building_edit_image_get_db.php", function( your_return_data ) {
    alert(your_return_data);

        $("#editimagefile").fileinput({
            showUpload: false,
            showCaption: false,
            overwriteInitial: true,
            initialPreview: [
                "http://lorempixel.com/800/460/people/1",
                "http://lorempixel.com/800/460/people/2"
            ],
            initialPreviewAsData: true, // identify if you are sending preview data only and not the raw markup
            initialPreviewFileType: 'image', // image is the default and can be overridden in config below
            browseClass: "btn btn-primary btn-lg",
            allowedFileExtensions : ['jpg', 'png','gif']
        });
    });
});

My php file to save the value of the array.

session_start();
require_once ('../aiboc_admin/class/Buidling_Image.php');

$editid = $_SESSION['BUILD_LIST_EDIT_ID'];

$getimgs = Buidling_Image::GetGalleryImageByID($editid);

    foreach ($getimgs as $setimgs)
    {
        $imgs[] = $setimgs['img_url'];
    }
echo json_encode($imgs,JSON_UNESCAPED_SLASHES);
+4
source share
1 answer

You should use $.parseJSON(), since you get the json format when using json_encode:

$.get( "php/building_edit_image_get_db.php", function( your_return_data ) {

    $("#editimagefile").fileinput({
        showUpload: false,
        showCaption: false,
        overwriteInitial: true,

        initialPreview: $.parseJSON(your_return_data),

        initialPreviewAsData: true, // identify if you are sending preview data only and not the raw markup
        initialPreviewFileType: 'image', // image is the default and can be overridden in config below
        browseClass: "btn btn-primary btn-lg",
        allowedFileExtensions : ['jpg', 'png','gif']
    });

});

$.getJSON() $.get(), :

$.getJSON( "php/building_edit_image_get_db.php", function( your_return_data ) {

, .

+2

Source: https://habr.com/ru/post/1660049/


All Articles