接收Ajax并在Photo中显示输出?

时间:2012-09-30 08:15:25

标签: php jquery ajax image

如何在Photo中接收Ajax并显示输出? 图像应显示在div标签内 在每个拜耳文本之后,更改文本以更改照片内容 显示的照片以

的形式书写

此代码

<script>
function AjaxRequest() {
    var xmlhttp = null;
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp = new XMLHttpRequest();
    } // else // code for IE6, IE5
    return xmlhttp;
}

function ajaxAction() {
    var font_category = document.getElementById("ajaxField").value;
var xmlHttp = AjaxRequest();
    xmlHttp.open("POST" , "ajax.php?message=" + message , true);
    xmlHttp.onreadystatechange = function() {
        if(xmlHttp.readyState == 1)
            response.innerHTML = "Loading...";
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
            response.innerHTML = xmlHttp.responseText;
    }
    xmlHttp.send(null);
}
</script>

<textarea id="ajaxField12" name="message" rows="2" cols="20" value="Send" onchange="javascript:ajaxAction();">

</textarea> 


<div id="ajaxResponse" height="42" width="42"> 
</div>

2 个答案:

答案 0 :(得分:0)

如果你想要显示照片而不是使用类似的东西

$("document").ready(function(){
$("textarea").on("keyup",function(){ 
      $values=$(this).val();
      $.get("ajax.php?message="+values,function(data){
     $("#ajaxResponse").html("<img src="+data+" height='10' width='10'>");

})

})

})

在您的服务器端

 <?php
    $statement = $pdo->prepare("SELECT img.src from img WHERE message=?");
    $statement->execute(array($_GET["message"]));
    if($row_count = $statement->rowCount()>0);
       { $row = $statement->fetch(PDO::FETCH_ASSOC);
       echo $row["src"];
       }
    ?>

您不应使用mysql_*个功能 您应该了解准备好的语句并使用PDO或MySQLi。如果您无法决定,请查看评论或转到PDO

答案 1 :(得分:0)

我修改了这样的代码 但它不会在屏幕上打印任何内容 甚至文本的内容

$("document").ready(function(){
$("textarea").on("keyup",function(){ 
      $values=$(this).val();
      $.get("ajax.php?message="+values,function(data){
     $("#ajaxResponse").html("<img src="+data+" height='10' width='10'>");
})
})
})
<textarea id="textarea" name="textarea" rows="2" cols="20" value="Send">

</textarea>
<div id="ajaxResponse" height="42" width="42"> 
</div>

ajax.php代码是:

   $message = 'test';
    $message = $_REQUEST['message'];
    $im = @imagecreate(110, 20)
        or die("Cannot Initialize new GD image stream");
    $background_color = imagecolorallocate($im, 0, 0, 0);
    $text_color = imagecolorallocate($im, 233, 14, 91);
    imagestring($im, 1, 5, 5,  $message, $text_color);
    imagepng($im);
    imagedestroy($im);

但是在ajax.php中有错误!