MC front end to PostgreSQL - lesson 4

Sadhunathan Nadesan sadhu at castandcrew.com
Mon Jan 27 20:40:02 EST 2003


| Perhaps if Pierre agrees I will post the actual stack on his ftp server,
| similar to what he has put there.  I will take a slightly different
| tact in describing things for the list, although the applications are
| very similar.


	And now, lesson 4.  I hope I numbered the other ones right. 

	Ok,  you have seen and understood the middle layer, cgi, the
	back end, data base, and now it's time for the front end, an
	MC client.  Welp, you already know all about that.  All you
	need is to see an example of transmitting data to the middle
	layer.

	I like the approach of putting all functions or subroutines in
	the main stack script in one place, and then keeping each
	control very simple, maybe only calling one routine.

	Again, my data structure was very simple, just a title field
	and a scrolling text field for contents (field names 'title'
	and 'contents'), and then a few buttons for next, previous,
	list, add, modify, search and delete.  

	Buttons
	--------

	Search
		Enter some pattern such as a single word in the title
		field and the data base returns all records with that
		pattern in either the title, or the contents field,
		and makes one card for each returned record, so you
		can wind through them with the next and previous
		buttons.

	List
		Makes a list behavior box of the contents field
		listing all titles in the data base.  You can scroll
		through them, highlight one, double click it, then
		that record is returned to you (the list clears).

	Add	
		Put a new title in the title field, any new contents
		you want in the contents field, and it is added to the
		data base.  Duplicate titles rejected.

	Modify
		Change the contents field and post to the data base to
		modify an existing record.

	Delete
		Delete the record matching this title field.



Code
----

As with Pierre's example, I didn't put the real server name, I replaced
that with localhost, so put your own server name there, and your own cgi
program name there too.  Pierre used 2 middle pieces, and html file that
was actually a cgi (to confuse hackers) which in turn invoked a stack.
So you had to use the IDE to edit the stack file.  I preferred to
combine these as one, and write it as a simple cgi script using the
mc language and the local (vi) Unix editor, which is the world's best
editor of course.  (grin).

So, that host name is the only thing you would need to change if you
were using my stack.

I'm sure you can write this prettier and more efficiently than I can but
here you go (main stack script):


----------------------cut here--------------------------
	

on AzzClear
  set the listBehavior of field "contents" to false
  go to card 1
  put the number of cards into cardnum
  if (cardnum > 1) then
    repeat with i = cardnum down to 2
      delete card i
    end repeat
  end if
  put empty into field "title"
  put empty into field "contents"
end AzzClear


on preOpenCard
  AzzClear
end preOpenCard



on AzzAdd
  replace "&" with "" in field "title"
  replace "=" with "" in field "title"
  replace "&" with "" in field "contents"
  replace "=" with "" in field "contents"
  
  put "action=add&" into azzdata
  put "title=" & field "title" & "&" after azzdata
  put "contents=" & field "contents" after azzdata
  
  set httpheaders to "Content-type: application/x-www-form-urlencoded" & return
  post urlEncode(azzdata) to url ("http://localhost/cgi-bin/azz.cgi")
  if (it contains "ERROR") then
    answer it
  else
    answer "Your addition was successful"
  end if
end AzzAdd


on AzzSearch
  set the listBehavior of field "contents" to false
  
  # put empty into sqldata
  replace "&" with "" in field "title"
  replace "=" with "" in field "title"
  
  put "action=search&" into azzdata
  put "title=" & field "title" after azzdata
  
  set httpheaders to "Content-type: application/x-www-form-urlencoded" & return
  post urlEncode(azzdata) to url ("http://localhost/cgi-bin/azz.cgi")
  put it into sqldata
  
  if (the number of characters in sqldata is zero) then
    answer "Sorry, no such address card" & cr
  else
    repeat for each line this_line in sqldata
      if (this_line contains "|") then //title line
        visual effect scroll left
        clone this card
        split this_line by "|"
        put this_line[1] into field "title"
        put this_line[2] into field "contents"
      else
        
        if (the length of this_line >  40) then
          repeat with i = 1 to the length of this_line
            put char i of this_line after field "contents"
            if (i mod  40 = 0) then put return after field "contents"
          end repeat
          
        else
          put cr & this_line after field "contents"
        end if
        
        
      end if
    end repeat
  end if
  
end AzzSearch


on AzzList
  AzzClear
  set the listBehavior of field "contents" to true
  put "action=list&" into azzdata
  set httpheaders to "Content-type: application/x-www-form-urlencoded" & return
  post urlEncode(azzdata) to url ("http://localhost/cgi-bin/azz.cgi")
  put it into sqldata
  
  if (the number of characters in sqldata is zero) then
    answer "Sorry, your data base is empty" & cr
  else
    
    put "In the contents field below is a list of all the card titles"  into field "title"
    put sqldata into field "contents"
    
  end if
end AzzList

on AzzModify
  replace "&" with "" in field "title"
  replace "=" with "" in field "title"
  replace "&" with "" in field "contents"
  replace "=" with "" in field "contents"
  
  put "action=modify&" into azzdata
  put "title=" & field "title" & "&" after azzdata
  put "contents=" & field "contents" after azzdata
  
  set httpheaders to "Content-type: application/x-www-form-urlencoded" & return
  post urlEncode(azzdata) to url ("http://localhost/cgi-bin/azz.cgi")
  if (it contains "UPDATE 1") then
    answer "Your update was successful"
    
  else
    put  "Sorry! Your update was NOT successful" & cr & cr after it
    answer it
  end if
end AzzModify


on AzzDelete
  put "action=delete&" into azzdata
  put "title=" & field "title" & "&" after azzdata
  put "contents=" & field "contents" after azzdata
  
  set httpheaders to "Content-type: application/x-www-form-urlencoded" & return
  post urlEncode(azzdata) to url ("http://localhost/cgi-bin/azz.cgi")
  if (it contains "DELETE 1") then
    answer "Your deletion was successful"
  else
    answer "Sorry! Your deletion was NOT successful"
  end if
end AzzDelete
----------------------cut here--------------------------


As mentioned several times, I'm not an expert, this is a work in
progress, error checking code is minimal to none, etc.  It's just an
example to get you going.

Hopefully, it was of some help.  No warantee for fitness of purpose.

Pleasant alohas,
Sadhu


ps, all suggestions for improvement welcome.



More information about the metacard mailing list