dongqiuge5435 2014-12-16 15:53
浏览 29

有问题显示使用$ .post()jquery和php从数据库返回图像

I'm retrieving a table from database as result of a search action, but when I try to display the result I see the table and the data, but the image returned in each row is not render, I think my problem is in the $('#search').html(data), I'm not sure please someone knows what is the problem?

this is the result

http://s9.postimg.org/mro5qn46n/search_result.jpg

****This is the search page, where result table is displayed****

<table align="center">
  <tr>
     <td>
     <label for="criteria">Select Criteria</label>
     </td>
    <td>
       <select name="select" id="criteria">
           <option selected="true" style="display:none;"></option>              
           <option value="value1">name</option> 
           <option value="value2">apartment</option>
      </select>
  </td>
  <td>
      <input type="text" name="value" size="40" maxlength="60" id="value"\>
  </td>
  </tr>
  <tr>
  <td>
      <input name="name-submit" type="button" id="submit" value="Search"\>

  </td>
  </tr> 
  <tr>
  <td >
  <div id="search"></div>
  </td>
  </tr> 
</table> 

<script type="text/javascript" src="../js/jquery-1.11.1.min.js"></script>

<script type="text/javascript">
$('#submit').click(function(){

    var criteria = $("#criteria option:selected").text();
    var    value = $("#value").val();

    $.post("search_r.php",{criteria:criteria,value:value},function(data){

        $('#search').html(data);

          });

    });

</script> 

****This is the Page that calls $.post() in the main seach page ****

<?php
$criteria = $_POST['criteria'];
  $value = $_POST['value'];

      $con = mysqli_connect("localhost","root","");
            mysqli_select_db($con,"gables");

   $query = "SELECT * FROM residents WHERE $criteria = '$value'";       
  $result = mysqli_query($con,$query);

    echo "<table border='1'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Last Name</th>
<th>Apartment</th>
<th>Parking</th>
<th>Phone1</th>
<th>Phone2</th>
<th>image</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
  echo "<tr>";
  echo "<td>" . $row['0'] . "</td>";
  echo "<td>" . $row['1'] . "</td>";
  echo "<td>" . $row['2'] . "</td>";
  echo "<td>" . $row['3'] . "</td>";
  echo "<td>" . $row['4'] . "</td>";
  echo "<td>" . $row['5'] . "</td>";
  echo "<td>" . $row['6'] . "</td>";

  echo "<td><img src=get_image.php?id=".$row['0']." width=160 height=120/></td>";   
  echo "</tr>";
}
echo "</table>";


?>

***Here the get_image.php****

<?php
$con = mysqli_connect("localhost","root","");
       mysqli_select_db($con,"gables");

$id = $_GET['id'];

$query = "SELECT * FROM residents WHERE id='$id'";
$result = mysqli_query($con,$query);

if($result)
$picture = mysqli_fetch_array($result);
header('Content-Type: image/jpg');

echo $picture['11'];

?>
  • 写回答

1条回答 默认 最新

  • dpy15285 2014-12-16 16:39
    关注

    You can chage the get_image.php file as this. Then this work will work.

    <?php
    function get_image($id){
    $con = mysqli_connect("localhost","root","");
           mysqli_select_db($con,"gables");
    
    $query = "SELECT * FROM residents WHERE id='$id'";
    $result = mysqli_query($con,$query);
    
    if($result)
    $picture = mysqli_fetch_array($result);
    return $picture['11'];
    }
    ?>
    

    Then use require_once(); function and in image src like this.echo "<td><img src='".get_image($row['0'])."' width=160 height=120/></td>";. In your code check how the src path will print and it will be print like as echo string, not as a execute the file.

    echo 
    
        "<table border='1'>
        <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Last Name</th>
        <th>Apartment</th>
        <th>Parking</th>
        <th>Phone1</th>
        <th>Phone2</th>
        <th>image</th>
        </tr>";
        require_once('search_r.php');
        while($row = mysqli_fetch_array($result))
        {
          echo "<tr>";
          echo "<td>" . $row['0'] . "</td>";
          echo "<td>" . $row['1'] . "</td>";
          echo "<td>" . $row['2'] . "</td>";
          echo "<td>" . $row['3'] . "</td>";
          echo "<td>" . $row['4'] . "</td>";
          echo "<td>" . $row['5'] . "</td>";
          echo "<td>" . $row['6'] . "</td>";
    
          echo "<td><img src='".get_image($row['0'])."' width=160 height=120/></td>";   
          echo "</tr>";
        }
        echo "</table>"; 
    
    评论

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了