Validating Form Input ColdFusion Help

In this final section of the chapter, you will add one last piece of code to your home. cfm template. This code will determine whether the user has selected a radio button. If the user wants to score their quiz they must select one of the answer radio buttons. Otherwise they will be redirected to start the quiz over. ColdFusion offers a simple way to validate that a form field has been selected. All you need to do is add an additional hidden form field. This hidden form field has the following format:

<INPUT TYPE-‘hidden’ NAME-‘field_name_required’ VALUE-‘Message to the USE”>

The form field that you want to be required is the user_answer form field. Using the preceding format, you would then code your hidden form field as follows:

<INPUT TYPE-‘hidden’ ~E-‘user_answer_required’ VALUE-‘Please select an ianswer”>

When ColdFusion processes this template, it will make sure that the user _.answer field has been selected. If it hasn’t, ColdFusion will return the message you emtered in the VALUE attribute and prompt the user to go back and try again. Your final home efm template should reflect the changes indicated in Listing 3.7. After you have made your changes review the results in your browser

<!OocrvPE HTML PUBLIC ‘-//W3C//DTD HTML 4.0 Transitional//EN’>
<CFIF NOT ISDEFINED(‘question_id’»
<CFQUERY NAME-‘get_count’· DATASOURCE-‘quiz’ DBTYPE-‘OOBC’>
SELECT count(question_id) as total_questions
FROM questions
</CFQUERY>
</CFIF>
<CFQUERY NAME-‘get_question’ DATASOURCE-‘quiz’ DBTYPE-‘OOBC’>
SELECT.question_id,
question,
answer1,
answer2,
answer3,
answer4
FROM questions
WHERE question_id –
<CFIF ISDEFINED(‘question_id’»*question_id*
<CFELSE»RandRange(l, get_count.total_questions)*
</CFIF>
</CFQUERY>
<HTML>
<HEAD>
<TITLE>My Home Page(/TITLE>
</HEAD>
<BODY BGCOLOR-“FFFFFF’>
<DIV ALIGN-‘ center’>
. <HRXB>We 1come to My Home Page!</B><HR>
now() returns the current timestamp (date &time)
<B>Today’s date is:</B><BR>
<CFOUTPUT>

The following variable, ‘today’, displays the current date in the format: Tuesday September. 15, 1998

now() returns the current times tamp (date &time)

dddd – full day of week
mmmm – full name of month
d – day of month
yyyy – year in four digits
—>
<CFSET today – DateFormat(Now(), ‘dddd, mmmm d, yyyy’)
<I)itoday*</I)
</CFOUTPUT>
</DIV>
<!— The following is code that dynamically generates each quiz
question —)
<CFOUTPUT QUERY-‘get_question’>
<FORM ACTION-‘quiz_results.cfm’ METHOD-‘post’)
<P><HR><B)Your Question is:</B>
<BR><I>*question*</I><P>
<INPUT TYPE-‘hidden’ NAME-‘ today’ VALUE-“today”)
<INPUT TYPE-‘hidden’ NAME-‘question_id’
VALUE-‘*question_id*’>
<INPUT TYPE-‘radio’ NAME-‘user_answer’
VALUE-‘*answerl.’) ‘answerl.<BR>
<INPUT TYPE-‘radio’ NAME-‘user_answer’
VALUE-‘*answer2.’> .answer2*<BR>
<INPUT TYPE-‘radio’ NAME-‘user_answer’
VAlUE-‘*answer3*’) ‘answer3.<BR>
<INPUT TYPE-‘radio’ NAME-‘ user_answer ,
VALUE-‘*answer4.’) *answer4.<BR>
<INPUT TYPE-‘hidden’ NAME-‘user_answer_required’
VALUE-‘Please select an answer! ‘>
<P><INPUT TYPE-‘Submit’ VALUE-‘Score Questionl’>
<INPUT TYPE-‘Button” VALUE-‘Get New Question I ,
ONCLICK-‘location.href-‘home.cfm”>
</FORM>
</CFOUTPUT>
</BOOY>
</HTML>

Where Do We Go from Here?

You’ve accomplished quite a bit in this chapter. You’ve created your own database  driven Web application. You’re now able to access a database using ColdFusion and if you’ve never worked with databases or SQL that is quite a step. You’ve also used some intermediate features of ColdFusion such as generating random numbers and creating dynamic SQL. At this point, you ‘could start coding your own basic applications for deployment over the Web. As you have also probably realized by now, Cold- Fusion is an’easy language to work with and creating Web applications with ColdFusion isn’t all that difficult.

You should realize though that these first three chapters of the book are just the tip of the iceberg. The rest of the material in the book will be much more in depth

Posted on November 14, 2015 in Getting Data From a Database

Share the Story

Back to Top
Share This