mardi 3 mars 2015

Getting an image source from JSON data

I'm attempting to call a PHP file and have it return a result (a single record's 'pageLocation') from a database table ('page'). I then want to get that result into a variable, so I can use it while creating an image in html.


Currently, the image is being created but the source is not feeding into it, leaving a default empty image at the correct size.


Javascript:



// Loads a list of comics created by the user from the database.

function loadComic()
{
var xmlhttp = new XMLHttpRequest();
var getID = '<?php echo $_SESSION["userID"]; ?>';
var url = "loadCom.php?userID="+getID;


xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
loadComicJSON(xmlhttp.responseText);
}
}

xmlhttp.open("GET", url, true);
xmlhttp.send();

}

// JSON parsing for 'loadComic'.

function loadComicJSON(response)
{
var arr = JSON.parse(response);
var i;
var out = "";

document.getElementById("loadList").innerHTML="";

if (arr.length == 0)
{
//Non-relevant code affecting layout if no comics are found.
}
else
{
out+="<br>";

for(i = 0; i < arr.length; i++)
{

// Gets image source from database.

imgSrc = "";
tempID = arr[i].comicID;
$.post("getCover.php", {'comicID':tempID}, function(result)
{
imgSrc += ("" + result);
}
);

// Creates image item and associated radio button.

out += "<hr><br><img name = '" + ('com' + arr[i].comicID) + "' id='" + ('com' + arr[i].comicID) + "' onclick='resizeThumb(this)' height='100px;' src='" + imgSrc + "'><input name='comicList' type='radio' id='" + arr[i].comicID + "' value='" + arr[i].comicID + "'>" + arr[i].comicName + " </option><br><br>";
}

}


}

</script>


PHP (getCover.php):



<?php
if (isset($_POST["comicID"]))
{
include_once('includes/conn.inc.php');
$checkID = $_POST["comicID"];

$query = ("SELECT FIRST (pageLocation) FROM page WHERE comicID = '$checkID' ORDER BY pageNum");
$result = mysqli_query($conn, $query);


$conn->close();
echo ($result);
}
else
{
$checkID = null;
echo "Error. No comic found.";
}

?>


Thanks for any help provided.


Aucun commentaire:

Enregistrer un commentaire