【问题标题】:Receive Ajax and display output in a Photo?接收 Ajax 并在照片中显示输出?
【发布时间】:2012-09-30 08:15:25
【问题描述】:

如何接收 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>

【问题讨论】:

  • 代码有问题吗?

标签: php jquery ajax image


【解决方案1】:

如果你想展示照片而不是使用这样的东西

$("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

【讨论】:

  • 如果可以避免,请不要使用mysql_* 功能,它们不再维护,社区已经开始deprecation process 。相反,您应该了解prepared statements 并使用PDOMySQLi。如果您无法决定,this article 将帮助您选择。如果你想学习,here is a good PDO-related tutorial.
  • @vascowhite 我对此很熟悉,但我只是在展示。与我们之前的 cmets 相同。可能是用户不熟悉 PDOmysqli
  • 再说一次,我要对你说,你不应该教人们坏/错误的做法。如果 OP 不熟悉 PDO,请向他介绍它。学习起来并不比 mysql_* 函数难。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-22
  • 1970-01-01
  • 1970-01-01
  • 2018-02-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多