Solved

Passing a variable among different templates

Posted on 2003-11-11
8
336 Views
Last Modified: 2013-12-24
Hello!  I need to pass my exam_name from exam_name.cfm to create_exam.cfm and after clicking "Create Next Question" link, exam_name is passed in the same template, create_exam.cfm.  I don't know if using a session variable is a good choice for passing a variable from template to template.  Anyway, the session.ExamName is never passed after I click "Create Next Question" link, and I need help with that.  Please also let me know any better ways to pass exam_name among different templates and good web documents that I can learn about passing variables and setting variables.  
p.s. code like below is to test if the session variable is passed in the if statement:
<CFOUTPUT><H1>#session.ExamName# - session</H1></CFOUTPUT>

Here is my code for create_exam.cfm:
<form name="form1" action="create_exam.cfm" method="post">
<!--- use cfparam tag to define a value if x and exam_name are not defined --->
<cfparam name="url.x" default="">
<cfparam name="url.z" default="">
<cfparam name="form.exam_name" default="">

<CFAPPLICATION NAME="Name"
SESSIONMANAGEMENT="Yes"
SESSIONTIMEOUT="#CreateTimeSpan(0, 1, 20, 0)#">

<CFSET session.ExamName='#form.exam_name#'>
<CFOUTPUT><H1>#session.ExamName# Edit Page - session</H1></CFOUTPUT>

<!--- First let's insert this data into our database for safe keeping and later usage.
        (the data is submitted from exam_name.cfm) --->
<cfif form.exam_name is #session.ExamName#>
<CFOUTPUT><H1>#session.ExamName# - session</H1></CFOUTPUT>
<CFINSERT DATASOURCE="userLogin" TABLENAME="tblExams" FORMFIELDS="exam_name">

<cfelseif url.x is "create_next_question">
<CFOUTPUT><H1>#session.ExamName# - session</H1></CFOUTPUT>
<!---  insert question to db --->              
<cfquery name="qQuestionInsert" datasource="userLogin">
         INSERT INTO tblQuestions(exam_id, q_ans, parent_id, correct)
         VALUES('#tblExams.exam_id#', '#form.question#', '0', 'NO')
