How to pass php variable as parameter in a JS function call

Asked

Viewed 121 times

2

Hello, I want to pass a php variable as a parameter in a Javascript function call. So:

<a href="calcular($numero)">Calcular</a>

I’ve tried it this way:

<a href="calcular(<?php echo $numero; ?>)></a>

However, the detail that makes it difficult, is that <a></a> is already inside a <?php ?> for being in a while.

Any idea of resolution?

  • You can simply use concatenation, without using the <?php. Something like that: <a href="calcular('.$numero.')></a>, but it depends a little on how you’re using the quotes. You can put the code of that while?

1 answer

1


You can simply use concatenation, without using the <?php. Something like that: <a href="calcular('.$numero.')></a>, but it depends a little on how you’re using the quotes.

For example:

<?php

while ($numero< 10){
    echo '<a href="calcular('.$numero.')"></a>';
}

?>

Browser other questions tagged

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