【问题标题】:ajax image as response in phpajax 图像作为 php 中的响应
【发布时间】:2010-09-26 05:34:54
【问题描述】:

我想显示一个图像作为对使用 php 的 ajax 调用的响应。有人有完整的代码吗?请帮帮我。。 提前谢谢..

【问题讨论】:

    标签: php ajax image response


    【解决方案1】:

    好吧,我没有完整的代码,如果有,我不会给你。

    您需要首先使用PHP's various image generation functions 创建图像。要使其动态化,您可以发送它various parameters which are encoded in the url,并且可以使用 php 中的 $_GET 超全局来获取。

    然后,您可以将现有图像占位符元素的 src 设置为动态图像的位置,然后瞧!即时动态图像。

    【讨论】:

    • 如果提问者在上述任何方面有困难,那么他们可能不应该尝试他们正在做的任何事情,而是尝试更简单的事情。
    【解决方案2】:

    您应该使用jQuery + JSON 组合。您可以使用json_encode将php数组转换为JSON格式。

    index.php:

    <script type="text/javascript" src="jquery-1.4.2.js"></script>
    <script type="text/javascript" src="ajax.js"></script>
    
    <a href='car.php' class='ajax'>Car Image</a>
    <a href='bike.php' class='ajax'>Bike Image</a>
    
    <div id="title">Title comes here</div>
    <div id="image">Image comes here</div>
    

    car.php:

    <?php
    $jsonArray['title'] = "Car";
    $jsonArray['image'] = "<img src='images/car.jpeg'>";
    echo json_encode($jsonArray);
    ?>
    

    bike.php:

    <?php
    $jsonArray['title'] = "Bike";
    $jsonArray['image'] = "<img src='images/bike.jpeg'>";
    echo json_encode($jsonArray);
    ?>
    

    ajax.js:

    jQuery(document).ready(function(){
    
      jQuery('.ajax').click(function(event) {
    
          event.preventDefault();
    
          jQuery.getJSON(this.href, function(snippets) {
              for(var id in snippets) {
                  jQuery('#' + id).html(snippets[id]);
              }
          });
      });
    
    });
    

    【讨论】:

      猜你喜欢
      • 2013-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-07
      • 2010-11-05
      • 1970-01-01
      • 2011-09-17
      • 1970-01-01
      相关资源
      最近更新 更多