Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: Alex4108 on March 10, 2012, 08:08:31 PM

Title: Passing a message from Source to Template
Post by: Alex4108 on March 10, 2012, 08:08:31 PM
So I'm stuck... yet again coding with SMF.

I have an addition to the Forum Shop mod, that allows me to give credits to just one user.  The page is structured a bit like this:

<input fields>
<submit button to index.php?action=actions;step=2>

From what I understand, this is a bit how the SMF System will run when I click Submit

1. Go to Sources, and run the function for step=2
2. Load the Template that the Source calls in step=2 (In this case, being ShopAdmin2)

What I need to do, is pass a message from when the Source runs (if everything was okay, any errors, etc.) to the Template page. 

Originally, I thought maybe a $context['blah']['blahblah'] = "MESSAGE"; method would work, but the Template was not too thrilled with that.
I also can't really set $_GET or $_POST values, so those are out of the question.

So my question, is how should I pass a message from a Source to a Template?
Title: Re: Passing a message from Source to Template
Post by: live627 on March 10, 2012, 08:33:58 PM
Did you bring $context into scope?
Title: Re: Passing a message from Source to Template
Post by: Alex4108 on March 10, 2012, 09:30:18 PM
Quote from: live627 on March 10, 2012, 08:33:58 PM
Did you bring $context into scope?

You mean global $context;?

Yes
Title: Re: Passing a message from Source to Template
Post by: Suki on March 11, 2012, 09:47:26 AM
Why you can't set get or post vars?

step is a get var.

You also don't need a separate function, you can manipulate the form data in the same function by doing:

if (isset($_GET['step']) && $_GET['step'] == 2)
{
// code to validate/manipulate the form data, here you can set a custom message too

if (something_went_wrong)
$context['custom_error'] = $txt['my_custom_error'];

}

and on your template you simple call $context['custom_error']   wherever you want it to appear, you set $context['custom_error'] as null by default (inside the same function) so it only appears if an error occur when someone hit the save button


If you want to use  separate page, then you need to specify your own action int he form and create that action in de action array on index.php or create it via hooks:

<form action=', $scripturl ,'?action=myownaction