We've Moved! Please visit our new and improved forum over at our new portal: https://portal.plumvoice.com/hc/en-us/community/topics

Receiving variables from foreign databases

Questions and answers about IVR programming for Plum DEV

Moderators: admin, support

Post Reply
aghuber11
Posts: 14
Joined: Tue Jun 21, 2016 1:02 pm

Receiving variables from foreign databases

Post by aghuber11 »

Hello,

I'm attempting to make an ivr system that references a foreign database (mine for this example) and then pull information from it and return it as a variable to the original post page.

Here is the code from the original ivr system - start.php

Code: Select all

<?php
  header("Content-type: text/xml");
  echo "<?xml version=\"1.0\"?>\n";
?>

<vxml version="2.0">
  <var name="license_number"/>
  <var name="request_type"/>
  <var name="pin"/>
  <form id="login">
     <field name="pin" type="digits">
     <prompt>
         Please enter or say your pin.
     </prompt>
     <filled>
         <submit next="http://hubenterprises.com.mx/test_script/pin.php" method="post" namelist="pin"/>
     </filled>
     <noinput>
        I'm sorry, I didn't hear you. Please try again.
        <reprompt/>
     </noinput>
     </field>
    <field name="license" type="digits">
      <prompt>
        Please enter your license number
      </prompt>
      <filled>
        <assign name="license_number" expr="license"/>
      </filled>
    </field>
    <field name="request">
      <grammar type="application/srgs+xml" mode="dtmf" root="choice" maxdigits="1">
        <rule id="choice">
          <one-of>
            <item>1</item>
            <item>2</item>
          </one-of>
        </rule>
      </grammar>
      <prompt>
        To renew your license, press 1. To request a new license plate, press 2.
      </prompt>
      <filled>
        <assign name="request_type" expr="request"/>
        <submit next="process_request.php" method="post" namelist="
        license_number request_type" fetchtimeout="60s"/>
      </filled>
    </field>
    </form>
  </vxml>
Here is the code for the php page. - pin.php

Code: Select all

<?php
  header("Content-type: text/xml");
  echo "<?xml version=\"1.0\"?>\n";
?>
<?php

header("Content-type: application/voicexml+xml");

$connection = mysql_connect("databasename","user","PW");
mysql_select_db("database");

$result = mysql_query("SELECT * FROM DUMMY WHERE pin = '" . $_POST["pin"]

. "'");

?>
<?
if (mysql_num_rows($result) > 0) {

$row = mysql_fetch_assoc($result);

?>
<vxml version="2.0">
  <form id="login">
    <field name="pin" expr="<?=$_POST['pin']?>"/>
      <filled>
        <if cond="pin!='<?=$row['pin']?>'">
            <clear namelist="pin"/><throw event="nomatch"/>
        <else/>
    <submit next="http://hubenterprises.com.mx/test_script/start.php" namelist="pin" method="post"/>
        </if>
      </filled>
      <noinput>
           Error<reprompt/>
      </noinput>
    </field>
  </form>

<?
} else {
?>
    <form>
        <block>
            There was no pin passed in.
        </block>
    </form>

<?

}

?>

</vxml>

<?
mysql_close($connection);

?>
Help!

Thank you for your time

support
Posts: 3632
Joined: Mon Jun 02, 2003 3:47 pm
Location: Boston, MA
Contact:

Re: Receiving variables from foreign databases

Post by support »

Hello,

It seems as though part of your problem may be that you should be using a <subdialog> element rather than <submit>. The <submit> element is meant to be used to transition to the page specified by the "next" property, and therefore will not return to the initial page it's called from. Here is an example of how to use a <subdialog> element:

start.vxml:

Code: Select all

<?xml version="1.0"?>
<vxml version="2.0">
  <form id="login">
     <field name="pin" type="digits">
         <prompt>
         Please enter your pin.
         </prompt>
         <filled>
             <subdialog name="sub" next="subdialog.php" method="post" namelist="pin"/>
             You entered <value expr="sub.pin_number"/>.
         </filled>
         <noinput>
            I'm sorry, I didn't hear you. Please try again.
            <reprompt/>
         </noinput>
     </field>
 </form>
</vxml>


subdialog.php:

