show image coming from postgresql database with ajax

Asked

Viewed 76 times

1

I saved the image in the postgresql(oid) database with php. Now I intend to show this image on the screen through AJAX, but I’m not getting it. The line "$("#image") is maintained. attr("src","data:image/'.$sqlRow ['extension'].';Base64,'.$sqlRow ['image'].'");" from the table page.php does not display anything, if the row is removed. populates the input text.

Table:

<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <thead>
    <tr>
      <th>id</th>
      <th>nome</th>
      <th>botao</th>
    </tr>
  </thead>
  <tbody>
  <?php $tab = pg_query($db,"select id, nome from tabela"); while($tabRow = pg_fetch_assoc($tab)){ ?>
  <form>
    <tr>
      <td><?php echo $tabRow['id'];?></td>
      <td><?php echo $tabRow['nome'];?></td>
      <td><button type="button" id="<?php echo $tabRow['id'];?>" name="btn" onclick="btn(this)">consultar</button></td>
    </tr>
  </form>
  <?php } ?>      
  </tbody>
</table>
<div id="mostra"></div>
<br />
<!-- resultado a ser mostrado -->
<form>
<input type="id" name="id" value=""<br />
<input type="nome" name="nomr" value=""<br />
<img id="imagem" src=""/>
</form>

jQuery/Ajax script:

<script>
function btn(e){
    var txt = $(e).attr('id');
    $.ajax({
        type: 'post',
        url: 'tabela.php',
        data: {id: txt},
        beforeSend: function() { $("#loader").show(); },
        success: function(data) { $("#loader").hide(); }
    })
    .done(function(data){
        $("#mostra").html(data);
        //$("#form")[0].form.reset();
    });
}
</script>

Table page.php that gets the instruction and gives echo:

if(isset($_POST['id'])){

$sql = pg_query($db,"select id, nome, encode(lo_get(imagem),'base64') as imagem, extensao from tabela where id = ".$_POST['id']."");
$sqlRow = pg_fetch_assoc($sql);

echo '<script>
      $(function(){
          $("#id").val("'.$sqlRow ['id'].'");
          $("#nome").val("'.$sqlRow ['nome'].'");         
          $("#imagem").attr("src","data:image/'.$sqlRow ['extensao'].';base64,'.$sqlRow ['imagem'].'");
      });
      </script>';
}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.