</cfquery>
<!---  insert answers to db --->
<cfquery name="qAnswerInsert" datasource="userLogin">
         INSERT INTO tblQuestions(exam_id, q_ans, parent_id, correct)
         VALUES('#tblExams.exam_id#', '#form.q_ans#', '1', #form.chbox#)
</cfquery>

<cfelseif url.z is "finish_create_current_exam">
<CFOUTPUT><H1>#session.ExamName# - session</H1></CFOUTPUT>
<!---  insert question to db --->              
<cfquery name="qQuestionInsert" datasource="userLogin">
         INSERT INTO tblQuestions(exam_id, q_ans, parent_id, correct)
         VALUES('#tblExams.exam_id#', '#form.question#', '0', 'NO')
</cfquery>
<!---  insert answers to db --->
<cfquery name="qAnswerInsert" datasource="userLogin">
         INSERT INTO tblQuestions(exam_id, q_ans, parent_id, correct)
         VALUES('#tblExams.exam_id#', '#form.q_ans#', '1', #form.chbox#)
</cfquery>
</cfif>

<!---  if instructor click "submit" from create_edit_exam page to submit an exam name or
         click "create_next_question" link --->
<cfif form.exam_name is #session.ExamName# OR url.x is "create_next_question">
<CFOUTPUT><H1>#session.ExamName# - session</H1></CFOUTPUT>
<h2>Welcome Instructor</h2>            
<p><h4>Enter Question below</h4>
<input type="text" name="question" value="" size=120>
</p>
<p>Weight <input type="text" name="weight" size="4"></p>
<h4>Input Answers and Check the Right Answers</h4>
<table cellpadding="3" cellspacing="3">
<tr>
      <th>#</th>
      <th>Answer</th>
    <th>Correct Answer</th>
</tr>
<cfoutput>
<cfloop index="i" from="1" to="10" step="1">
<cfset q_ans=i>
<cfset chbox=i>
<tr>
  <td>#i#.</td>
  <td><input type="text" name="q_ans" value="" size=120></td>
  <td><input type="checkbox" name="chbox" value="NO"><br></td>
</tr>
</cfloop>
</cfoutput>
</table>
<p><input type="RESET" VALUE="RESET"></p>&nbsp;
</cfif>
<p><a href="create_exam.cfm?x=#create_next_question#"></a></p>
<p><a href="help.cfm?y=#help#">Help</a></p>
<p><a href="create_exam.cfm?z=#finish_create_current_exam#">Finish Create Current Exam</a></p>
</form>

0
Comment
Question by:yrs7
  • 4
  • 2
  • 2
8 Comments
 
LVL 10

Expert Comment

by:Mause
Comment Utility
try this
<a href="#void()" onclick="document.form1.submit();"></a>

this will submit the form and every form value is submitted, now you don't need session variables for this anymore

Mause
0
 
LVL 17

Assisted Solution

by:anandkp
anandkp earned 75 total points
Comment Utility
I wld use Cookies !

Set :
<CFCOOKIE NAME="my_username"  VALUE="anandkp"><!--- use expires property if required ... read cfdocs for more info abt cookies  --->

Read :
<CFIF IsDefined("Cookie.my_username")>
    <CFOUTPUT>#Cookie.my_username#</CFOUTPUT>
</CFIF>

HTH

K'Rgds
Anand
0
 

Author Comment

by:yrs7
Comment Utility
Due to sentain if conditions required in my program, I cannot use Mause's code, but thanks anyway.  Could you give me more details to fix my program, anandkp?  Thanks.
Ying
0
 

Author Comment

by:yrs7
Comment Utility
What I meant in the last message is I need help to pass parameters in create_exam.cfm page when I click "Create Next Question" link in create_exam.cfm page using cookies.
Please help!
Ying
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 17

Expert Comment

by:anandkp
Comment Utility
yes - its very easy to implement cookies - ive already given u the example on how to use them - what u need to do is ... save the exam_name in a cookie [when the user logs in or submits a form  -as per ur flow]

once u have the exam name in cookie ... access it anywhere by using the above example & u shld be thru

let me know - incase u need help with code ...
so i can guide u thru with it

K'Rgds
Anand
0
 
LVL 10

Accepted Solution

by:
Mause earned 75 total points
Comment Utility
Thats an option to use cookies but I don't like them (I always try to avoid them).
So if you want to use session variables I think you should add cfide and cftoken to your link like this

<a href="create_exam.cfm?x=#create_next_question#&#SESSION.URLTOKEN#">

and offcourse all session variables must be set.

Mause
0
 

Author Comment

by:yrs7
Comment Utility
Hello!  Mause, I would like to learn how to use session variable from you in after I fix this program with Cookie since I already modify my code a couple days ago.  I will try your method later.  I hope you don't mind.   anandkp...I do need your help to guide me through the code.  I don't know how to fix the first and forth if statements.  Also, I still don't really know how to pass the cookie after I click "Create Next Question" link.  The following is the code I need help w/.  

<form name="form1" action="create_exam.cfm" method="post">
<cfparam name="url.x" default="">
<cfparam name="url.z" default="">
<cfparam name="form.exam_name" default="">
<cfset e_name=form.exam_name>

<CFCOOKIE NAME="my_examname"  VALUE="e_name" expires="never">

<CFIF IsDefined("Cookie.my_examname")>
    <CFOUTPUT>#Cookie.my_examname#</CFOUTPUT>
</CFIF>

<CFOUTPUT>
<H1>#form.exam_name# Edit Page - form</H1>
<H1>#Cookie.my_examname# - cookie name</H1>
<H1>#e_name# - cookie value</H1>
</CFOUTPUT>
<!--- &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&--->
<!--- First let's insert this data into our database for safe keeping and later usage.
        (the data is submitted from exam_name.cfm) --->
<cfif e_exam is #e_name#>
<CFINSERT DATASOURCE="userLogin" TABLENAME="tblExams" FORMFIELDS="exam_name">
<!--- &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&--->
<!--- if instructor click "create_next_question" link --->
<cfelseif url.x is "create_next_question">
<!---  insert question to db --->              
<cfquery name="qQuestionInsert" datasource="userLogin">
         INSERT INTO tblQuestions(exam_id, q_ans, parent_id, correct)
         VALUES('#tblExams.exam_id#', '#form.question#', '0', 'NO')
</cfquery>
<!---  insert answers to db --->
<cfquery name="qAnswerInsert" datasource="userLogin">
         INSERT INTO tblQuestions(exam_id, q_ans, parent_id, correct)
         VALUES('#tblExams.exam_id#', '#form.q_ans#', '1', #form.chbox#)
</cfquery>
<!--- &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&--->
<cfelseif url.z is "finish_create_current_exam">
<!---  insert question to db --->              
<cfquery name="qQuestionInsert" datasource="userLogin">
         INSERT INTO tblQuestions(exam_id, q_ans, parent_id, correct)
         VALUES('#tblExams.exam_id#', '#form.question#', '0', 'NO')
</cfquery>
<!---  insert answers to db --->
<cfquery name="qAnswerInsert" datasource="userLogin">
         INSERT INTO tblQuestions(exam_id, q_ans, parent_id, correct)
         VALUES('#tblExams.exam_id#', '#form.q_ans#', '1', #form.chbox#)
</cfquery>
<script>
      alert("Exam is created!");
    self.location="create_edit_exam.cfm";
</script>      
</cfif>
<!--- &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& --->
<!---  if instructor click "submit" from create_edit_exam page to submit an exam name or
         click "create_next_question" link --->
<cfif e_exam is #e_name# OR url.x is "create_next_question">
<h2>Welcome Instructor</h2>            
<p><h4>Enter Question below</h4>
<input type="text" name="question" value="" size=120>
</p>
<p>Weight <input type="text" name="weight" size="4"></p>
<h4>Input Answers and Check the Right Answers</h4>
<table cellpadding="3" cellspacing="3">
<tr>
      <th>#</th>
      <th>Answer</th>
    <th>Correct Answer</th>
</tr>
<cfoutput>
<cfloop index="i" from="1" to="10" step="1">
<cfset q_ans=i>
<cfset chbox=i>
<tr>
  <td>#i#.</td>
  <td><input type="text" name="q_ans" value="" size=120></td>
  <td><input type="checkbox" name="chbox" value="NO"><br></td>
</tr>
</cfloop>
</cfoutput>
</table>
<p><input type="RESET" VALUE="RESET"></p>&nbsp;
</cfif>
<p><a href="create_exam.cfm?x=#create_next_question#">Create Next Question</a></p>
<p><a href="help.cfm?y=#help#">Help</a></p>
<p><a href="create_exam.cfm?z=#finish_create_current_exam#">Finish Create Current Exam</a></p>
</form>

Thanks for your help!!
0
 

Author Comment

by:yrs7
Comment Utility
Hi, my cookie question is solved.  The main reason I couldn't pass the variables I need in my form is b/c of some HTML syntax mistake.  
Ying
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Script 12 114
Help Fix A 403 error 13 94
IIS url rewrite rule for web application 3 46
CFFILE upload help 98 108
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now