Pass PHP variable value inside while loop for javascript

Asked

Viewed 381 times

-4

I have a variable inside a code php with a while loop and need to modify this variable $codigo inside the javascript.

This variable $codigo receives a different number for each repeat loop

data-toggle='popover1', data-toggle='popover2' ...

And this value needs to be increased also to javascript

 $('[data-toggle='popover1']'), $('[data-toggle='popover2']') ...

Follows the code:

$(document).ready(function(){
$('[data-toggle="popover<?php echo $codigo; ?>"]').popover({html: true});   
});
<?php
$codigo = 1;
while ($codigo < 10) {
$codigo = $i;
$i++;
?>

<div class="container">
<button type="button" class="btn btn-primary" data-toggle='modal' data-target='#Alt'>MODAL</button>
</div>


<!-- Modal -->
<div class="modal fade" id="Alt" role="dialog">
<div class="modal-dialog">

<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
</div>
<div class="modal-body">
<p><button id='id_codigo'
type="button" class="btn btn-primary" 
data-position="top-left" title="Contato" 
data-toggle="popover<?php echo $codigo; ?>" 
data-placement="down" data-content="Nome: id='id_nome'
<br>Endereço: id='id_endereco'
<br>Contato: id='id_contato' ">Contato</button></p>
</div>
</div>

</div>
</div>

<?php
}
?>

Follow the code online

  • Explains right which problem is facing, what you want to result and what outcome is currently happening.

  • I’ll edit my question

  • In reality as this variable is inside a loop of repetition and receives an increment every repetition, also need this increment in javascript, IE, I need to always pass the value of the php variable to the java script inside this loop of repetition.

  • Can’t do what you want, manipulate with the Javascript variables hosted in PHP. There is a layer distinction between client(html) and server(php). It is a cycle the user requests a page. The server starts a code on php and when end all php activities the server returns to the client an html page and ceases its activity. The browser receives the page and renders it, depending on the actions of the user a new request is sent to the server. It is a.

1 answer

1


By following your model you can do something like this:

<?php
$i = 1;
while ($i < 10) {
    $codigo = $i;
    $i++;
    ?>

    <button type='button' title='Sem Contato' data-toggle='popover<?php print $codigo ?>'>Sem Contato</button>

    <script>
    $(document).ready(function(){
        $("[data-toggle='popover<?php print $codigo ?>']").popover({html: true});   
    });
    </script>


    <?php
}
?>

What I did was add the same <?php print $codigo ?> from the above line. I suggest nesting this another way, for example: Print all buttons then repeat the loop by printing the separate JS.

  • In case to pass the value of the variable pro javascript is this way? Here it did not work

  • This does not pass the value to javascript, just "wrote" a javascript, if you look at the rendered source code you will see that it was data-toggle='popover1' data-toggle='popover2' etc. To pass a variable follows the same scheme in the variable declaration var variavelJS = <?php print $codigo ?>;. Put the need for the final result to understand what you want.

  • I really need to pass the value of the variable in php pro javascript.

  • So would it look like this? data-toggle='Load variableJS

  • Ready I edited my question and asked how I would like it to stay

  • Good morning Carlos, then, as @Augusto Vasques said, are "different layers", IE, you can not access a PHP variable in javascript or javascript in PHP. What you can do is write Javascript code using PHP. And this code that I gave you works for this, you checked the "rendered source code" I mean, in the browser? There is a detail there that is passing is also the repetition of single quotes, if you open single quotes should open double quotes internally at first and so sequentially, and vice versa.

  • 1

    In your "rendered code" it should look like this $("[data-toggle='popover1']") and not like this $('[data-toggle='popover1']'). Sorry I asked you that you don’t need an answer, are you a beginner in programming?! If it is, give an update on your javascript and php concepts, just re-read some books to fix/masterize what each one does, so you’ll spend a lot less time with small things. For example, it is taking you hours, if you read two books, one a day, you will stop spending several hours in small passages. That is, the old phrase of a few hours sharpening the axe.

  • That I am beginner. Thank you for the reply.

Show 3 more comments

Browser other questions tagged

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