News:

Wondering if this will always be free?  See why free is better.

Main Menu

Passing $context data to custom action page

Started by Andy00, December 30, 2013, 07:02:41 AM

Previous topic - Next topic

Andy00

Hi all, I've followed the wiki to create a custom action, http://wiki.simplemachines.org/smf/Add_a_custom_action_using_integration_hooks, and almost everything worked fine. There's one thing I still can't understand: inside my custom action previous page, I add some data to $context array but in my page those data are not available.
What could be the problem?
I use SMF 2.0.6 and the navigation to my custom page is done trough a link.

Thanks in advance.

Sorck

Have you remembered to put global $context; at the start of your function? (Just after the opening curly brace).

Andy00

Yes, it's the first line both in my function and template. I made a print_r in my page and I see $context content, but my data are not there.
The page I move from is Display.template.php, I modified also Display.php adding custom data to $context. Core parameters are present but not mine.

margarett

Yes, that's how it really works. From Display you call your custom action, but that restarts from index, thus cleaning what you might have set up in context.

You need to use superglobals for that, methinks.
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

emanuele



Take a peek at what I'm doing! ;D




Hai bisogno di supporto in Italiano?

Aiutateci ad aiutarvi: spiegate bene il vostro problema: no, "non funziona" non è una spiegazione!!
1) Cosa fai,
2) cosa ti aspetti,
3) cosa ottieni.

Andy00

Well, I know superglobals but i though there was a way to use $context instead.
Anyway, the code is very simple, I added this line in Display.php


....
// The central part of the board - topic display.
function Display()
{
global $scripturl, $txt, $modSettings, $context, $settings;
global $options, $sourcedir, $user_info, $board_info, $topic, $board;
global $attachments, $messages_request, $topicinfo, $language, $smcFunc;

        // My line  ******************
$context['myCustomData'] = 'myCustomData';

// What are you gonna display if these are empty?!
if (empty($topic))
fatal_lang_error('no_board', false);
....



In Display.template.php I added this link


<a href="' ,
$scripturl , '?action=myAction;sesc=' , $context['session_id'] , '">Click here</a>



Finally, inside myAction.template.php, I wrote


echo '<h3>' , $context['myCustomData'], '</h3>';



....and nothing appears.

margarett

Yes, it is working correctly.

Display.php fills your variable, then the template is loaded and your variable is available inside it.

When you load your custom action, there is no connection to Display, except the fact you come from there (but you can come from any other place...)
So $context is initialized in index.php but it doesn't go through Display.php to get to your custom action and, therefore, your content is not loaded.
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Andy00

I see, so in this case I think I should use globalvars or query string and not modify index.php (right?).

margarett

It depends on how you really call your custom action. If it comes from a form, the best way is to submit the variable and get it in $_POST. If not, maybe you can pass it in URL: index.php?action=youraction;variable=value (this, of course, makes it visible)

Or you can use the database for that -> set a setting in Display.php and read/reset it in your custom action.
You can also use cache or SMF cookie. And this is what I can think of, maybe there are other methods ;)
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Arantor

Why do you need the session id anyway...? What information are you trying to pass from a topic to your custom action?
Holder of controversial views, all of which my own.


Andy00

Quote from: margarett on December 30, 2013, 10:49:11 AM
It depends on how you really call your custom action. If it comes from a form, the best way is to submit the variable and get it in $_POST. If not, maybe you can pass it in URL: index.php?action=youraction;variable=value (this, of course, makes it visible)
Yes, I think the POST would be better. I have my link inside the quickModForm form, whose action is quickmod2, so I have to use a little javascript to modify the action to mine one when the user click on my button.

Quote from: Arantor Beeblebrox the First on December 30, 2013, 10:56:33 AM
Why do you need the session id anyway...? What information are you trying to pass from a topic to your custom action?
I need it beacuse it's an action which modify "directly" the database, without going to another page. I read it here http://wiki.simplemachines.org/smf/Understanding_SMF_Security#When_do_you_need_session_checking.3F
I hope I've understood it well :)

Arantor

QuoteI have my link inside the quickModForm form, whose action is quickmod2, so I have to use a little javascript to modify the action to mine one when the user click on my button.

Are you using a *link* or a *button*? Your original code indicated a link which does not require this.

QuoteI need it beacuse it's an action which modify "directly" the database, without going to another page.

Yup. But I think you're confused about how you're trying to do whatever it is you're trying to do - but since I don't actually know *what it is* you're trying to do, I can't really help you. It always works so much better when people ask how to achieve an overall goal rather than fixating on a single specific bit that may or may not be pertinent in the first place.
Holder of controversial views, all of which my own.


Andy00

The overall goal is simply to pass some parameters from Display.template.php to my custom action. This action (the link or button) has to be visible only to registered users and modifies the database directly.

Now I use a link, built as specified before, I show it only if $context['user']['is_guest'] != 1

That's all :)

Arantor

What parameters from the display template, exactly?
Holder of controversial views, all of which my own.


Andy00

Thery are all custom parameters, taken from one of my table. I'd prefer to not show them in query string, that's why I was thinking about putting them in POST and change the action of the form. Will session check work if sesc is in POST?

Arantor

Holder of controversial views, all of which my own.


margarett

I'd say you are better passing the topic ID (since you are coming from Display) in the URL and then perform the database interactions in your custom action. Since you want to pass parameters that, if I understood correctly, are already in the database, you might as well get those in your action.
Quote from: Andy00 on December 31, 2013, 07:41:21 AM
Will session check work if sesc is in POST?
Yes, that's how posting (and many other user actions) works.
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Kays

For POST, "sc" should be used. "sesc" is used with GET. However, rather than either of those, $context['session_var'] is recommended instead. ;)

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

Andy00

Thanks to all, I'm still learning step by step how to make things work in the "right" way.

Quote
Since you want to pass parameters that, if I understood correctly, are already in the database, you might as well get those in your action.

I have those params in previous page, isn't always a good idea to save query numbers?

Kays

My question is: Are you using the data from that query on the display page?

If not, why run that query there at all? Then it's best done in the function for your custom page. Just pass the the data required like topic id to your custom page as suggested above.

If so, what sort of data is it? "Parameters" is rather vague.

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

Advertisement: