From what I understand, you defined the variable $cidade
on the PHP page and wants to refer it directly (using PHP code) to the file clima.js
.
If this is the case it won’t work because the block <script src="clima.js"></script>
is interpreted by the browser (client) and not by the server (which is the case for PHP codes).
What you can do to get around this is on the PHP page to insert a code of this type:
<script src="clima.js"></script>
<?php
// este elemento HTML faz a separação entre os códigos server e client side
?>
<input type="hidden" value="<?php echo $cidade;?>" id='idCidade'/>
<script>
// obtenção do parâmetro cidade
var idCidade = window.document.getElementById ('idCidade').value;
// função do arquivo clima.js
var result=getClima(idCidade);
alert(result);
</script>
It is good practice to separate the server side and client side codes (in your case, PHP and Javascript respectively). The more separated, the better for maintenance and understanding.
You want to pass the variable once when the page loads or you want to pass data after the page loads and without reloading the page?
– Sergio
Just one more duplicate... People had to get used to looking at the people next door or research if they already have an answer for this instead of going out answering.
– Bacco
Agree @Bacco, let’s create a discussion on Meta
– Wallace Maxters
@Wallacemaxters think this is not even the case. If the guy does not read the related, imagine participating in the goal. I will not give -1 this time in the answers because not so "wrong" (and there are new people who are helping in the best intention), despite having some made with crystal ball.
– Bacco
@Sergio I even looked and researched old questions but I could not understand how I would even so I decided to post a new question I believe that this can be a simple step for experienced and complicated programmers for beginners as you can see I did what I was suggested and even then did not work if you can give a help will be welcome
– Silva