Call javascript by passing php variable

Asked

Viewed 624 times

-4

I need to call a javascript by passing a php variable to it, when I put the whole script in php I can pass the variable this way location: '<?php echo $cidade;?>, BR', but when I call him that <script src="clima.js"></script> I don’t know how to tell for the.js climate that the variable it should use is the $city that already exists in php

  • 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?

  • 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.

  • Agree @Bacco, let’s create a discussion on Meta

  • @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.

  • @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

2 answers

2


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.

1

Good I would recommend you assign the variable for an input and there in javascript you do the value capture.

    <input type="hidden" value="<?= $suavarial ?>" id='cidadeClima'/>

    <script>

     var getCidade = window.document.getElementById('cidadeClima').value;
     console.log(getCidade);

   </script>
  • +1 for not being able to.

  • 1

    Except for the fact that you’re wearing jQuery and the same has not been mentioned at any time.....

  • @Celsomtrindade, sorry. They are so conditioned to use jQuery, that when I think (wrong) in Avascript, the use of jQuery comes to mind. But I will edit the response for javascript

  • @Altered Celsomatrinity.

  • How will this climate be used.js ? will be visible on all pages or on a specific page ?

  • @Lucaoa the file climate.js will be called on several pages I did as follows and it worked to see if it is the best way in php <script>&#xA; function varCod(){&#xA; var cod = document.getElementById('idCidade').value;&#xA; return cod;&#xA; }&#xA; var cCod = varCod();&#xA; </script> and in the climate.js put var cidade = cCod;&#xA;&#xA; $(document).ready(function() {&#xA; $.simpleWeather({&#xA; location: cidade+', BR', worked is the right way

  • If it worked beauty. I just hope I haven’t given an echo <script>...</script>, because it is totally against the idea of Designer Patterns.

Show 2 more comments

Browser other questions tagged

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