douxi9245 2017-02-16 06:36
浏览 69

将值从HTML传递到PHP文件

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:

  1. index.html containing the form to grab phone number and message with a submit button to post to the PHP file.
  2. 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; ?>
  • 写回答

2条回答 默认 最新

  • duan0818 2017-02-16 06:40
    关注

    To use Ajax, there is little change in both files.

    First I suggest you to use jQuery Ajax from index.html and maintain the form sending Ajax request in submit action event. And in from file, the response is by echo and I suggest you to use a json_encode response.

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效