Administrator Recipe: Add Survey Questions

Now that we can add surveys, we need a way to add questions for each survey. Survey questions come in many different formats and our survey builder offers six different types: text question and answer, essay, multiple choice with one answer chosen from an option button group, multiple choice with one answer chosen from a drop-down list, multiple choice with multiple answers chosen from checkboxes, and true/false.

Each of these question types is input in the same fashion: The question is entered into a text field and the possible answers are entered into a text area field. When a question type offers multiple options for an answer, each option is placed on a separate line in the text area field. Server-side coding, covered later in this chapter, converts the text area field answer into the proper type whether drop-down list, radio button group, or whatever when the survey is presented to the user.

When the question is input completely, the standard Dreamweaver Insert Record behavior is used to add it to the data source and link it to a specific survey. In addition to the onscreen form elements, a hidden form field retains an ID number for the related survey. The survey ID is passed on to the data source record when the record is stored.

Step 1: Implement Add Survey Question Design

Because the formatting of the different types of question is handled by another page in the application, the Add Survey Question requires a limited number of form elements.

  1. Create a new page for your server model, either by hand or by using a template.

    graphics/book.gif In the SurveyBuilder folder, locate the folder for your server model and open the add_question page there.

  2. Insert a table in the Content region of your page to hold the form elements for the Add Survey Question application.

    graphics/book.gif From the Snippets panel, drag the Recipes > SurveyBuilder > Wireframes > Add Survey Question Wireframe snippet into the Content editable region.

  3. Add the form and the required elements: a drop-down list to choose the question type, a text field for entering the question, a text area field for the answers, two form buttons one for submitting the question and one for cancelling the operation and the previously mentioned hidden field.

    graphics/book.gif Place your cursor in the row below the words ADD SURVEY QUESTION and insert the Recipes > SurveyBuilder > Forms > Add Survey Question - Form snippet [r5-2].


    Figure r5-2.

    graphics/07fig02.jpg


  4. Save the file.

Step 2: Add Dynamic Attributes

Two elements require a nominal bit of hand-coding. First, we have to set the hidden element to store the survey ID passed to it by the Edit Survey page. Second, we need to add a bit of JavaScript to effectively abort the operation when the Cancel button is clicked.

Let's enter the code for the hidden element first:

  1. Make sure you can see the Hidden Form Field symbol in Design view. If it's not visible, choose Visual Aids > Invisible Elements from View Options on the Document toolbar.

    You might also need to enable Hidden Form Fields to display by selecting that option from the Invisible Elements category of Preferences.

  2. Select the hidden form element.

  3. In the Property inspector, enter the following code into the Value field:

    graphics/vb.gif

     <%= Request("ID")%> 

    graphics/js.gif

     <%= Request("ID")%> 

    graphics/cf.gif

     <cfoutput>#URL.ID#</cfoutput>

    graphics/php.gif

     <?php echo $_GET['ID']; ?> 

Next, we'll add the JavaScript to effectively cancel the operation:

  1. Click the Cancel form button.

  2. From the Behaviors panel, choose Add (+) and select Call JavaScript from the list.

  3. In the Call JavaScript dialog, enter history.back(); in the JavaScript field and click OK.

Step 3: Insert Record New Survey Question

After the user has entered the question and answer, the question is ready to be stored. To store the data, you apply the Insert Record server behavior.

For ASP
  1. From the Server Behaviors panel, choose Add (+) and select Insert Record from the list.

  2. In the Insert Record dialog, choose your data source.

    graphics/book.gif Choose Recipes from the Connection list.

  3. Select the table you want to insert the record into.

    graphics/book.gif From the Insert into Table list, choose SurveyQuestions.

  4. Set the page you want to appear after the record is inserted.

    graphics/book.gif Choose the edit_survey.asp file from your server model's folder for the After Inserting, Go To field.

  5. Select the form on the current page from which to gather the record values.

    graphics/book.gif From the Get Values From list, make sure AddSurveyQuestion is selected.

  6. In the Form Elements area, match the form elements on the page to the fields in the data source table.

    graphics/book.gif Set QuestionType to insert into the QuestionType column as Numeric type.

    Set QuestionText to insert into the QuestionText column as Text type.

    Set AnswerText to insert into the QuestionAnswers column as Text type.

    Set SurveyID to insert into the QuestionSurvey column as Numeric type.

  7. When you're sure your choices are correct, click OK to close the dialog and add the behavior.

For ColdFusion and PHP
  1. From the Server Behaviors panel, choose Add (+) and select Insert Record.

  2. In the Insert Record dialog, choose the current form.

    graphics/book.gif Select AddSurveyQuestion from the Submit Values From list.

  3. Select your data source from the list.

    graphics/book.gif Choose Recipes from the Data Source list.

  4. Enter your username and password, if needed.

  5. Select the table in the data source to insert into from the list.

    graphics/book.gif Choose SurveyQuestions (surveyQuestions for PHP) from the Insert into Table list.

  6. Set the data source fields to their corresponding form elements.

    graphics/book.gif Make sure the QuestionID data column is set to be an unused Primary Key.

    Set QuestionSurvey to the FORM.SurveyID form element and submit as Numeric type (Integer in PHP).

    Set QuestionType to the FORM.QuestionType form element and submit as Numeric type (Integer in PHP).

    Set QuestionText to the FORM.QuestionText form element and submit as Text type.

    Set QuestionAnswers to the FORM.AnswerText form element and submit as Text type.

  7. In the After Inserting, Go To field, enter edit_survey.cfm or edit_survey.php as appropriate and click OK to close the dialog.

  8. Save your page.



Macromedia Dreamweaver MX 2004 Web Application Recipes
Macromedia Dreamweaver MX 2004 Web Application Recipes
ISBN: 0735713200
EAN: 2147483647
Year: 2003
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net