How to load an image respecting / linking with the return of a variable in Json

Asked

Viewed 168 times

0

Good morning friends....

I don’t know if I’ll be able to make myself understood, but I need to upload an image linked to the type of feedback I might receive from Json.

Example:

Red, Yellow and Truth Signals

echo 'SINAL' .$data["CreditData"][0]["SINAL"].'</td>'; 

Let’s say it returns red.... instead of appearing written, it wanted to carry a red image.... red.png

I wonder how I can do that?

<img src='' id="icone" width="60px">
<br/>

<script>
$(document).ready(function(){
	$('#icone').attr('src','https://meusite.com.br/icons/' + data[0].sinal + '.png');
	});
});
</script>

Hold on, have some idea to help me?

Hugs

  • The script is on the same PHP page or is a .js external?

1 answer

0


Marcos, all right?

You can leave a div or any other empty tag in the place where you want the image to appear. After that, in the result of your JSON, you can create an element img with javascript and hang it on your div empty.

For example:

<div id="image"></div>

<script>
var img = document.createElement("IMG");
img.src = "img/exemplo.png";
document.getElementById('image').appendChild(img);
</script>

Of course you should put the conditions according to your return, but this logic is the way you would like it. You can use the AJAX to capture this file JSON.

*EDIT: I’d rather you used a call "AJAX" to capture the JSON right in the JAVASCRIPT, without using the PHP. But we can solve this problem faster.

You can add this value from PHP within a input hidden, capture the value of it by javascript and do the process, for example:

<input value="<?php $data["CreditData"][0]["SINAL"] ?>" id="cor" type="hidden"></input>

<div id="image"></div>

<script>
var cor = document.getElementById('cor').value;
var img = document.createElement("IMG");

if(cor === "red")
   img.src = "img/vermelho.png";
else if(cor === "green")
   img.src = "img/verde.png";


document.getElementById('image').appendChild(img);
</script>

I think this will solve your problem, but I will leave a link for you to learn how the request works AJAX: https://www.devmedia.com.br/ajax-com-jquery-trabalhando-com-requisicoes-assincronas/37141

  • Luis, thanks for the return... But the question that img/example.png will vary... may be red.png or green.pnj or even yellow.png.. everything depends on what to return of the query to Json. Hence my doubt how to load the image that corresponds with what to return of the Json query

  • Could you tell me what the return of JSON is? You will probably have information that will tell you which color you want, based on that, you put "if... Else" conditions to tell you which color. You can change only img.src = "linkdaimg.png" within "if" by placing the path you need depending on the condition

  • Luis, he returns so echo 'SIGN' . $data["Creditdata"][0]["SIGN"]. The return of the SIGN can be: red or green dai as the one that appears I would like you to call ... red.png or green.png... could make an example for me?

  • Marcos, I’ve made an issue in my reply, I think it might help you!

  • Luis this well ... I’m just not getting connection with the Json... but I’m trying here

Browser other questions tagged

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