Index  Up  <<  >>  


One-click Multiple Variables

MiniVend can set multiple variables with a single button or form control. You first define the variable set (or profile, as in search and order profiles) inside a scratch variable:

  [set Search by Category]
  mv_search_field=category
  mv_search_file=categories
  mv_todo=search
  [/set]

The special variable mv_click sets variables just as if they were put in on the form. It is controlled by a single button, as in:

    <INPUT TYPE="submit" NAME="mv_click" VALUE="Search by Category">

When the user clicks the submit button, all three variables will take on the values defined in the ``Search by Category'' scratch variable. You can set the scratch variable on the same form as the button is on -- in fact that is recommended for clarity.

The variable will not be carried from form to form, it must be set on the form being submitted.

The special variable mv_check sets variables for the form actions checkout, control, refresh, return, search, and submit. This function operates after all of the values are set from the form, including the ones set by mv_click, and can be used to condition input to search routines or orders.

The variable sets can contain and be generated by most MiniVend tags -- the profile is interpolated for MiniVend tags before being used. Careful of interpolation order, and don't use the [post] tag -- it will not work. Embedded Perl will work, and is recommended for most conditional operations within the profile.

Any setting of variables already containing a value will overwrite the variable, so to build sets of fields (as in mv_search_field and mv_return_fields) you must use comma separation or place the null character with a &#0; literal.

Here is a small example which will set the value of mv_nextpage to route the user to a special page if their search inputs are invalid:

    <FORM ...
    <INPUT TYPE=hidden NAME=mv_check VALUE="Invalid Input">
    ...
    </FORM>

    [set Invalid Input]
    [perl cgi]
    my $type        = $Safe->{cgi}->{mv_searchtype};
    my $spell_check = $Safe->{cgi}->{mv_spelling_errors};
    my $out = '';
    if($spell_check and $type eq 'text') {
        $out .= "mv_todo=return\n";
        $out .= "mv_nextpage=special/cannot_spell_check\n";
    }
    return $out;
    [/perl]
    [/set]
mv_subroutine
The special variable mv_subroutine defines a named subroutine that will be called if defined on the current form. (This variable can have been set by mv_click.)

It takes arguments based on the special form variables mv_argN, where N is an integer that corresponds to the argument number (starting at zero). Arguments can be of four types:

identifier
Identifies a form variable (on the current form) the value of which will be supplied as that argument.

HASH(identifier)
Identifies a variable that will be parsed as a series of key-value pairs and supplied as a hash *reference*.

ARRAY(identifier)
Identifies a variable that will be parsed as a series of values and supplied as an array *reference*.

LITERAL(string)
A string that will be passed unchanged.

CODE(routine)
Identifies a named Perl subroutine that will be called to produce the argument. The arguments are a null-separated list placed in a variable of the same name, which will be parsed as above for HASH, ARRAY, LITERAL, identifier, and CODE references. The return value (hash reference, scalar, etc.) depends on the subroutine. This is recursive.

SAFE(list)
Identifies additional arguments passed to the subroutine parser, as in [perl sub values]. It will not be placed in the argument list, so you should use mv_arg9999 or some such to pass it from the original form. Never necessary or desirable when calling a GlobalSub.

The return value for the subroutine is available in the fixed session variable return_value, accessible as [data session return_value].

An example would be a password-protected message. Here is a form:

  <FORM METHOD=POST ACTION="[process-target]">
  <INPUT TYPE=hidden NAME=mv_subroutine VALUE=disclose_secret>
  <INPUT TYPE=hidden NAME=mv_arg0     VALUE=message>
  <INPUT TYPE=hidden NAME=mv_arg1     VALUE=password>
  <INPUT TYPE=hidden NAME=mv_doit     VALUE=return>
  <INPUT TYPE=hidden NAME=mv_nextpage VALUE=secret>
  Select
  <SELECT NAME=message>
      <OPTION VALUE="secret"> Secret
      <OPTION VALUE="top_secret"> Top Secret
  </SELECT>
  Password <INPUT TYPE=password NAME=password VALUE="">
  <INPUT TYPE=submit VALUE="See a Secret">
  </FORM>

And a subroutine:

  [set disclose_secret]
  my($message, $password) = @_;
  my $out = '';
  if($message =~ /^secret/) {
      return "Password is wrong."
          unless $password eq 'foo';
      $out = "Software documentation might as well be a secret sometimes.";
  }
  elsif($message =~ /top_secret/) {
      return "Password is wrong."
          unless $password eq 'bar';
      $out = "You can do a lot with software if you read the documentation!";
  }
  return $out || 'No message like that';
  [/set]

And a results page secret.html (remember, set the destination for the return action with mv_nextpage):

    <HTML>
    <HEAD>
    <TITLE>Secret?</TITLE>
    </HEAD>
    [body 1]

    Secret: [data session return_value]
    </BODY>
    </HTML>


Index  Up  <<  >>