Page 1 of 1

Form example using CXP

Posted: Thu Nov 13, 2014 5:30 pm
by rdonnay
I always struggled with validating data that was sent back in a form, but with CXP, this is so easy and elegant.

This is all handled in one CXP page.

An array is used to define all the items on the form.
The class DC_NoIvarContainer is used to dynamically create the form variables.
The commands DCFORM, DCTABLE, DCINPUT and DCHTML are used to render the HTML code.

Give it a try:
http://donnay-software.com/ds/form.cxp

FORM.CXP

Code: Select all

<%#Code locality="page-global"%>
<%
#include "dcdialog.ch"
#include "dccxp.ch"

FUNCTION RenderForm( aItems, lSubmitted )

LOCAL GetList := {}, oTable, i, oForm, cHtml, lValid := .t., cVarName, cValue

// Validate each form item
FOR i := 1 TO Len(aItems)
  cVarName := aItems[i,4]
  cValue := aItems[i,2]
  IF cVarName $ {'Name','Street','City','State','Zip','Email','Ccard','ExpDate','CvvCode' } ;
   .AND. Empty(cValue)
    aItems[i,5] := '***'
  ELSEIF cVarName = 'Email' .AND. !'@' $ cValue
    aItems[i,5] := '*** Invalid Email Address'
  ELSEIF cVarName = 'Ccard' .AND. Len(Trim(cValue)) < 15
    aItems[i,5] := '*** Card Number must be 15 or 16 digits'
  ELSEIF cVarName = 'ExpDate' .AND. Len(Trim(cValue)) < 5
    aItems[i,5] := '*** Expiration Date must be mm/yy'
  ELSEIF cVarName = 'CvvCode' .AND. Len(Trim(cValue)) < 3
    aItems[i,5] := '*** CvvCode must be 3 or more digits'
  ENDIF
  IF !Empty(aItems[i,5])
    lValid := .f.
  ENDIF
NEXT

IF lValid

  cHtml :=  '<h2>Thank you for visiting our website. Come again soon!</h2>'

  cHtml += '<pre>'
  FOR i := 1 TO Len(aItems)
    cHtml += aItems[i,1] + ': ' + aItems[i,2] + Chr(13)+Chr(10)
  NEXT
  cHtml += '</pre>'

ELSE

  DCFORM OBJECT oForm METHOD 'POST' ACTION './Form.Cxp?submit'

  DCTABLE OBJECT oTable ROWS Len(aItems)+4 COLUMNS 2 PARENT oForm BORDER 0

  FOR i := 1 TO Len(aItems)

    @ i,1 DCHTML aItems[i,1] PARENT oTable TDOPTION "align=right", "width=200"

    @ i,2 DCINPUT VALUE IIF(Empty(aItems[i,2]),nil,aItems[i,2]) ;
          VARNAME aItems[i,4] ;
          SIZE aItems[i,3] ;
          MAXLENGTH aItems[i,3] ;
          PARENT oTable

    @ i,2 DCHTML aItems[i,5] STYLE "color:red" PARENT oTable

  NEXT

  @ i, 2 DCHTML IIF(lSubmitted,'<em><b>Try Again!! Make the RED go away!!</b></em><br>','') + ;
                'Please fill in all fields that are marked with ***' ;
         PARENT oTable COLSPAN 2

  @ i + 2, 2 DCSUBMIT CAPTION 'Submit Form!' VARNAME 'Submit' PARENT oTable

  DCREAD HTML TO cHtml

ENDIF

RETURN cHtml

%>

<%#Code locality="page-render"%>

<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
   <link rel="stylesheet" type="text/css" href="dccxp.css" />
</head>

<body>

<%

IF PCount() == 0
  oForm := DC_NoIvarContainer():new()
ELSE
  oForm := ::httpRequest:form
ENDIF

aItems := { ;
 { 'Name',oForm:name,20,'Name',''}, ;
 { 'Street',oForm:street,30,'Street','' }, ;
 { 'City',oForm:city,30,'City','' }, ;
 { 'State',oForm:state,2,'State','' }, ;
 { 'Zip',oForm:zip,10,'Zip','' }, ;
 { 'Phone',oForm:phone,20,'Phone','' }, ;
 { 'Email',oForm:email,60,'Email','' }, ;
 { 'Credit Card Number',oForm:ccard,16,'Ccard','' }, ;
 { 'Expiration Date (mm/yy)',oForm:expdate,5,'ExpDate','' }, ;
 { 'CVV Code',oForm:cvvcode,4,'CvvCode','' } ;
 }

cHtml := RenderForm( aItems, PCount()>0 )

? cHtml

%>

</body>
</html>

Re: Form example using CXP

Posted: Tue Nov 25, 2014 10:56 am
by Zdeno Bielik
Hi Roger,

I just installed Xbase 2.0 and eXpress 1.9.260 and also successfully compiled CxpHttpServer from previous topic.
But if now I want try compile this sample, I still get error, because dccxp.ch is missing in my exp folder.
Please, can you post it here or send me it to eMail?
Or is this "new features" still "in development" and will be ready for us later?

Zdeno

Re: Form example using CXP

Posted: Tue Nov 25, 2014 11:05 am
by rdonnay
Or is this "new features" still "in development" and will be ready for us later?
Yes. this is a new feature that is in development, however you should still be able to write HTML code in an CXP page and run it.

Re: Form example using CXP

Posted: Fri Dec 26, 2014 7:09 am
by Zdeno Bielik
Hi Roger,

sometimes when I press Enter key instead of TAB key for skip to next field, any error occurs - see attached file.
I have two questions:
- if there is any error/problem in cxp/web app while it is executed/running,
is any actual error-log report automaticaly saved and sended to web-admin/user?
(did you already get automaticaly any info from CXP runtime about this problem which I currently reported too?)
and is log-file still over-writed or added to any error-log file?
- is this default behavior that attached info page with that all informations
and completly source code are displayed?
is it possible turn it off for web-user/customers?

Regards
Zdeno

Re: Form example using CXP

Posted: Sun Dec 28, 2014 12:25 pm
by rdonnay
I just corrected an error in my DC_Html*() code. I have been modifying it with new features and broke it.
I think it is fixed now. The samples runs ok for me.