Code: Select all

<?php
     header("Content-type: text/xml");
     echo("<?xml version=\"1.0\"?>\n");
     $pin_number = $_POST["pin"];
?>
<vxml version="2.0">
       <form>
         <block>
           <var name="pin_number" expr="'<?php echo($pin_number)?>'"/>
           <return namelist="pin_number"/>
         </block>
       </form>
</vxml>
Regards,
Plum Support

aghuber11
Posts: 14
Joined: Tue Jun 21, 2016 1:02 pm

Re: Receiving variables from foreign databases

Post by aghuber11 »

Thank you for your time, i appreciate your response

aghuber11
Posts: 14
Joined: Tue Jun 21, 2016 1:02 pm

Re: Receiving variables from foreign databases

Post by aghuber11 »

Hello,

I have re-evaluated my approach to how i'm doing this and i just have one simple question.

Once i use a select query to query my DB and get the rows that i need, how do i parse them into VXML variables so i can send them back using the namelist function?

The only information that is being returned from this pin is from the row license_number (which will be changed to their name eventually) how do i capture it in a VXML var

something like <var name="first_name" expr="'<?= $first_name ?>'"/>

This is a subdialog code thats ment to return database data to my IVR system.

Here is the initial subdialog code

Code: Select all

   <subdialog name="result" src="subdialog.php" namelist="result">
         <filled>
           Your name is <value expr="result.pin"/>.
           <assign name="pin_number" expr="result.pin"/>
         </filled>
       </subdialog>
Here is my Query

Code: Select all

<?php
     header("Content-type: text/xml");
     echo("<?xml version=\"1.0\"?>\n");
     //Connecting to sql db.
     $connect = mysqli_connect("host", "user", "Password", "Database Name");
     //retrieving form data to sql db.
     /* Create a prepared statement */
     $stmt = $mysqli -> prepare("SELECT license_number FROM request WHERE pin_number=?");

     /* Bind parameters */
     $stmt -> bind_param("s", $_POST['result']);

     /* Execute it */
     $stmt -> execute();

     /* Bind results */
     $stmt -> bind_result($result);

    /* Fetch the value */
    $stmt -> fetch();

    echo $result;
?>
<var name="pin_number"/>
<vxml version="2.0">
  <form>
    <field name="pin" type="digits">
      <prompt>what is your pin?</prompt>
      <filled>
        <return namelist="pin"/>
      </filled>
    </field>
  </form>
</vxml>
Thank you for your time

support
Posts: 3632
Joined: Mon Jun 02, 2003 3:47 pm
Location: Boston, MA
Contact:

Re: Receiving variables from foreign databases

Post by support »

Hello,

To return PHP variables to your VXML code, you will want to include something like this under the PHP code where you define your $first_name variable:

Code: Select all

<vxml version="2.0">
   <form>
       <block>
          <var name="first_name" expr="'<?php echo($first_name)?>'"
          <return namelist="first_name"/>
       </block>
   </form>
</vxml>
If you would like to pass multiple PHP variables back to your original VXML code, you could do something like the following underneath your PHP script:

Code: Select all

<vxml version="2.0">
   <form>
      <block>
         <var name="first_name" expr="'<?php echo($first_name)?>'"/>
         <var name="last_name" expr="'<?php echo($last_name)?>'"/>
         <var name="DOB" expr="'<?php echo($date_of_birth)?>'"/>
         <return namelist="first_name last_name DOB"/>
      </block>
   </form>
</vxml>
where you are passing your PHP variables $first_name, $last_name, and $date_of_birth into the VXML variables first_name, last_name, and DOB.

In your subdialog.php, it seems as though you are prompting the user for their pin after querying your database, so your subdialog will only return the pin number that the user enters instead of any data queried with that pin number. If you would like to query your database with that pin number, we recommend that you prompt the user for the pin number before using the <subdialog> tag in the first bit of code you provided, and then pass that pin number into subdialog.php using the namelist property.

Regards,
Plum Support

aghuber11
Posts: 14
Joined: Tue Jun 21, 2016 1:02 pm

Re: Receiving variables from foreign databases

Post by aghuber11 »

i think i love you guys.

Thank you again for your prompt response

Post Reply