Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4567614
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:57:23+00:00 2026-05-21T18:57:23+00:00

I have an ajax script, which I kinda understand, but still need some extra

  • 0

I have an ajax script, which I kinda understand, but still need some extra help.

$('.images').click(function(){
    var imageId = $(this).attr('id');
    alert(imageName);
    $.ajax({
            type: "get",
            url: "imageData.php",
            dataType: "json",
            data: {getImageId: imageId},
            error: function() {
                alert("error");
            },
            success: function(data){
                alert(imageId);
                $("#images_"+imageId).html(data);
            }
        });
    //$('#images_'+imageId).toggle();

});

I have that code, it goes to this imageData.php file

<?php
if(isset($_GET)){
    $images = "";
    $path = 'img/';
    $imageId = $_GET['getImageId'];
    $sql = mysql_query("SELECT * FROM images WHERE iID = '".$imageId."'");
    while($row = mysql_fetch_array($sql)){
        $images .= $path.$row['images'];
}
    $json = json_encode($images);
?>
<img src='<?php echo $json;?>'/>

    <?php
}
    ?>

Why does it output error when I try to echo a string from $images, but it outputs correctly when I do echo $imageId;? I’m trying to output something from mysql, but not trying to output just the id.

Need help please, thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T18:57:24+00:00Added an answer on May 21, 2026 at 6:57 pm

    You don’t need use json_encode here, there is not data that needs to be in JSON format. There is also no reason to loop over the result set, if the query only returns one image.

    Try this:

    <?php
    if(isset($_GET['getImageId'])) {
        $path = '';
        $imageId = mysql_real_escape_string($_GET['getImageId']); // SQL injection!
        $result = mysql_query("SELECT images FROM images WHERE iID = '".$imageId."'");
        $row = mysql_fetch_array($result);
        if($row) {
            $path = 'img/' . $row['images'];
        }
    }
    ?>
    <?php if($path): ?>
        <img src='<?php echo $path;?>'/>
    <?php endif; ?>
    

    If the iID is actually an integer, you need to omit the single quotes in the query.

    You also have to change the dataType from json to html, as you are returning an image tag (HTML) and not JSON:

    $.ajax({
        type: "get",
        url: "imageData.php",
        dataType: "html",
        data: {getImageId: imageId},
        error: function() {
            alert("error");
        },
        success: function(data){
            $("#images_"+imageId).html(data);
        }
    });
    

    Another option is to return only text (the link) and create the images on the client side:

    <?php
    if(isset($_GET['getImageId'])) {
        $path = '';
        $imageId = mysql_real_escape_string($_GET['getImageId']); // SQL injection!
        $result = mysql_query("SELECT images FROM images WHERE iID = '".$imageId."'");
        $row = mysql_fetch_array($result);
        if($row) {
            echo 'img/' . $row['images'];
        }
    }
    ?>
    

    And in JavaScript:

    $.ajax({
        type: "get",
        url: "imageData.php",
        dataType: "text",
        data: {getImageId: imageId},
        error: function() {
            alert("error");
        },
        success: function(data){
            $("#images_"+imageId).html('<img src="' + data + '" />');
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a input tag which is non editable, but some times i need
I have the following script which I need to combine together somehow. Each function
I have the following script which works kind of... $(document).ready(function(){ // add or remove
I have ajax call which pulls from the processing script 'getajax.php'. Call is made
I have a script which is posting some form data to a Quickbase and
Im using Ajax to send values to a PHP script which writes some value
I have a perl script which is called via Ajax. It simply writes 3
I have a script that retrieves objects from a remote server through an Ajax
I have $.ajax({ url: identity, success: function(data) { ProcessIdentityServer(data) } }); When 'data' is
I have some AJAX work that brings across an additional form element that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.