The Edit Survey page serves three main functions: to modify the survey information such as title and start/end dates; to provide an overview of associated questions and a link to modify them; and to take the survey offline, even when in the declared active timeframe. A parameter, passed from the link in Survey Manager, determines the proper survey record to use; our custom code redirects the user to the Survey Manager page if the parameter is missing, which is a far better solution than displaying an empty recordset. Because the Edit Survey page is also the conduit to modifying the survey's questions, the URL parameter, if found, is put into a session variable and used to filter the survey recordset. In addition to the survey recordset, a second recordset for the questions is created for this page. When the modifications to the survey record are complete, a standard Update Record server behavior stores the changes. In addition to updating the current survey record, the Edit Survey recipe describes how to delete a record. What makes this delete function different from others included in this book is that it also removes associated records in this case, orphaned questions. Step 1: Implement Edit Survey Design To handle the requirements for the Edit Survey page, the layout must accommodate both a form for displaying the survey fields and a content table for listing the questions. Create a page for the Edit Survey recipe, either from a template or from the File > New dialog. In the SurveyBuilder folder, locate the folder for your server model and open the Edit_Survey page. In the Content area of your page, create a header for the page a placeholder to hold both the form and the content table. From the Snippets panel, drag the Recipes > SurveyBuilder > Wireframes > Edit Survey Wireframe snippet into the Content editable region. In the form area, add a form with the following form elements: three text fields (for the survey title, start date, and end date), one text area (for the completion message), one checkbox to enable or disable the survey, and three buttons (to update or delete the survey or to add a new question). Place your cursor in the first row below the words EDIT SURVEY and insert the Recipes > SurveyBuilder > Forms > Edit Survey - Form snippet. In the Content area, create a table, 2 rows by 2 columns, to hold the questions. Place your cursor in the third row below the words EDIT SURVEY at the bottom of the content region and insert the Recipes > SurveyBuilder > ContentTables > Survey Questions - Content Table snippet [r5-6].
Figure r5-6. Save the page. Step 2: Adding Database Components Quite often in this book's recipes when custom code is added, it is inserted after a standard Dreamweaver server behavior has been applied. In this case, however, we need to include the custom code first because the server behavior relies on the code's results. If you've completed the Survey Manager recipe, you'll remember how a URL parameter (ID) is used to pass the SurveyID to various pages, including this one. Our custom code takes the URL parameter and puts it in a session variable that can be used when editing the associated questions. If the URL parameter is not found, the code redirects the user to the Survey Manager page to select a survey to work on. The user is also redirected to the Survey Manager page if the session has expired. In Code view, place the cursor at the top of the page, just after the DOCTYPE declaration, and press Enter (Return) to make a space. Don't worry about the exact placement of this code block; because of the way Dreamweaver inserts recordsets, we'll need to adjust its placement later in the recipe. Insert the following code: From the Snippets panel, open the Recipes > SurveyBuilder > Custom Code folder for your server model and insert the Set Session - SurveyID to Request ID snippet. | <% if (cStr(Request("ID"))<>"") then Session("SurveyID") = cStr(Request("ID")) end if if (cStr(Session("ID"))="") then Response.Redirect("survey_manager.asp") end if %> | | <% if (String(Request("ID"))!="undefined") Session("SurveyID") = String(Request("ID")); if (String(Session("ID"))=="undefined") Response.Redirect("survey_manager.asp"); %> | | <cfif isDefined("URL.ID"> <cfset Session.SurveyID = URL.ID> </cfif> <cfif not IsDefined("Session.ID")> <cflocation url="survey_manager.cfm"> </cfif> | | <?php session_start(); if (isset($_GET['ID'])) { $_SESSION['ID'] = $_GET['ID']; } else { header("Location: survey_manager.php"); } ?> | When you're done, save the page. Now we're ready to insert the first of two recordsets, both of which reference the session variable just set. From the Bindings panel, choose Add (+) and select Recordset (Query) from the list. In the simple Recordset dialog, give the recordset an appropriate name. Enter Survey in the Name field. Select the desired data source connection. Choose Recipes from the Connection (Data Source) list. Choose the needed recordset. From the Tables list, select Surveys (surveys in PHP). Leaving the Columns option set to All, set the four Filter list elements like this: Keep the Sort option set to None and click OK to close the dialog. The second recordset, which displays a list of the survey's questions, is a bit more complex. Records are needed from two data source tables: SurveyQuestions and QuestionTypes. To accomplish this, a SQL statement using an inner join is entered through the Advanced Recordset dialog. For ASP | As a preliminary step, from the Snippets panel, use the Copy Snippet command to copy the Recipes > Survey Builder > SQL > Edit Survey RS - ASP SQL Statement snippet to the Clipboard. |
From the Bindings panel, choose Add (+) and select Recordset (Query) from the list. In the Recordset dialog, choose Advanced. Enter an appropriate name for the recordset. Enter Questions in the Name field. Select the desired data source. Choose Recipes from the Connections list. In the SQL area, enter the following code: Paste the copied snippet into the SQL area by pressing Ctrl-V (Command-V). [View full width] SELECT * FROM SurveyQuestions INNER JOIN QuestionTypes ON SurveyQuestions.QuestionType = QuestionTypes.TypeID WHERE QuestionSurvey = IDParam
In the Variables section, choose Add (+) to create a new variable. Enter the name for the variable. In the Name column, enter IDParam. Enter a default value for the variable. In the Default Value column, enter 1. Enter a run-time value for the variable. In the Run-Time Value column, enter Session("SurveyID"). Click OK to close the dialog and insert the recordset. For ColdFusion | As a preliminary step, from the Snippets panel, use the Copy Snippet command to copy the Recipes > Survey Builder > SQL > Edit Survey RS - CF SQL Statement snippet to the Clipboard. |
From the Bindings panel, choose Add (+) and select Recordset (Query). Switch to the advanced view of the dialog and enter an appropriate name for the recordset. Enter Questions in the Name field. Choose your data source. Select Recipes from the Data Source list. Enter a username and password if necessary. In the SQL area, enter the following code: Paste the copied snippet into the SQL area by pressing Ctrl-V (Command-V) [View full width] SELECT * FROM SurveyQuestions INNER JOIN QuestionTypes ON SurveyQuestions.QuestionType = QuestionTypes.TypeID WHERE QuestionSurvey = #Session.SurveyID#
In the Page Parameter section, choose Add (+) and, in the Add Parameter dialog, make sure Session.SurveyID is chosen in the Name list. Enter 0 as the Default Value and click OK to close the dialog. Verify your entries in the Recordset dialog and click OK to close that dialog. For PHP | As a preliminary step, from the Snippets panel, use the Copy Snippet command to copy the Recipes > Survey Builder > SQL > Edit Survey RS - PHP SQL Statement snippet to the Clipboard. |
From the Bindings panel, choose Add (+) and select Recordset from the list. In the Recordset dialog, choose Advanced. Enter an appropriate name for the recordset. Enter Questions in the Name field. Select the desired data source. Choose Recipes from the Connections list. In the SQL area, enter the following code: Press Ctrl-V (Command-V) to paste the copied snippet into the SQL area. [View full width] SELECT * FROM surveyquestions INNER JOIN questiontypes ON surveyquestions.QuestionType = questiontypes.TypeID WHERE questionsurvey = IDParam
In the Variables section, choose Add (+) to create a new variable. Enter the name for the variable in the Add Variable dialog. In the Name field, enter IDParam. Enter a default value for the variable. In the Default Value field, enter 1. Enter a run-time value for the variable. In the Run-time Value field, enter $_SESSION['SurveyID'] and click OK to close the Add Variable dialog. Click OK again to close the Recordset dialog and insert the recordset. Step 3: Data Binding Process Now that we have our two recordsets ready to go, we'll bind the data to the various form and text elements. First, we'll attach data fields from the Survey recordset to the form elements. As with the Survey Manager page, PHP requires custom formatting code and a function for converting the date values into a usable format. From the Bindings panel, expand the Survey recordset. Place the data source fields onto the page in their respective areas: Drag SurveyName onto the SurveyTitle text field. ASP and ColdFusion users: Drag SurveyStartDate onto the SurveyStartDate text field. PHP users: Select the SurveyStartDate text field, switch to Code view, and then insert the Recipes > Survey Builder > CustomCode_PHP > Survey Manager - Reformat SurveyStartDate Column snippet into the value attribute. ASP and ColdFusion users: Drag SurveyEndDate onto the SurveyEndDate text field. PHP users: Select the SurveyEndDate text field, switch to Code view, and then insert the Recipes > Survey Builder > CustomCode_PHP > Survey Manager - Reformat SurveyEndDate Column snippet into the value attribute. Drag SurveyCompletedMessage onto the SurveyCompletionMessage text area. ColdFusion users should apply formatting to the two date form fields: Select the SurveyStartDate form field and, from the Format column of the selected entry in the Bindings panel, choose Date/Time > 1/17/00 or whichever date format you would prefer. Select the SurveyEndDate form field and, from the Format column of the selected entry in the Bindings panel, choose Date/Time > 1/17/00 or whichever date format you'd prefer. PHP users need to add another function to the page to reconvert the dates to their previous formats: In Code view, position your cursor at the end of the document and, from the Snippets panel, insert the Recipes > SurveyBuilder > CustomCode_PHP > Survey Manager - Reformat Date snippet. | [View full width] <?php function reformatDate($date) { return substr($date,5,2) . "/" . substr($date,8,2) . "/" . substr($date,0,4); } ?> | All that's left in the Survey recordset is to link the checkbox to a data field. Select the SurveyStatus checkbox next to the Live label. In the Property inspector, choose Dynamic to open the Dynamic CheckBox dialog. Select the lightning bolt next to the Check If field to open the Dynamic Data dialog. In the Dynamic Data dialog, select SurveyLive from the Survey recordset and click OK. In the Equal To field, enter True and click OK to close the Dynamic CheckBox dialog [r5-7].
Figure r5-7. One extra step is required for the ColdFusion and PHP server models for the survey section. A hidden field form element is needed to hold the session variable so that it can be used in the Update Record server behavior applied in the next step of the recipe. From the Forms tab of the Insert bar, choose Hidden Field. In the Property inspector, enter SurveyID in the Name field on the left side of the inspector. Enter the session variable data field as the hidden element's value: In the Property inspector, select the lightning bolt icon next to the Value field to open the Dynamic Data dialog. In the Dynamic Data dialog, expand the CFParam entry and choose SESSION.SurveyID. Click OK when you're done. Enter the following code directly into the Init Val field of the Property inspector: <?php echo $_SESSION['SurveyID']; ?> When you're done, save your page. Let's move on to binding data in the Survey Questions area. To display a list of questions for a particular form, we'll bind two data fields from the Questions recordset. From the Bindings panel, expand the Questions recordset. Place the data fields on the page in the appropriate places. Drag QuestionText onto the table cell beneath the label Survey Questions. Drag QuestionType (TypeName in PHP) onto the table cell beneath the label Question Type. Now we'll use one of the fields just inserted as a link for editing a particular question. Select the Questions.QuestionText dynamic text entry from the page. In the Property inspector, select the folder icon next to the Link field to open the Select File dialog. In the Select File dialog, choose Parameters. In the Parameters dialog, enter a variable name. Enter ID in the Name column. Enter a value for the new variable. In the Value column, select the lightning bolt symbol to open the Dynamic Data dialog. From there, choose QuestionsID from the Questions recordset and click OK to close the Dynamic Data dialog. Click OK to close the Parameters dialog. Choose a file to pass the parameter to. Select the edit_question file for your server model. Click OK to close the dialog and add the link. Finally, we'll apply a Repeat Region server behavior to the entire row. From the Question recordset, select the row that contains the dynamic text elements. Choose <tr> from the Tag Selector. From the Server Behaviors panel, choose Add (+) and select Repeat Region from the list. In the Repeat Region dialog, choose the desired recordset. Select Questions from the Recordset list. Select the number of records you want to show. Choose Show All Records. Click OK when you're done. Save the page. Step 4: Update the Modified Record The Update Record server behavior stores the modified values in the Survey recordset. After the record has been updated, the user is taken back to the Survey Manager page. For ASP From the Server Behaviors panel, choose Add (+) and select Update Record. In the Update Record dialog, select the desired data source connection. Choose Recipes from the Connection list. Choose the table that contains the data you are updating. From the Table to Update list, choose Surveys. Choose the recordset from which to get data source fields. From the Select Record From field, choose Survey. Set the primary key for the recordset. From the Unique Key Column list, choose SurveyID and make sure the Numeric option is selected. Select the file you would like to appear when the update is complete. For the After Updating, Go To field, select the survey_manager.asp page. Choose the form on the page from which to obtain the values. From the Get Values From list, choose EditSurvey. In the Form Elements area, set the form elements to their corresponding data source fields. Set the SurveyTitle form element to update the SurveyName data source field as Text. Set the SurveyStartDate form element to update the SurveyStartDate data source field as Date MS Access. Set the SurveyEndDate form element to update the SurveyEndDate data source field as Date MS Access. Set the SurveyCompletionMessage form element to update the SurveyCompletedMessage data source field as Text. Set the SurveyStatus form element to update the SurveyLive data source field as Checkbox MS Access. Make sure your entries are correct and then click OK to close. For ColdFusion and PHP From the Server Behaviors panel, choose Add (+) and select Update Record. In the Update Record dialog, choose the current form. Select EditSurvey from the Submit Values From list. Select your data source from the list. Choose Recipes from the Data Source list. Enter your username and password, if needed. Select the table in the data source to insert into from the list. Choose Surveys (surveys for PHP) from the Insert into Table list. Set the data source fields to their corresponding form elements. Set SurveyID to the FORM.SurveyID form element as Numeric, with the Primary Key option selected. Set SurveyName to the FORM.SurveyTitle form element and submit it as Text. Set SurveyStartDate to the FORM.SurveyStartDate form element and submit it as Date MS Access type (Date in PHP). Set SurveyEndDate to the FORM.SurveyEndDate form element and submit it as Date MS Access type (Date in PHP). Set SurveyLive to the FORM.SurveyStatus form element and submit it as Checkbox MS Access type (Checkbox 1.0 in PHP). Set SurveyCompletedMessage to the FORM.SurveyCompletionMessage form element and submit it as Text. In the After Inserting, Go To field, enter the path to the file you want displayed after the record is updated. Choose Browse and select the survey_manager.cfm or survey_manager.php file, as appropriate. Check your entries to verify they are correct and, if so, click OK. Step 5: Apply Delete Commands/Queries So far, we've handled displaying and updating a survey's record. The next step and it's a fairly involved one is deleting the survey record. What makes it so involved is that not only must the current survey record be removed, but so must that survey's questions and responses to those surveys. These operations are accomplished through the use of SQL commands (known as queries in ColdFusion) covered in this step. One command, RemoveOrphanQuestions, removes all the fields in the SurveyQuestions table that no longer have a corresponding SurveyID in the Surveys table: [View full width] DELETE SurveyQuestions.* FROM Surveys RIGHT JOIN SurveyQuestions ON [Surveys].[SurveyID]=[SurveyQuestions] .[QuestionSurvey] WHERE ([Surveys].[SurveyID]) Is Null;
A second command, RemoveOrphanResults, performs a similar function for the results of questions that no longer exist: [View full width] DELETE SurveyResults.* FROM SurveyQuestions RIGHT JOIN SurveyResults ON [SurveyQuestions] .[QuestionID]=[SurveyResults].[ResultQuestion] WHERE ((([SurveyQuestions].[QuestionID]) Is Null));
As you can see, the sequence of code application is important here. First the survey record is removed, and then the survey's questions, followed by the survey questions' responses. A bit later in this chapter (Steps 6 and 7), we'll walk through moving the code to its proper location and linking it to the Delete button. It's best to wait to test these commands until all the steps have been completed. For ASP First, we'll insert a SQL command to remove the current record. The ID for the current record, MM_recordId, is gathered from the Update Server behavior, applied earlier. From the Bindings panel, choose Command. In the Command dialog, enter an appropriate name. Enter DeleteActive in the Name field. Choose the data source. From the Connections list, select Recipes. Set the type of command. From the Type list, choose Delete. Leave the Return Recordset Named options blank. In the SQL area, enter the following code: | DELETE FROM Surveys WHERE SurveyID = IDParam | In the Variable area, choose Add (+) to add a new SQL variable. Enter the name of the variable. In the Name column, enter IDParam. Insert the run-time value. In the Run-Time value column, enter Request("MM_recordId"). Click OK to insert the command. Note You'll notice in both instances that a Stored Procedure command type is used. Access does not truly support stored procedures, but this technique allows us to directly execute a database command. Next, we'll access a command included in the data source to remove the questions associated with the deleted survey. From the Bindings panel, choose Add (+) and choose Command. Enter an appropriate name for the command. Enter RemoveOrphanQuestions in the Name field. Choose a data source. From the Connections list, choose Recipes. Select the proper type of command. Choose Stored Procedure from the Type list. Leave the Return Recordset Named options empty. In the SQL area, enter the following command call: RemoveOrphanQuestions Click OK to close the dialog and insert the command. The final command to insert serves to remove any results attached to the previously removed questions. From the Bindings panel, choose Add (+) and choose Command. Enter an appropriate name for the command. Enter RemoveOrphanResults in the Name field. Choose a data source. From the Connections list, choose Recipes. Select the proper type of command. Choose Stored Procedure from the Type list. Leave the Return Recordset Named options empty. In the SQL area, enter the following command call: RemoveOrphanResults Click OK to close the dialog and insert the command. Note Again, refrain from testing the delete command until you've completed the remaining steps for this page. For ColdFusion The first task is to insert a SQL query to remove the current record. We'll get the ID for the current record from the session variable SurveyID stored earlier. From the Bindings panel, choose Recordset (Query). In the Advanced Recordset dialog, enter an appropriate name. Enter DeleteActive in the Name field. Choose the data source. From the Data Source list, select Recipes. Enter the username and password, if required. In the SQL area, enter the following code: | DELETE FROM Surveys WHERE SurveyID = #Session.SurveyID# | Click OK to insert the query. Next, we'll access a query included in the data source to remove the questions associated with the deleted survey. From the Bindings panel, choose Recordset (Query). In the Advanced Recordset dialog, enter an appropriate name. Enter RemoveOrphanQuestions in the Name field. Choose the data source. From the Data Source list, select Recipes. Enter the username and password, if required. In the SQL area, enter the following code: | EXECUTE RemoveOrphanQuestions | Click OK to insert the command. The final command to insert serves to remove any results attached to the previously removed questions. From the Bindings panel, choose Recordset (Query). In the Advanced Recordset dialog, enter an appropriate name. Enter RemoveOrphanResults in the Name field. Choose the data source. From the Data Source list, select Recipes. Enter the username and password, if required. In the SQL area, enter the following code: | EXECUTE RemoveOrphanResults | Click OK to insert the command. Note Again, refrain from testing the delete command until you've completed the remaining steps for this page. For PHP To handle the delete command in PHP, custom code is added. The code block inserted is conditional and only executes when the Delete button has been encountered. The code block must be placed following the session variable declaration. In Code view, place your cursor after the routine that sets the session variable. You'll find the code block near the top of the page. It looks like this: | <?php session_start(); if (isset($_GET['ID'])) { $_SESSION['ID'] = $_GET['SurveyID']; } else { header("Location: survey_manager.php"); } ?> | Insert the following code: From the Snippets panel, insert the Recipes > SurveyBuilder > CustomCode_PHP > Edit Survey Delete Command snippet. Save your page. PHP developers can skip the following two steps because the code is already properly placed and made conditional. Resume the recipe at Step 8. Step 6: Move Commands to the Top of the Page For the application to work properly, three groups of code the delete commands, the delete operation declaration, and the session variable declaration must be moved before the recordset declarations. As you can see from the Server Behaviors panel, the three delete commands (in ColdFusion, they appear with the prefix CFQUERY) added in the previous step now follow the two recordsets, Survey and Questions. Unfortunately, the Server Behaviors panel doesn't support drag-and-drop repositioning, so we'll have to move the code manually. Note This step is necessary only for ASP and ColdFusion users. First, let's reposition the code creating the three commands: Switch to Code view. In the Server Behaviors panel, choose the DeleteActive command. The first of the three commands is selected in the code. Select the current code function as well as the two following it. The selected code should look like this: | [View full width] <% set DeleteActive = Server.CreateObject("ADODB.Command") DeleteActive.ActiveConnection = MM_Recipes_STRING DeleteActive.CommandText = "DELETE FROM Surveys WHERE SurveyID = " + Replace(DeleteActive__IDParam, "'", "''") + " " DeleteActive.CommandType = 1 DeleteActive.CommandTimeout = 0 DeleteActive.Prepared = true DeleteActive.Execute() %> <% set RemoveOrphanQuestions = Server.CreateObject("ADODB.Command") RemoveOrphanQuestions.ActiveConnection = MM_Recipes_STRING RemoveOrphanQuestions.CommandText = "RemoveOrphanQuestions" RemoveOrphanQuestions.CommandType = 4 RemoveOrphanQuestions.CommandTimeout = 0 RemoveOrphanQuestions.Prepared = true RemoveOrphanQuestions.Execute() %> <% set RemoveOrphanResults = Server.CreateObject("ADODB.Command") RemoveOrphanResults.ActiveConnection = MM_Recipes_STRING RemoveOrphanResults.CommandText = "RemoveOrphanResults" RemoveOrphanResults.CommandType = 4 RemoveOrphanResults.CommandTimeout = 0 RemoveOrphanResults.Prepared = true RemoveOrphanResults.Execute() %> | | [View full width] <% set DeleteActive = Server.CreateObject("ADODB.Command") DeleteActive.ActiveConnection = MM_Recipes_STRING DeleteActive.CommandText = "DELETE FROM Surveys WHERE SurveyID = " + Replace(DeleteActive__IDParam, "'", "''") + " " DeleteActive.CommandType = 1 DeleteActive.CommandTimeout = 0 DeleteActive.Prepared = true DeleteActive.Execute() %> <% set RemoveOrphanQuestions = Server.CreateObject("ADODB.Command") RemoveOrphanQuestions.ActiveConnection = MM_Recipes_STRING RemoveOrphanQuestions.CommandText = "RemoveOrphanQuestions" RemoveOrphanQuestions.CommandType = 4 RemoveOrphanQuestions.CommandTimeout = 0 RemoveOrphanQuestions.Prepared = true RemoveOrphanQuestions.Execute() %> <% set RemoveOrphanResults = Server.CreateObject("ADODB.Command") RemoveOrphanResults.ActiveConnection = MM_Recipes_STRING RemoveOrphanResults.CommandText = "RemoveOrphanResults" RemoveOrphanResults.CommandType = 4 RemoveOrphanResults.CommandTimeout = 0 RemoveOrphanResults.Prepared = true RemoveOrphanResults.Execute() %> | | <cfquery name="DeleteActive" datasource="Recipes"> DELETE FROM Surveys WHERE SurveyID=#Session.SurveyID# </cfquery> <cfquery name="RemoveOrphanQuestions" datasource="Recipes"> EXECUTE RemoveOrphanQuestions </cfquery> <cfquery name="RemoveOrphanResults" datasource="Recipes"> EXECUTE RemoveOrphanResults </cfquery> | Cut the code by choosing Edit > Cut or press Ctrl-X (Command-X). Place the cursor at the top of the page. ASP users should place the cursor just after the Connection Include. Paste the code by choosing Edit > Paste or press Ctrl-V (Command-V). Save the page. Next, we'll need to move the delete operation declaration. (This step is only necessary for the ASP server models.) In Code view, use Find and Replace to locate the first instance of DeleteActive__IDParam. Note the double underscore characters in the name. Cut the surrounding function by choosing Edit > Cut or press Ctrl-X (Command-X). The code to cut looks like this: | [View full width] <% if(Request("MM_recordId") <> "") then DeleteActive__IDParam = Request("MM_recordId") %> | | [View full width] <% if(String(Request("MM_recordId")) != "undefined") { DeleteActive__IDParam = String(Request("MM_recordId"));}} %> | Move to the top of the page above the three commands moved previously. Paste the code by choosing Edit > Paste or press Ctrl-V (Command-V). Finally, we'll move the session definition to the top of the page. In Code view, locate the code function above the opening <html> tag. The code should look like this: | <% if (cStr(Request("SurveyID"))<>"") then Session("SurveyID") = cStr(Request("SurveyID")) end if if (cStr(Session("SurveyID"))="") then Response.Redirect("survey_manager.asp") end if %> | | <% if (String(Request("SurveyID"))!="undefined") Session("SurveyID") = String(Request("SurveyID")); if (String(Session("SurveyID"))=="undefined") Response.Redirect("survey_manager.asp"); %> | | <cfif isDefined("URL.SurveyID")> <cfset Session.SurveyID = URL.SurveyID> </cfif> <cfif not IsDefined("Session.SurveyID")> <cflocation url="survey_manager.cfm"> </cfif> | The ColdFusion code is located just above the <cfquery> tags. Select the code and cut it using Edit > Cut or press Ctrl-X (Command-X). Place the cursor at the top of the page under the Connection Include and paste the code by choosing Edit > Paste or pressing Ctrl-V (Command-V). Note Don't test the page until we make the delete code conditional! You could inadvertently delete a survey record. Step 7: Add Conditional Code As the code now stands, the current survey and all its associated questions and responses would be automatically deleted when this page is executed. Obviously, we want these actions to take place only when the Delete button is selected. To achieve this, we must make certain the code is conditional by wrapping it with an If-then clause so that it executes only when desired. Our conditional clause is added in two parts. The first sets up the condition and is placed before the DeleteActive declaration and the three commands/queries. The second follows the third command/query and redirects the user to a specific page when the delete action is complete. In Code view, position the cursor after the session variable declaration moved in the previous step and before the delete operation declaration. Insert the following code: From the Snippets panel, open the Recipes > SurveyBuilder > Custom Code folder for your server model and insert the If Statement - Delete Operation snippet. | <% if (cStr(Request("Delete"))<>"") then %> | | <% if (String(Request("Delete"))!="undefined") { %> | | [View full width] <cfif IsDefined("Session.SurveyID") AND IsDefined("FORM .Delete")> | Now let's add the second half of our conditional code. Place the cursor below the third delete command/query, which took care of removing the orphaned survey responses. Insert the following code: From the Snippets panel, open the Recipes > SurveyBuilder > Custom Code folder for your server model and insert the Redirect Survey Manager snippet. | <% Response.Redirect("survey_manager.asp") end if %> | | <% Response.Redirect("survey_manager.asp"); } %> | | <cflocation url="survey_manager.cfm"> </cfif> | Save the page. There's one last step to take, but the page can be previewed now if you like. Step 8: Link to Entering New Questions The final step is to configure the Add New Question button. We'll use the Go To URL behavior to open the appropriate add_question file while passing the survey ID as a parameter. Select the Add New Question form button. From the Behaviors panel, choose Add (+) and select Go To URL from the list. In the Go to URL dialog, select Browse to open the Select File dialog [r5-8].
Figure r5-8. Enter the following code in the File field: | add_question.asp?ID=<%=Session("SurveyID")%> | | add_question.asp?ID=<%=Session("SurveyID")%> | | add_question.cfm?ID=<cfoutput>#Session.SurveyID#</cfoutput> | | add_question.php?ID=<?php echo $_SESSION['SurveyID']; ?> | Click OK to close the dialog. Save your page. |