;;; form-to-database.mhtml: -*- Meta-HTML -*- 

;;;  Copyright (c) 1996 Brian J. Fox
;;;  Author: Brian J. Fox (bfox@ai.mit.edu) Fri Jul 19 09:10:09 1996.
<defmacro form-to-database>
  <set-var %attributes>
  <FORM METHOD="POST" ACTION="<get-var mhtml::current-doc>">
    %body
    <p>
    <INPUT TYPE="SUBMIT" NAME="ACTION"
	   VALUE="<or <get-var action-name> "Submit Form">">
    <INPUT TYPE="SUBMIT" NAME="ACTION" VALUE="Cancel">
  </FORM>

  <when <string-eq <get-var-once posted::action> Cancel>>
    <if <get-var-once cancel>
	<redirect <get-var cancel>>>
  </when>

  <when <string-eq <get-var-once posted::action>
		   <or <get-var action-name> "Submit Form">>>
    <when <not <get-var-once <get-var key>>>>
      <ptext>
	I'm sorry, but you must supply the field <b><get-var key></b>
	to continue, or click <b>Cancel</b> if you don't wish to
	submit your information.
      </ptext>
    </when>

    <when <get-var-once <get-var key>>>
      <with-open-database
       db <concat <get-var mhtml::include-prefix mhtml::relative-prefix>/
		  <or <get-var dbname> anonymous.db>>
       mode=write-create>
	<unset-var posted::action>
	<package-delete temp>
	<alist-to-package <package-to-alist posted> temp>
	<set-var x =
	  <database-save-package db <get-var-once temp::<get-var key>>
				 temp strip=t>>
      </with-open-database>
      <if <get-var redirect-to>
	  <redirect <get-var redirect-to>>>
    </when>
  </when>
</defmacro>
