How to send a php variable in a javascript function parameter?

Asked

Viewed 534 times

0

I want to send a php variable when calling a javascript function.
Thus:

href="javascript:calcular($valor1)"

I try to do this, it doesn’t seem to work. Is there some other way to do this?

  • 1

    I suggest learning to search the site. There are more than 200 questions (really serious) with this subject and several examples.

2 answers

1

Just print the variable using the function echo

href="javascript:calcular(<?php echo $valor1; ?>)"
  • It still doesn’t pass. I created a function just for testing purposes: function calcular(cod){ alert(cod);} and it didn’t work that way.

  • 1

    What the generated html code looks like?

  • I don’t know what you mean

1

Hello, I would do so:

function calcular(element){
  var variavel = $(element).data('var');
}
<a data-var="<?= $calcular ?>" onClick="calcular(this)">Clique</a>

Browser other questions tagged

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