Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 10-20-2011, 11:35 AM
Chunky Monkey Chunky Monkey is offline
 
Join Date: Oct 2008
Posts: 76
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Accessing $vbulletin on a page called via AJAX

Hello,

I want to add some ajax functionality to the UI that calls a custom php script and accesses $vbulletin and data managers to do different things depending on which button a user pushes from the UI.

The problem I'm having is that $vbulletin and other vBulletin related variables are not in scope on the php page the AJAX request hits, even when I include global.php on it.

Can anyone tell me what I would have to do to give php pages accessed via AJAX requests access to $vbulletin, etc.?

For starters, I would like the file, say myAjaxCalls.php, to do something as simple as this when the JavaScript hits it via AJAX and displays what the PHP page outputs:

PHP Code:
<?php
echo 'Hello ' $vbulletin->userinfo['username'];
?>
$vbulletin does not appear to be in scope at the moment, even when I include global.php on the page so that myAjaxCalls.php looks like

PHP Code:
<?php
include 'global.php';
echo 
'Hello ' $vbulletin->userinfo['username'];
?>
Any help would be greatly appreciated!
Reply With Quote
  #2  
Old 10-20-2011, 11:54 AM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I'm not an expert on ajax or anything, but I don't see anything wrong with your code. It works with a regular (not ajax) request. You have your script in the same directory as global.php, right?

Does an ajax request automatically include cookies? You'd need either the cookies or the security token for vb to recognize you as logged in. Maybe try using something like the FireFox addon "Tamper Data" to see what's being passed back and forth when the request is made.
Reply With Quote
  #3  
Old 10-20-2011, 12:22 PM
nerbert nerbert is offline
 
Join Date: May 2008
Posts: 784
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Bad syntax. Try:

require_once('./global.php');
also put this in your code:
ini_set('display_errors', '1');
Then your syntax errors should show
Reply With Quote
  #4  
Old 10-20-2011, 12:38 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by nerbert View Post
Bad syntax. Try:

require_once('./global.php');
also put this in your code:
ini_set('display_errors', '1');
Then your syntax errors should show
Good call - I agree, I forgot that by default the errors aren't displayed (I put display_errors = On in my php.ini long ago...Edit: on my test forum - you don't want to do that on your live forum). But just for the record, there's nothing wrong with the syntax in the OP.
Reply With Quote
  #5  
Old 10-20-2011, 01:03 PM
nerbert nerbert is offline
 
Join Date: May 2008
Posts: 784
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I always use require() or require_once(). I thought include and require had the same syntax.

@Chunky Monkey, is the "Hello " coming through?
Reply With Quote
  #6  
Old 10-20-2011, 03:36 PM
Chunky Monkey Chunky Monkey is offline
 
Join Date: Oct 2008
Posts: 76
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by nerbert View Post
Bad syntax. Try:

require_once('./global.php');
also put this in your code:
ini_set('display_errors', '1');
Then your syntax errors should show
It looks like the above did the trick, it is echoing my username now. Thank you, nerbert and kh99. :up:

In the past "include 'filename.php'" always seemed to work, so I'm not entirely sure why it doesn't here but this did.

Thanks again,
Chunky Monkey
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:30 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04798 seconds
  • Memory Usage 2,208KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (2)bbcode_php
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (6)post_thanks_box
  • (6)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (6)post_thanks_postbit_info
  • (6)postbit
  • (6)postbit_onlinestatus
  • (6)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete