用jquery POST然后显示GD PHP


jquery with POST and then display GD PHP

我有一个简单的问题-为什么我的*.png图片,由PHP GD创建,不能在网页上正确显示?我的PHP代码是:

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);
?>

…和简单的jQuery AJAX post在这里:

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
      });

图片显示正确,但不是所有东西都可见。'line_1'是可见的,因为imageline(...)中的所有元素都是静态的,但'line_2'不显示,因为'imageline(..., $jVar, ...)具有由$_POST['jPacket']从scipt.js发送的值。在graphBack.insertAfter($('#sipGraph p'));中IMG是否正确显示?

当我执行上述方案,但数据不是从jQuery发送,而是普通的HTML表单一切都ok…

您发送的帖子不是页面上显示的内容。当您将var graphBack = $('<img id="backGraph" src="back.php" alt="backGraph"/>');附加到页面时,您正在创建第二个单独的请求,没有post -本质上是在页面上加载图像。Back实际上被调用了两次,使用POST的时间并不是您所显示的响应。

我将只是沿着$('<img id="backGraph" src="back.php?jPacket='+ MYAWESOMEJPACKET +'" alt="backGraph"/>');的行附加一个生成的图像标记,并替换当前的图像标记—进一步更新脚本以使用GET。