4

Is it possible to grab a MyBB PHP variable with Javascript? I am an userscript coder, and right now I use:

var uid = $("#panel").find('a').attr('href').replace(/[^0-9]/g, '');

To grab the user id (uid) of the current user. However, if I were to grab the UID from the user with PHP, it would look like this:

<?php echo {$mybb->user['uid']} ?>

Now to the actual question. Is it possible to grab the UID through Javascript, using the $mybb->user['uid']?

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Lemvig Kiggeren
  • 317
  • 1
  • 3
  • 13
  • 1
    Not without access to the server. PHP is server-side so if you are writing a userscript that only runs in the client and has no access to the server-side PHP then you cannot use PHP variables. Your jQuery method is still probably the most effective way to do this given that you are writing a client side injected script. If you do have access to server-side code, you should use AJAX, or the setting JS variables by outputting JS code with php variables echoed into it. – Ennui Jan 06 '14 at 21:14
  • That's what I thought... Thanks though! – Lemvig Kiggeren Jan 06 '14 at 22:28

4 Answers4

2

My answer is Yes, you can but it has some limits.

Well, if you remember in every headerinclude templates always have these code:

<script type="text/javascript">
<!--
    var cookieDomain = "{$mybb->settings['cookiedomain']}";
    var cookiePath = "{$mybb->settings['cookiepath']}";
    var cookiePrefix = "{$mybb->settings['cookieprefix']}";
    var deleteevent_confirm = "{$lang->deleteevent_confirm}";
    var removeattach_confirm = "{$lang->removeattach_confirm}";
    var loading_text = '{$lang->ajax_loading}';
    var saving_changes = '{$lang->saving_changes}';
    var use_xmlhttprequest = "{$mybb->settings['use_xmlhttprequest']}";
    var my_post_key = "{$mybb->post_code}";
    var imagepath = "{$theme['imgdir']}";
// -->
</script>

You can use MyBB's variables in any template with one condition: it's must be defined in the correlative PHP file. (Of course that variable must be inside the brackets { })

For example, the correlative PHP file with headerinclude template is global.php.

That's it. Have fun with MyBB :)

Toan Nguyen
  • 911
  • 13
  • 35
  • When I do that, it will output the variable as a string. It will not work because they are defined as a string hence the quotation marks. – Lemvig Kiggeren Jan 15 '14 at 15:53
1

You can add a plugin to your forum. It is called template conditionals. Once you have that installed you can use the Mybb->user variable in all templates. There are many applications of this plugin, but for your code you can do:

<setvar uservar>$mybb->user['uid']</setvar>

var {$tplvars['uservar']} = $("#panel").find('a').attr('href').replace(/[^0-9]/g, '');

The plugin is on the MyBBHacks forum at the link below.

http://mybbhacks.zingaburga.com/showthread.php?tid=464

leefish
  • 46
  • 4
0

First, you will not be able to directly access a php server side variable in javascript, but there are several work arounds you could do.

The first one that comes to mind is to put this in your html page somewhere

<script type="text/javascript">
    <?php echo "var uid = '". $mybb->user['uid']."';" ?>;
</script>

The downside to this is that this variable is now directly view able and editable in the DOM.

mituw16
  • 5,126
  • 3
  • 23
  • 48
  • How is that going to work? Really. It's for an userscript, so I am going to inject it on the page somewhere. – Lemvig Kiggeren Jan 06 '14 at 19:37
  • What do you mean? If you write into a javascript block like that, you can access that variable in any following javascript. – mituw16 Jan 06 '14 at 21:05
0

If you are using (or can use) a template you may place a PHP variable in a JavaScript variable defined within that template's content. If you plan to run this JavaScript on every page you must add the variable definition to the script block in the headerinclude template.

<script type="text/javascript">
<!--
    var userID = {$mybb->user['uid']};
// -->
</script>

That example script will place the user's uid in the JavaScript variable userID.

Please note that <?php echo $mybb->user['uid']; ?> will not work as MyBB's template system does not allow PHP tags within it. You may instead use {$mybb->user['uid'} as an alternative.

Also, take note that if you are using a plugin and eval'ing the template in a function in your plugin file you will also need to globalise the PHP variable you are going to use.

Jordan
  • 26
  • 2