Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

Form to run sub/function in plugin

Quote Reply
Form to run sub/function in plugin
How could I get an html form I have in one of my plugin subs to run another function withing the same plugin module? (I dont want to run an external cgi program to handle the form data)

sub blah {

print qq~ <form action="run my function with the values I pass it here"> ~;

}



sub myfunction {

$a = shift;
$b = shift; <values passed to my function from form

rest of function

}

Edit: Is the way to solve this with $IN->param('formfield_name') somehow?


http://www.iuni.com/...tware/web/index.html
Links Plugins

Last edited by:

Ian: Jun 10, 2002, 7:40 PM
Quote Reply
Re: [Ian] Form to run sub/function in plugin In reply to
If I must do this by seperate cgi files (I prefer not to), then is this sort of how its done? ( I have simplified for the purposes of this question)

In my form:

<form action="thefile.cgi" method="POST">
<input name="input1">
<input name ="input2">
<input type="submit" value="Submit" name="B1"></form>

In my cgi:

#!/usr/bin/perl

print "Content-type:text/html\n\n";

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}

print "<html><head><title>Form Output</title></head><body>";
print "<h2>Results from FORM post</h2>\n";

foreach $key (keys(%FORM)) {
print "$key = $FORM{$key}<br>";
}

print "</body></html>";



What do you think????


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Form to run sub/function in plugin In reply to
Two remarks:

1) To produce html output from your plugin module, use GT::Template to parse templates (see docs for how it works). You can then ship the templates with your plugin, and allow people to customize them easily.

2) If you want to call a sub through a form, you have to include a separate cgi script. The script only serves as a window to the world of your plugin package. Look at page.cgi for an example of how you can implement this. If you do it like this, also calling LInks::init etc, then you will be able to use $IN, which will contain all your form input.

Hope that helps.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Form to run sub/function in plugin In reply to
Yes, that does help, thanks Ivan.

Regarding your point 1... I am working on this... I do have one user template so far. The form in question though, isl an admin form, so it is one that I don't want the user to customize.

It looks like have cgi is the way to go for processing my admin functions (which use forms at least) then. Hmmm... I have a lot of work to do.


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [yogi] Form to run sub/function in plugin In reply to
One more question on the topic:

When I am writing a cgi to be included with a plug-in for other people's computers, how do you deal with the path information at the top of the cgi? Does the user have to go in and modify these before they can use your plug-in or is there a way for specifying this in a more automatic way?

use lib 'c:/path/to/your/cgi-bin/lsql/admin';



Thanks.


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Form to run sub/function in plugin In reply to
There is an automatic way. When installing, you basically read the file, do some substitutions (like the lib path, also the path to perl), and then write the file to the appropriate directory. I suggest you have a look at Paul's MyLinks_DB plugin, to see how it's done.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Form to run sub/function in plugin In reply to
Good idea, I hope he doesn't mindSmile

Thanks again Ivan!

(almost bed time here anyways)


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Form to run sub/function in plugin In reply to
No :) (I don't mind)
Quote Reply
Re: [Paul] Form to run sub/function in plugin In reply to
Thanks, Paul... I was going to Private Message you this morning about this.... it was pretty late here when I was having this conversation with Ivan.

Thanks!


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Form to run sub/function in plugin In reply to
Hi,

If you want to run a function in the admin area, your form should look like:

<form action="admin.cgi" method="POST">
<input type="hidden" name="do" value="plugin">
<input type="hidden" name="plugin" value="YourPlugin">
<input type="hidden" name="func" value="function_name">
...
</form>

Then, when the admin submits this form, it will run Plugins::YourPlugin::function_name(). You can use $IN, $DB, etc, no need to do your own form parsing.

Remember, this is only for the admin (as it's calling admin.cgi which is password protected). If you want something like this for users, your best off using a template. Make your form:

<form action="admin.cgi" method="POST">
<input type="hidden" name="p" value="template_name">

This will parse and load template_name.html. You can then just put a function at the top of that template: <%Plugins::MyPlugin::function_name%> to run the function you need.

Hope this helps,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Form to run sub/function in plugin In reply to
Wow, Alex, you on fire this evening!

I can barely keep up cutting and pasting your solutions to my scap book of tools!! (not to mention reading 2.1.1 release notes)

Thanks very much again!Smile


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Alex] Form to run sub/function in plugin In reply to
I think in the second code fragment Alex meant page.cgi, not admin.cgi (admin.cgi is password protected).


PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [pugdog] Form to run sub/function in plugin In reply to
But won't this still work, as the plugin is run by the admin, under their logged in session???Crazy


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Form to run sub/function in plugin In reply to
Yes, it will:

Quote:


This will parse and load template_name.html. You can then just put a function at the top of that template: <%Plugins::MyPlugin::function_name%> to run the function you need.


this is loading the specific subroutine you need, by calling it directly. Just make sure your called subroutine sets up the environment it needs to runs.

This is one of my favorite features of the template parser :)

Check the docs on running functions from within the template parser.


PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [pugdog] Form to run sub/function in plugin In reply to
Quote:
This is one of my favorite features of the template parser :)


I am growing to like this too.

It seems extremely useful.


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Alex] Form to run sub/function in plugin In reply to
Hi Alex,

The first option here uses admin.cgi, and hence can be used only by pages for the admin, as you said.

The second option allows a user.

What about an editor using a custom form in browser_info.html? They are logged in.... but I am assuming that the form action still cannot use admin.cgi as a vehicle to send the information to a plug-in sub.

Would this be a correct assumption? Crazy


http://www.iuni.com/...tware/web/index.html
Links Plugins

Last edited by:

Ian: Jun 16, 2002, 1:27 PM
Quote Reply
Re: [Ian] Form to run sub/function in plugin In reply to
Hi,

Correct, editors can not go through admin.cgi (as if they try to submit anything to admin.cgi the admin password dialog will pop up).

You'll need to go through a template.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Form to run sub/function in plugin In reply to
Thanks Alex.

Just a note, I tried doing the "admin" version of this and I keep gettin an internal server error:

Example:

<form method="POST" action="$CFG->{admin_root_url}/admin.cgi" >
<input type="hidden" name="do" value="plugin">
<input type="hidden" name="plugin" value="ToolBox">
<input type="hidden" name="func" value="stripfrom">
blah blah
</form>


And in ToolBox.pm:


sub stripfrom {
blah blah
return;
}


I have run into problems running admin.cgi from a form, where the form fails, but a hyperlink to the same works. For example running the force repair (nph-build.cgi?do=repair&force=1) does exactly that... I cannot run it from a form (it does an all build instead), but a hyperlink works just fine.

WeirdCrazy


http://www.iuni.com/...tware/web/index.html
Links Plugins

Last edited by:

Ian: Jun 17, 2002, 10:48 AM
Quote Reply
Re: [Ian] Form to run sub/function in plugin In reply to
Hi,

Your function must print out headers, so start with:

print $IN->header;

and you should be ok.

As for FORM vs URL based, there shouldn't be any difference. Look for html errors in the form (most likely the values are not passed in properly).

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Form to run sub/function in plugin In reply to
Thats done it! Thanks AlexCool

>>Look for html errors in the form (most likely the values are not passed in properly).


Will do... I'll go back and check everything.


http://www.iuni.com/...tware/web/index.html
Links Plugins