• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 348
  • Last Modified:

Passing a variable among different templates

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
yrs7
Asked:
yrs7
  • 4
  • 2
  • 2
2 Solutions
 
MauseCommented:
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
 
anandkpCommented:
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
 
yrs7Author Commented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
yrs7Author Commented:
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
 
anandkpCommented:
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
 
MauseCommented:
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
 
yrs7Author Commented:
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
 
yrs7Author Commented:
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now