【发布时间】:2017-02-16 06:36:43
【问题描述】:
这是我第一次在这里发帖。我是一名初学者,正在尝试设计一个使用 Twilio API 发送 SMS 的工具。
我有以下几点:
- index.html 包含用于获取电话号码和消息的表单,并带有提交按钮以发布到 PHP 文件。
- send-sms.php 进行处理并在向最终用户显示成功消息之前在单独的页面上发送短信。
我想使用 AJAX 在 HTML 文件和 PHP 之间来回切换,以使用户留在那个 HTML 页面上。
有什么技巧可以做到这一点?
我的 HTML 是基本的:
<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>
如何在上面的 HTML 中使用 AJAX 与 PHP 通信并返回成功消息?
这是我的 PHP 文件中的一些代码:
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; ?>
【问题讨论】:
-
如果您想使用 ajax,请阅读有关它的教程(实际上有数以千计的教程,如果您只是 google 的话)并试一试。当您遇到困难时,请回来,向我们展示您的尝试,我们可以为您提供帮助。 SO 既不是教程网站,也不是免费的编码服务。