简体   繁体   English

将PHP变量传递到Jquery弹出窗口

[英]Pass PHP Variable into Jquery popup

I have the following setup: JQuery: 我有以下设置:JQuery:

$('.btn-setting').click(function(e){
        e.preventDefault();
        $('#myModal').modal('show');
    });

Which opens a HTML file as a Dialog box, the problem is that I cannot pass any PHP variables onto the page for reference: 它打开一个HTML文件作为对话框,问题是我无法将任何PHP变量传递到页面上进行引用:

<a href="<?= echo $UserID; ?>" class="btn btn-info btn-setting">Click for dialog</a>

Anyone suggest a method to enable me to do so? 有人建议一种使我能够这样做的方法吗?


Alright. 好的。 The problem is now, i'm viewing the source of the page.. Yet, it's showing what i'm expecting, now When I click the button to bring up the dialog box, it's showing different data... 现在的问题是,我正在查看页面的源。。但是,它显示的是我的期望,现在,当我单击按钮调出对话框时,它显示的是不同的数据...

Not recommended I know, but heres a live: 不推荐,我知道,但是这里有直播:

92.236.219.136/Admin/ 92.236.219.136/管理员/

User: Stack 用户:堆栈

Pass: stack 通过:堆叠

This is the most robust way, especially if you want to pass more than just one string: 这是最可靠的方法,尤其是如果您要传递多个字符串时,尤其如此:

<?php

// Call this function to pass an object to the JS code
function php_vars_to_js($id, $obj) {
  echo "<script id='$id' type='text/php_data'>";
  echo htmlspecialchars( json_encode( obj ) );
  echo '</script>';
}

// for examlpe
php_vars_to_js( "php_vars", array("a_string" => "foo", "a_num" => 12 ) );

And then read it this way: 然后以这种方式阅读:

var php_vars = $.jsonDecode( $("php_vars").text() );

You can get the passed href in jQuery like this: 您可以像这样在jQuery中获取传递的href

$('.btn-setting').click(function(e){
    e.preventDefault();
    var passedValue = $(this).attr('href');
    // passedValue contains your <?=$UserID?> from the link
    // ...
    $('#myModal').modal('show');
});
<a href="<?php echo $UserID; ?>" class="btn btn-info btn-setting">Click for dialog</a>

你在回声回声

You can use tha data attribute, like this: 您可以使用tha数据属性,如下所示:

<a href="<?php echo $UserID; ?>" data-variable1="<?php echo $UserID; ?>" class="btn btn-info btn-setting">Click for dialog</a>

And in your JS: 在您的JS中:

$('.btn-setting').click(function(e){
        e.preventDefault();
        var1 = $(this).attr("data-variable1"); // Here is your PHP Variable now.. you can handle it to your Modal etc..
        $('#myModal').modal('show');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM