I have simple question – why my *.png picture, created by PHP GD, is not displayed properly on the web page?
My PHP code is:
back.php
<?php
$height = 200;
$width = 600;
$jVar = $_POST['jPacket']; //velue from jQuery script
$im = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefill($im, 0, 0, $white);
//line_1
imageline($im, 10, 0, 10, $height, $black);
//line_2
imageline($im, $jVar, ($height/2), $height-$jVar, ($height/2), $black);
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
…and simple jQuery AJAX post is here:
script.js
var graphBack = $('<img id="backGraph" src="back.php" alt="backGraph"/>');
$('.value').keyup(function() {
$.post(
"back.php", //send the POST here
{
jPacket: $('.value').val() //value from <input>
}, function() {
$('#backGraph').remove(); //remove old picture
graphBack.insertAfter($('#sipGraph p')); //put the picture here
});
The picture is displayed properly but not everything is visible. The ‘line_1’ is visible because all elements in imageline(...) are static but the ‘line_2’ is not displayed because 'imageline(..., $jVar, ...) has value that is send by $_POST['jPacket'] from scipt.js.
Do I properly show the IMG in graphBack.insertAfter($('#sipGraph p'));?
When I perform above scanario but data is sent not from jQuery but ordinary HTML FORM everything is ok…
The post you are sending is not what is being shown on the page. When you attach
var graphBack = $('<img id="backGraph" src="back.php" alt="backGraph"/>');to the page, you are creating a second, separate request with no post – essentially loading an image on the page. Back is being called twice essentially, and the time with POST is not the response that you are displaying.I would instead simply attach a generated image tag along the lines of
$('<img id="backGraph" src="back.php?jPacket='+ MYAWESOMEJPACKET +'" alt="backGraph"/>');and replace the current one – further updating the script to use GET.