|
|
Adding Email and Basic Validation to Form Next we will add all the WebBatch code to make this form excel! code=WebCmdData() If code!='' Then Goto processform webout('Content-type: text/html',2) webout('<html>',1) webout('<head>',1) webout('<title>SimpleForm</title>',1) webout('</head>',1) webout('<body>',1) Webout('<form action="/webcgi/webbatch.exe?SimpleForm.web+response" method="POST">',1) Webout('Name: <br>',1) Webout('<input type="text" name="yourname"><br>',1) Webout('<br>',1) Webout('Email: <br>',1) Webout('<input type="text" name="emailaddress"><br>',1) Webout('<br>',1) Webout('Comments: <br>',1) Webout('<textarea name="comments" rows="20" cols="60"></textarea><br><br>',1) Webout('<input type="submit" value="Submit">',1) Webout('</form>',1) webout('</body>',1) webout('</html>',1) Exit :ProcessForm WebOut('Content-type: text/html',2) ; Output Content Type WebOut('<html><head><title>Form Successfully Submitted</title></head><body><pre>',1); Usual HTML UserName=WebParamData('yourname') Email=WebParamData('emailaddress') Comments=WebParamData('comments') If UserName=="" then WebOut('<H1>Username is blank, please go back and complete and resubmit</H1>', 1) ; WebBatch Command Title exit endif If Email=="" then WebOut('<H1>Email is blank, please go back and complete and resubmit</H1>', 1) ; WebBatch Command Title exit endif If Comments=="" then WebOut('<H1>Comments is blank, please go back and complete and resubmit</H1>', 1) ; WebBatch Command Title exit endif AddExtender("WWPST44I.DLL") host="mailserver.darkchip.com" fromaddr=Email userid="" ; no username needed for simple SMTP password="" ; ditto above port="" tolist="john_smith@darkchip.com" cclist="" bcclist="" subject="Comments from %Username% on Webform" msg=Comments attachments="" flags="" kInit(host,fromaddr,userid,password,port) kDest(tolist,cclist,bcclist) kSendText(subject,msg,attachments,flags) WebOut('<H1>Thanks %UserName%</H1>', 1) ; WebBatch Command Title WebOut('<H1>Email to %Email% has been sent!</H1>', 1) ; WebBatch Command Title WebOut('</pre></body></html>',1) ; Usual HTML Exit Let's go through line by line section by section what this code means and how it works code=WebCmdData() If code!='' Then Goto processform This first line of code assigns the value of any parameters that were passed to the .web file. In the form code below on the action line, you will notice that when the submit button is pressed, the SimpleForm file calls itself again, but passes a variable called response to the .web file. What this allows us to do, is to use the same form to display to the user, accept the data and then to actually do the work, all in a single script. In this case, when the script is first run, Code will = "" and thus the simple emtpy fields will be displayed ready for people to fill out. When the form is complete and the submit button is pressed, the parameter "response" will get passed to the web script and the value of Code will equal "response" and hence program flow will immediately go to processing the form webout('Content-type: text/html',2) webout('<html>',1) webout('<head>',1) webout('<title>SimpleForm</title>',1) webout('</head>',1) webout('<body>',1) Webout('<form action="/webcgi/webbatch.exe?SimpleForm.web+response" method="POST">',1) Webout('Name: <br>',1) Webout('<input type="text" name="yourname"><br>',1) Webout('<br>',1) Webout('Email: <br>',1) Webout('<input type="text" name="emailaddress"><br>',1) Webout('<br>',1) Webout('Comments: <br>',1) Webout('<textarea name="comments" rows="20" cols="60"></textarea><br><br>',1) Webout('<input type="submit" value="Submit">',1) Webout('</form>',1) webout('</body>',1) webout('</html>',1) Exit Simply outputs the empty HTML content to the client and then exits the web script. When the submit button is pressed, the same web script is called again, but this time with a an appended parameter :ProcessForm WebOut('Content-type: text/html',2) ; Output Content Type WebOut('<html><head><title>Form Successfully Submitted</title></head><body><pre>',1) UserName=WebParamData('yourname') Email=WebParamData('emailaddress') Comments=WebParamData('comments') Because program flow has been transfered to this process section of the script, we next need to look at the variables of the user supplied data in the form. In the form, we named the fields : yourname, emailaddress and comments. All 3 of those fields have their data extracted by using the WebParamData function and who values are assigned to webbatch variables which we can use later on in our script If UserName=="" then WebOut('<H1>Username is blank, please go back and complete and resubmit</H1>', 1) ; exit endif If Email=="" then WebOut('<H1>Email is blank, please go back and complete and resubmit</H1>', 1) ; exit endif If Comments=="" then WebOut('<H1>Comments is blank, please go back and complete and resubmit</H1>', 1) ; exit endif Using standard validation checks, now that we have the contents of the fields from the form assigned to webbatch variables, we can now do very interesting things with them. One of the more common things we can and should do, is basic data input validation. In this case, we are looking inside each of the variables to see if they are blank (ie. the user did not supplied all the required input on the form). In the case of one of the fields being blank, we display an error message to the user, explaining which field is blank and ask to go back and fill in the missing data. When then exit the script and the webbatch program flow terminates AddExtender("WWPST44I.DLL") This is one of my favorite parts :) Part of the power of WebBatch is that it is modular. The creators have people to extend what they can do with WebBatch, by offering "extenders". I encourage you to visit their site often as they have many extenders to allow people to do almost anything. The above line is the syntax for how to call the extender which allows WebBatch to use email functions. Once invoked, a whole suite of Email WebBatch functions are available to use immediately host="mailserver.darkchip.com" fromaddr=Email userid="" ; no username needed for simple SMTP password="" ; ditto above port="" tolist="john_smith@darkchip.com" cclist="" bcclist="" subject="Comments from %Username% on Webform" msg=Comments attachments="" flags="" The above variables will be used by the Email WebBatch functions, and so, I am prepopulating them with data to be later used by the functions. Please note 3 very interesting variable assignments. 1) fromaddr=Email -> What this does is assign the fromaddr variable the value supplied by the user for the email field. 2)subject="Comments from %Username% on Webform" -> This assigns the subject variable a value and within the value, we use the data supplied by our user to include their UserName data they inputted into the form earlier. Note that because the Username variable is within a string with "", we need to enclose the variable name with a pair of %. What this does is to extract the value of he variable Username first, and then include that value inside of the string 3)msg=Comments -> this assigns the msg variable the input our user supplied on the form, in the comments section kInit(host,fromaddr,userid,password,port) kDest(tolist,cclist,bcclist) kSendText(subject,msg,attachments,flags) I encourage you to review the WebBatch syntax located in the help fies, but basically what these 3 lines are responsible for, are the actual sending of the email. The first 2 functions (kinit, and kdest) are responsisble for setting the email up with respect to what mail server you will be using to send this email, what tcp port you will be using, who id the email going to be delivered to,etc. The last function, ksendtext, is the actual function that takes this information and sends the email out WebOut('<H1>Thanks %UserName%</H1>', 1) ; WebBatch Command Title WebOut('<H1>Email to %Email% has been sent!</H1>', 1) ; WebBatch Command Title A simple confirmation note is displayed to the user once the email function has completed, using their Username and email address in the message. Note again the use of the % symbol to allow the usage of the variable contents within strings WebOut('</pre></body></html>',1) ; Usual HTML Exit That is basically it for this example. I chose a simple example with a simpe form structure, however you can make it as fancy as you wish. I hope that now that I have described the how and the what around enabling email and forms to work together, you have a better understanding on how to make use of this power yourself. |
Copyright by darkchip.com |