- Twilio Cookbook
- Roger Stringer
- 314字
- 2021-08-06 16:53:35
Letting users subscribe to receive surveys
Before we can send surveys, we want to have a way for users to subscribe to them.
This form will let users enter their phone numbers and add them to the survey system.
You can put this page on a section of your website and allow people to sign up to receive your surveys.
Getting ready
The complete source code for this recipe can be found in the Chapter3/
folder of the code bundle available at http://www.packtpub.com/support.
How to do it...
Let's build a handy system to let users subscribe to receive our surveys. Perform the following steps:
- Load the
sql.sql
file into your database. - Upload
config.php
to your website and make sure the following variables are set:<?php $accountsid = ''; // YOUR TWILIO ACCOUNT SID $authtoken = ''; // YOUR TWILIO AUTH TOKEN $fromNumber = ''; // PHONE NUMBER CALLS WILL COME FROM $dbhost = ''; // YOUR DATABASE HOST $dbname = ''; // YOUR DATABASE NAME $dbuser = ''; // YOUR DATABASE USER $dbpass = ''; // YOUR DATABASE PASS ?>
- Upload
pdo.class.php
to your website. - Upload a file on your web server called
subscribe.php
, with the following content:<?php include("config.php"); include("pdo.class.php"); include 'Services/Twilio.php'; $action = isset($_GET['action']) ? $_GET['action'] : null; switch($action){ case 'save': $fields = array('phone_number','status'); $pfields = array(); $_POST['status'] = 1; foreach( $fields as $k){ $v = $_POST[$k]; $pfields[] = "{$k} = '{$v}'"; } $sql = "INSERT INTO subscribers SET ".implode(",",$pfields); $pdo = Db::singleton(); $pdo->exec($sql); $qid = $pdo->lastInsertId(); if( isset($qid) && !empty($qid) ){ ?> <p>Thank you, you have been subscribed to receive surveys</p> <?php } default: ?> <h2>Subscribe to receive surveys</h2> <form method="POST" action="subscribe.php?action=save"> <table> <tr> <td>Please enter your phone number</td> <td><input type="text" name="phone_number" /></td> </tr> </table> <button type="submit">Save</button> </form> <?php break; } ?>
How it works...
In step 1, we set up our database; in step 2, we configured the settings.
In step 4, we uploaded subscribe.php
. The subscribe.php
file gives users a form where they enter their phone number and get added to the survey. This can work inside a Sales page on your website, where you want people to subscribe to your surveys.
I prefer letting users subscribe for surveys themselves rather than manually adding users to surveys, as it lets us get a better grasp on the system. When we give a subscriber some sort of incentive to sign up for a survey, they are more likely to reply to the surveys we send them. For example, we could use this as a contest, where each subscriber is entered into a draw for a prize, but only if they reply to our surveys.