How to exchange an image for Javascript

Asked

Viewed 147 times

0

I need to change an image through Java but I’m not getting it, I tried it this way and it didn’t work;

html

<img id="imagemPerfil" src="img/user1.png" alt="profile image"  class="circle z-depth-2 responsive-img activator gradient-45deg-light-blue-cyan gradient-shadow">

Javascript

document.getElementById("imagemPerfil").src ="<?php echo'../img/perfilUsuario/'.$usuario_pesquisado->getImg(); ?>";

I noticed that the value is received by php, but it is not passed to the image src

3 answers

0

Hello,

I see two options that Voce can use, maybe with more details of how you want to use I have other ideas, but let’s go there:

Voce can assign direct img to src:

<img id="imagemPerfil" src="<?php echo'../img/perfilUsuario/'.$usuario_pesquisado->getImg(); ?>" alt="profile image"  class="circle z-depth-2 responsive-img activator gradient-45deg-light-blue-cyan gradient-shadow">

Or you create an Ajax request via javascript and on Onsucess you change the image.

0

This may be browser caching, or server caching primarily if you are using a CDN.

Try placing a url with parameters to see if it doesn’t resolve. Example: /image/file.jpg?v=2

  • worse than not, I saw by the console that the way is still that of the old image

  • When you get the image by the direct url in the Browser it is loaded correctly?

  • Try placing another value, and check by the browser’s Inspect on the page if this new value is assigned.

  • yes I can catch the image by the browser

  • changed the value in javascript did not work, even removing from php

0


In the head of the page where the javascript will be

<script src="local/do//jQuery.js"></script>

In the context of HTML / Who shows the image

<img id="imagemPerfil"
     alt="profile image"
     class="circle z-depth-2 responsive-img activator gradient-45deg-light-blue-cyan gradient-shadow">

In the context of Javascript / Who asks for the image

<script>
    $(document).ready(function () {
        var params = {
            Request: "GetImgPerf"
        };
        $.get("PerfilUsuarioController.php", params, function (respImg) {
            $("#imagemPerfil").attr("src", respImg);
        });
    });
</script>

In the part where you enter the Profileecontroller.php, is where I did the test here at home, but to work for you, Check in your project, which is the way to access the image.

Generic PHP example / Who provides the image

class PerfilUsuarioController {

    public function verificarRequest() {
        $request = $_GET["Request"];
        switch ($request) {
            case "GetImgPerf":
                $this->getImgPerfil();
                break;

            default:
                break;
        }
    }

    public function getImgPerfil() {
        // Codigo de busca do usua ou qualquer outra verificação
        print $usuario_pesquisado->getImg();
    }

}

$perf = new PerfilUsuarioController();
$perf->verificarRequest();

Browser other questions tagged

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