Strobelight
Strobelight

Reputation: 11

Pass Variables to Included PHP file inside a Joomla Article

I'm new to Joomla, but not a new programmer. I've written several applications in PHP that I want to include inside Joomla articles. Simple enough:

<?php include 'file.php'; ?>

The issue is that inside the PHP files I have a bunch of code gathering and creating variables that I need to POST and retrieve. I can get those POST variables inside the Article, but I can't pass them back to the included PHP file.

I've even coded the included PHP files to access the Joomla framework hoping to retrieve Joomla user id for example. This won't run inside the Article either and returns empty. However, if I run the PHP file on its own outside of the Article, I can access all POST data (obviously) and also the Joomla JFactory data. So it runs fine, until it's placed as an included file inside an Article.

The only way I've been able to pass something to the included PHP file is using $_GET url variables like this:

<?php include 'file.php?data=something'; ?>

However, this simply isn't practical as I have too many variables to pass like this. Normally, included PHP files run as part of the parent script and have access to all variables. How can I accomplish this in Joomla??

Much appreciated!

Upvotes: 1

Views: 1436

Answers (4)

Strobelight
Strobelight

Reputation: 11

I resolved this using an Extension called Sourcerer https://www.nonumber.nl/extensions/sourcerer

Variables can be passed without issue now.

Thanks for the input.

Upvotes: 0

jonasfh
jonasfh

Reputation: 4509

It is not recommended to include php into articles. Try instead one of these approaches:

  1. Load your code in the index.php - file in your template: /templates/yourtemplate/index.php. This file is called every time your page is called.
  2. Make a template override of the component where you want you external php file to be loaded. If this is in an article, you copy /components/com_content/views/article/tmpl/default.php (and possibly also other files there) to /templates/yourtemplate/html/com_content/article. Then add your include-statement here. (info) This file will be loaded each time you view a single article, but you can have further logic to only run it if (whatever)...
  3. Use Jobin Jose's approach if you want to load you php-file inside content in an article. (info)
  4. Some other approach writing a plugin
  5. ...or a component

I would say probably the easiest method is 2. (or 1.), but it all depends what you want to do.

Upvotes: 1

Jobin
Jobin

Reputation: 8272

Try this,

You have to create a simple module for your requirement, means what you are trying to achieve with included php file.

then place that module within the article with {loadposition module_position} then you will get all the POST variables to that article and also suppose user_id and all other joomla Factory can be accessed bcoz its a Joomla module.

for a quick tutorial about module creation can be found here.

hope it make sense.

Upvotes: 0

Bocho Todorov
Bocho Todorov

Reputation: 429

Why do not you try to create a new Joomla component and do whatever you want it it?

I am absolutely beginner in php/mysql but managed to do it with my page, following this instructions: http://www.inmotionhosting.com/support/edu/joomla-3/create-component/helloworld

When you create this component, whatever you write in its "default.php" file, is actually a blank HTML/PHP page, with your joomla design and some other Joomla-features.

Sorry for the amateur answer and terminology :)

Upvotes: -1

Related Questions