vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Accessing $vbulletin on a page called via AJAX (https://vborg.vbsupport.ru/showthread.php?t=272418)

Chunky Monkey 10-20-2011 11:35 AM

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!

kh99 10-20-2011 11:54 AM

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.

nerbert 10-20-2011 12:22 PM

Bad syntax. Try:

require_once('./global.php');
also put this in your code:
ini_set('display_errors', '1');
Then your syntax errors should show

kh99 10-20-2011 12:38 PM

Quote:

Originally Posted by nerbert (Post 2259309)
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.

nerbert 10-20-2011 01:03 PM

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

@Chunky Monkey, is the "Hello " coming through?

Chunky Monkey 10-20-2011 03:36 PM

Quote:

Originally Posted by nerbert (Post 2259309)
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


All times are GMT. The time now is 05:02 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01670 seconds
  • Memory Usage 1,726KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (2)bbcode_php_printable
  • (2)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (6)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.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
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete