Solved

Passing a variable among different templates

Posted on 2003-11-11
8
340 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
ID: 9729057
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
ID: 9729921
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
ID: 9745915
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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:yrs7
ID: 9746024
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
 
LVL 17

Expert Comment

by:anandkp
ID: 9746037
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
ID: 9746500
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
ID: 9756735
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
ID: 9757509
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

777 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