Passing php variable to modal window with click function

I have a click function that launches a modal window. Inside the modal window, I load modal_window.php. The click function is as follows:

$('a#testmodal').click(function(e){
<? $id = $_GET['id']; ?>
varid = <? echo $id; ?>;
        $.get('modal_window.php?id=' + varid, function(data){
modal.open({content: data});});
                e.preventDefault();
            });

And the link that I use to launch it is as follows:

<a id="testmodal" href="modal_2.php?id=5">Test</a>

The strange thing is when I click on the link for the first time nothing happens. However, when I click on it, the second time everything works as it should. The reason for this is because the jquery part of my code is run for the first time before the php variable is set to the $ id variable (the jquery section is executed and then the php section is executed). Then, when I click on the link a second time (the $ id variable in the click function is set at this point), everything works fine.

, : , php. - :

<a id="testmodal" href="" var id ="5">Test</a>
0
2

, $_GET, , "". id, .

, , :

HTML

<a id="testmodal" href="#" data-id ="5">Test</a>

Javascript

$('#testmodal').click(function(e){
    e.preventDefault();
    $.get('modal_window.php?id=' + $(this).data('id'), function(data){
        modal.open({content: data});}); 
});

, , , , :

$('.open-modal').click(function(e){
    ...
});
+1

PHP , (PHP ). ,

: JavaScript?.

0

Source: https://habr.com/ru/post/1568105/


All Articles