我的用例是创建一个接受任何国际号码的HTML表单,并将其传递给一个php应用程序,该应用程序通过Twilio API调用该号码。现在PHP应用程序需要HTTP POST请求。
我正在使用intl-tel-input javascript来格式化和验证在html表单中输入的电话号码。作为一个新手,我想知道如何从运行在html表单上的javascript中获取格式化的数字,并从相同的html表单实例化post方法并调用php应用程序。
到目前为止我只得到了这个,我不确定如何设置post方法来调用php应用程序?有什么想法吗?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="build/css/intlTelInput.css">
</head>
<body>
<form>
<input id="phone" type="tel" name="phone">
<button type="submit">Call</button>
</form>
<script src="build/js/intlTelInput.js"></script>
<script src="build/js/prism.js"></script>
<script src="build/js/isValidNumber.js"></script>
<script>
var input = document.querySelector("#phone");
window.intlTelInput(input, {
separateDialCode: true,
utilsScript: "build/js/utils.js",
});
</script>
</body>
</html>以下是发起实际电话呼叫的PHP文件:
<?php
require_once '/path/to/vendor/autoload.php';
use Twilio\Rest\Client;
$sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$token = "your_auth_token";
$twilio = new Client($sid, $token);
$from = "xxxxx";
$to = $_POST['phone'];
$call = $twilio->calls
->create($to, $from, array("url" => "http://demo.twilio.com/docs/voice.xml"));
print($call->sid);正如我提到的,问题是如何从上面的html表单调用此php应用程序,并将phone变量中的值传递给它?
发布于 2019-07-18 15:59:44
First <button type="submit">Call</button> => <button type="button" id="submitButton">Call</button>
然后添加一些Javascript:
<script>
window.onload = function() {
const button = document.querySelector("#submitButton");
button.addEventListener('click', () => {
sendRequest();
alert('button clicked');
});
}
function sendRequest() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
}
};
const number = document.querySelector("#phone").value;
xhttp.open("POST", "/path/to/php/endpoint", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(`number=${number}`);
}
</script>发布于 2019-07-18 16:03:17
使用带有post方法的jquery ajax调用:如下所示:- html:
<form>
<input id="phone" type="tel" name="phone">
<button type="button" onclick="return send_number();">Call</button>
</form>jquery:
function send_number()
{
var post_data = { };
var phone = $("#phone").val();
post_data['phone'] = phone;
var APPUrl = 'App_receive_number.php';
$.ajax({
type: "POST",
url: APPUrl,
data: post_data,
cache: false,
success: function(res)
{
alert("Success! Sent!"); return true;
},
error: function (xhr, ajaxOptions, thrownError) {
alert("Failed! Please Try Again!");
return false;
}
});
}https://stackoverflow.com/questions/57089577
复制相似问题