This is my first time posting here. I'm a beginner trying to design a tool that sends SMS using Twilio API.
I have the following:
- index.html containing the form to grab phone number and message with a submit button to post to the PHP file.
- send-sms.php that does the processing and sends the sms before displaying a success message on a separate page to the end user.
I want to use AJAX to do the back and forth between the HTML file and the PHP to keep the user on that HTML page.
Any tips to accomplish this?
My HTML is basic:
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<div class="container">
<form id="contact" action="sendsms.php" method="post">
<h3>Send SMS Tester</h3>
<h4>by <a href="mailto:blah@gmail.com">Uncle Dan</a></h4>
<fieldset>
<input placeholder="To Phone Number (format: +1555123456)" name="phone" type="tel" tabindex="3" required>
</fieldset>
<fieldset>
<textarea placeholder="Message Body (max 160 chars)" name="body" tabindex="5" required></textarea>
</fieldset>
<fieldset>
<input name="submit" type="submit" id="contact-submit" value="Send message" data-submit="...Sending">
</fieldset>
<!--<input type="submit" value="Send message" />-->
</form>
</div>
</html>
How do I use AJAX in the HTML above to communicate with the PHP and return success message back?
Here's some code from my PHP file:
require __DIR__ . '/twilio-php-master/Twilio/autoload.php';
// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;
// Credentials to connect to Twilio
$accountSid = 'AC';
$authToken = 'xxx';
// Create a connection to Twilio's API with credentials
$client = new Client($accountSid, $authToken);
// Actually send the number. This is where the magic happens! *** This needs rework! Use the bookmarked Twilio article to change.
//$client = new Services_Twilio($account_sid, $auth_token);
$message = $client->messages->create(
//'to' => $_POST['phone'],
$_POST['phone'],
array(
'from' => '+1555123456',
'body' => $_POST['body']));
if($message->sid):
// This content will appear conditionally when the form is submitted
?>
<p>Up, up and awaaaay! Your SMS has been sent :)</p>
<?php endif; ?>