How to test for group membership
3 posters
Page 1 of 1
How to test for group membership
On a custom HTML page, is there code I can use to test if a user is in a group?
I would like to have a button that, when clicked, will take the user to one of two places depending on whether they are in a certain group or not.
Is this possible?
Thanks!
I would like to have a button that, when clicked, will take the user to one of two places depending on whether they are in a certain group or not.
Is this possible?
Thanks!
Re: How to test for group membership
Well anything is possible, it depends exactly what you want to accomplish. It could be as simple as adding a sinker (link) to the more advanced stuff like Ajax (which i have 0 knowledge of) and we could have Ange or someone help ya with that.
May you go into further detail what you want? What are the 2 links you want them to go to? Which groups would you like to see what?
-Brandon
May you go into further detail what you want? What are the 2 links you want them to go to? Which groups would you like to see what?
-Brandon
Remember to mark your topic when a solution is found.
General Rules | Tips & Tricks | FAQ | Forgot Founder Password?
Team Leader
Review Section Rules | Request A Review | Sticker Points
Re: How to test for group membership
So I want to have a button that is essentially a link to a page. If the user is in group ABC it will take them to page X. If the user is NOT in group ABC, it will take them to page Y.
Does that help?
Does that help?
Re: How to test for group membership
What are the links to the pages? Also may you include any cuurent html you have?
-Brandon
-Brandon
Remember to mark your topic when a solution is found.
General Rules | Tips & Tricks | FAQ | Forgot Founder Password?
Team Leader
Review Section Rules | Request A Review | Sticker Points
Re: How to test for group membership
I mean the links would just be to any two pages that doesn't really matter too much. My only question is how do I specifically test group membership, aka how do I write code to test if the user is a member of group ABC (simply to return a true or false)
Re: How to test for group membership
Well wouldnt you know who is a member of which group on the forum?
Yeah the html should be the easy part... However i am still a tad confused on what exactly what you want to accomplish?
Yeah the html should be the easy part... However i am still a tad confused on what exactly what you want to accomplish?
Remember to mark your topic when a solution is found.
General Rules | Tips & Tricks | FAQ | Forgot Founder Password?
Team Leader
Review Section Rules | Request A Review | Sticker Points
Re: How to test for group membership
I don't know how I can explain it differently... My question is that I need help writing a piece of code that will return true or false if a user is in a group.
So it might look like this:
If (userIsInGroupModerator)
{
Do this
} else {
or this
}
So it might look like this:
If (userIsInGroupModerator)
{
Do this
} else {
or this
}
Re: How to test for group membership
Yeah this is going to require a little more then html, probably will require atleast Javascript. I can help with the html parts, but if @Ange Tuteur can help with the javascript, it would be greatly apperciated as my skill level with code is not that high up yet .
-Brandon
-Brandon
Remember to mark your topic when a solution is found.
General Rules | Tips & Tricks | FAQ | Forgot Founder Password?
Team Leader
Review Section Rules | Request A Review | Sticker Points
Re: How to test for group membership
Hi @ian2000,
You can send an AJAX request to the /groups page, then manipulate the data as you need. Here's a quick example :
You'll see I'm querying the selector a[href*="/g1-"] in the returned data accessiable by the argument d ( short for data ). g1 would be the ID of the group I'm looking for, and as long as you don't have widgets on the side that link to groups, this method should work fine.
You can send an AJAX request to the /groups page, then manipulate the data as you need. Here's a quick example :
- Code:
<script type="text/javascript">// <![CDATA[
(function($) {
$.get('/groups', function(d) {
if ($('a[href*="/g1-"]', d)[0]) alert('ADMIN');
else if ($('a[href*="/g2-"]', d)[0]) alert('MOD');
else alert('MEMBER');
});
})(jQuery);
// ]]></script>
You'll see I'm querying the selector a[href*="/g1-"] in the returned data accessiable by the argument d ( short for data ). g1 would be the ID of the group I'm looking for, and as long as you don't have widgets on the side that link to groups, this method should work fine.
Re: How to test for group membership
This is super helpful @Ange Tuteur! Exactly what I needed help with! Your code works perfectly.
However I'm having trouble understanding something. I have some a piece in my HTML:
And I slightly modified your code, only replacing one of your alerts with:
And the result is:
"MOD" and "MEMBER" are still alerted as normal, though so thanks for that!
However I'm having trouble understanding something. I have some a piece in my HTML:
- Code:
<p>I want <b id="test">THIS</b> to change</p>
And I slightly modified your code, only replacing one of your alerts with:
- Code:
document.getElementById('test').innerHTML = "ABC"
And the result is:
- Code:
<script type="text/javascript">// <![CDATA[
(function($) {
$.get('/groups', function(d) {
if ($('a[href*="/g1-"]', d)[0]) document.getElementById('test').innerHTML = "ABC";
else if ($('a[href*="/g2-"]', d)[0]) alert('MOD');
else alert('MEMBER');
});
})(jQuery);
// ]]></script>
"MOD" and "MEMBER" are still alerted as normal, though so thanks for that!
Last edited by ian2000 on May 15th 2015, 9:58 am; edited 1 time in total
Re: How to test for group membership
Where did you install it? In the html page?
Remember to mark your topic when a solution is found.
General Rules | Tips & Tricks | FAQ | Forgot Founder Password?
Team Leader
Review Section Rules | Request A Review | Sticker Points
Re: How to test for group membership
It was placed in a small HTML page and the code that is relevant is in my post
Thanks
Thanks
Re: How to test for group membership
Did you try Ange's code as is?
Remember to mark your topic when a solution is found.
General Rules | Tips & Tricks | FAQ | Forgot Founder Password?
Team Leader
Review Section Rules | Request A Review | Sticker Points
Re: How to test for group membership
It should still work, unless the link to /g1- is not displaying on the /groups page. Here's an example similar to what you're trying to achieve :
- Code:
<p>Your user-level is : <b id="test">undefined</b></p>
<script type="text/javascript">// <![CDATA[
(function($) {
var node = document.getElementById('test');
$.get('/groups', function(d) {
if ($('a[href*="/g1-"]', d)[0]) node.innerHTML = 'Administrator';
else if ($('a[href*="/g2-"]', d)[0]) node.innerHTML = 'Moderator';
else node.innerHTML = 'User';
});
})(jQuery);
// ]]></script>
Re: How to test for group membership
This code works wonderfully! Thanks for your support, awesome as usual @AngeTuteur!
I learned so much from this example.
I learned so much from this example.
Similar topics
» Remove Membership Forum/Group
» Group Test by Ange Tuteur
» display the current number of members in each group along with the images of the Group Moderator
» Group design, group description pop up and add gif in the groups that can see in the main menu page
» I add a user into a group. but he didnt receive any email notification about been invited into group..
» Group Test by Ange Tuteur
» display the current number of members in each group along with the images of the Group Moderator
» Group design, group description pop up and add gif in the groups that can see in the main menu page
» I add a user into a group. but he didnt receive any email notification about been invited into group..
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum