Tellme More - Mother of Perl | 3 | WebReference

Tellme More - Mother of Perl | 3

To page 1To page 2current pageTo page 4To page 5To page 6To page 7
[previous] [next]

Tellme More

Handling Form Input

VoiceXML includes a highly granular syntax for handling forms, and does require more work when compared to HTML forms. Each VoiceXML field can contain a prompt, code to handle the user inputs and errors, and a grammar. Grammars define the range of input values the user can provide. Once all fields have been filled, the form is submitted to another VoiceXML document or to a script on a Web server via the HTTP protocol.

There are two ways users can provide input to form fields. The first is via the phone keypad (DTMF tones). The second is through voice commands, which will be covered in Part II of the VoiceXML series. One common task is to ask a user to input their PIN code to identify who they are. Let's simulate this dialog:

Tellme: Please enter your 4 digit pin code.

User: 1234

Tellme: Good Morning Mr. Eisenzopf. How may I help you?

Now let's write the VoiceXML document that implements this interface.

<?xml version="1.0"?>
<vxml version="1.0" > 
  <form id="hello"> 
    <field name="pin"> 
      <prompt>Please enter your 4 digit pin code.</prompt> 
  	   <filled>
  	     <submit next="http://www.webreference.com/cgi-bin/perl/20/pin.pl"/>
      </filled> 
    </field> 
  </form>
</vxml>

The filled element is called when the system receives input for the field. The user input for the pin field is submitted to the CGI script specified in the next attribute of the submit element. Writing scripts for VoiceXML input is virtually identical to writing scripts for HTML forms.

The filled element can occur either within the field element:

<?xml version="1.0"?>
<vxml version="1.0" > 
  <form id="hello"> 
    <field name="pin"> 
      <prompt>Please enter your 4 digit pin code.</prompt> 
  	   <filled>
  	     <submit next="http://www.webreference.com/cgi-bin/perl/20/pin.pl"/>
      </filled> 
    </field> 
  </form>
</vxml>

or within the form element:

<?xml version="1.0"?>
<vxml version="1.0" > 
  <form id="hello"> 
    <field name="pin"> 
      <prompt>Please enter your 4 digit pin code.</prompt>  
    </field> 
    <field name="ccnum">
      <prompt>Please enter your credit card number.</prompt>
    </field>
    <filled>
  	   <submit next="http://www.webreference.com/cgi-bin/perl/20/pin.pl"/>
    </filled>
  </form>
</vxml>

This allows you to control input handling at both the field and/or the form level. For example, in some cases, you may want to process field values each time they're filled. Other times, you will want to wait until all fields have been filled.

In our examples above, we are using the submit element to send the field values to a script specified by the next attribute. There are a number of other attributes that you can use to control how and what is sent. By default, Tellme will send the form values via the HTTP protocol to the specified url using the GET method. You can override the default by specifying POST instead, via the method attribute:

<submit next="http://www.webreference.com/cgi-bin/perl/20/pin.pl" method="POST"/>

When submitting field values via the filled element within the form scope as opposed to a field, you can control what fields get passed with the namelist attribute:

<submit next="http://www.webreference.com/cgi-bin/perl/20/pin.pl" 
        namelist="pin ccnum"/>

In addition to the submit element, we have a number of other options for acting on field input. First, we could have sent the user to another form:

      <filled>
  	     <goto next="#first_name"/>
      </filled>

or included a message:

      <filled>
  	     <prompt>Thank you Mr. Rich. It will take 24 to 
             48 hours to funnel money into your off-shore 
             accounts.</prompt>
        <submit next="http://www.textant.com/cgi-bin/launder.pl"/>
      </filled>

Tellme also offers several options for processing input inline instead of submitting it to a script. The simplest alternative is the result element. If the value of the name attribute matches the field value, it will execute the statements inside.

<?xml version="1.0"?>
<vxml version="1.0" > 
  <form id="hello"> 
    <field name="pin"> 
      <prompt>Please enter your 4 digit pin code.</prompt> 
  	   <filled>
  	     <result name="1234">
          <prompt>Good Morning Mr. Eisenzopf. How may I help you?</prompt>
          <goto next="#secret_laboritory"/>
        </result> 
        <result name="2468">
          <prompt>Good Morning Mr. King. How may I help you?</prompt>
          <goto next="#control_room"/>
        </result>
      </filled> 
    </field> 
  </form>
</vxml>

While the result element can match simple strings, it doesn't give us the power of real condition statements like if and else, but it does work well where you have a pre-defined set of inputs. If the contents will be more dynamic, like a PIN code, we really would need conditional statements.


To page 1To page 2current pageTo page 4To page 5To page 6To page 7
[previous] [next]

Produced by Jonathan Eisenzopf
Created: February 23, 2001

URL: http://www.webreference.com/perl/tutorial/20/3.html