将PHP变量传递给Jquery弹出窗口 [英] Pass PHP Variable into Jquery popup

查看:52
本文介绍了将PHP变量传递给Jquery弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下设置:
JQuery:

I have the following setup: JQuery:

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

这会打开一个HTML文件作为对话框,问题是我无法将任何PHP变量传递到该页面供参考:

Which opens a HTML file as a Dialog box, the problem is that I cannot pass any PHP variables onto the page for reference:

<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/Admin/

用户:堆栈

通过:堆栈


推荐答案

这是最强大的方式,特别是如果你想传递的不仅仅是一个字符串:

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 ) );

然后以这种方式阅读:

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

这篇关于将PHP变量传递给Jquery弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